mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
Merge pull request #1108 from akiyamaneko/rc4_enhanced
Rc4 API enhanced
This commit is contained in:
commit
08730bac39
@ -11,6 +11,7 @@ import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.HexUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.CryptoException;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
|
||||
/**
|
||||
* RC4加密解密算法实现<br>
|
||||
@ -97,6 +98,16 @@ public class RC4 implements Serializable {
|
||||
return HexUtil.encodeHexStr(encrypt(data, charset));
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密,使用UTF-8编码
|
||||
*
|
||||
* @param data 被加密的字符串
|
||||
* @return 加密后的Hex
|
||||
*/
|
||||
public String encryptHex(String data) {
|
||||
return HexUtil.encodeHexStr(encrypt(data));
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密
|
||||
*
|
||||
@ -109,6 +120,17 @@ public class RC4 implements Serializable {
|
||||
return Base64.encode(encrypt(data, charset));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 加密,使用UTF-8编码
|
||||
*
|
||||
* @param data 被加密的字符串
|
||||
* @return 加密后的Base64
|
||||
*/
|
||||
public String encryptBase64(String data) {
|
||||
return Base64.encode(encrypt(data));
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密
|
||||
*
|
||||
@ -132,6 +154,28 @@ public class RC4 implements Serializable {
|
||||
return decrypt(message, CharsetUtil.CHARSET_UTF_8);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密Hex(16进制)或Base64表示的字符串,使用默认编码UTF-8
|
||||
*
|
||||
* @param message 消息
|
||||
* @return 明文
|
||||
*/
|
||||
public String decrypt(String message) {
|
||||
return decrypt(SecureUtil.decode(message));
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密Hex(16进制)或Base64表示的字符串
|
||||
*
|
||||
* @param message 明文
|
||||
* @param charset 解密后的charset
|
||||
* @return 明文
|
||||
*/
|
||||
public String decrypt(String message, Charset charset) {
|
||||
return StrUtil.str(decrypt(message), charset);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 加密或解密指定值,调用此方法前需初始化密钥
|
||||
*
|
||||
|
@ -1,5 +1,6 @@
|
||||
package cn.hutool.crypto.test.symmetric;
|
||||
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@ -36,4 +37,35 @@ public class RC4Test {
|
||||
String msg2 = rc4.decrypt(crypt2);
|
||||
Assert.assertEquals(message2, msg2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecryptWithHexMessage() {
|
||||
String message = "这是第一个用来测试密文为十六进制字符串的消息!";
|
||||
String key = "生成一个密钥";
|
||||
RC4 rc4 = new RC4(key);
|
||||
String encryptHex = rc4.encryptHex(message, CharsetUtil.CHARSET_UTF_8);
|
||||
String msg = rc4.decrypt(encryptHex);
|
||||
Assert.assertEquals(message, msg);
|
||||
|
||||
String message2 = "这是第二个用来测试密文为十六进制字符串的消息!";
|
||||
String encryptHex2 = rc4.encryptHex(message2);
|
||||
String msg2 = rc4.decrypt(encryptHex2);
|
||||
Assert.assertEquals(message2, msg2);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDecryptWithBase64Message() {
|
||||
String message = "这是第一个用来测试密文为Base64编码的消息!";
|
||||
String key = "生成一个密钥";
|
||||
RC4 rc4 = new RC4(key);
|
||||
String encryptHex = rc4.encryptBase64(message, CharsetUtil.CHARSET_UTF_8);
|
||||
String msg = rc4.decrypt(encryptHex);
|
||||
Assert.assertEquals(message, msg);
|
||||
|
||||
String message2 = "这是第一个用来测试密文为Base64编码的消息!";
|
||||
String encryptHex2 = rc4.encryptBase64(message2);
|
||||
String msg2 = rc4.decrypt(encryptHex2);
|
||||
Assert.assertEquals(message2, msg2);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user