Merge pull request #1 from akiyamaneko/rc4_enhanced

RC4特性增强
This commit is contained in:
neko 2020-09-23 00:07:27 +08:00 committed by GitHub
commit 322c95fde2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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);
}
/**
* 解密Hex16进制或Base64表示的字符串使用默认编码UTF-8
*
* @param message 消息
* @return 明文
*/
public String decrypt(String message) {
return decrypt(SecureUtil.decode(message));
}
/**
* 解密Hex16进制或Base64表示的字符串
*
* @param message 明文
* @param charset 解密后的charset
* @return 明文
*/
public String decrypt(String message, Charset charset) {
return StrUtil.str(decrypt(message), charset);
}
/**
* 加密或解密指定值调用此方法前需初始化密钥
*
@ -216,4 +260,4 @@ public class RC4 implements Serializable {
sbox[j] = temp;
}
//----------------------------------------------------------------------------------------------------------------------- Private method end
}
}