Browse Source

RC4特性增强

neko 5 years ago
parent
commit
543bd0b45f
1 changed files with 45 additions and 1 deletions
  1. 45 1
      hutool-crypto/src/main/java/cn/hutool/crypto/symmetric/RC4.java

+ 45 - 1
hutool-crypto/src/main/java/cn/hutool/crypto/symmetric/RC4.java

@@ -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>
@@ -98,6 +99,16 @@ public class RC4 implements Serializable {
 	}
 
 	/**
+	 * 加密,使用UTF-8编码
+	 *
+	 * @param data 被加密的字符串
+	 * @return 加密后的Hex
+	 */
+	public String encryptHex(String data) {
+		return HexUtil.encodeHexStr(encrypt(data));
+	}
+
+	/**
 	 * 加密
 	 * 
 	 * @param 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));
+	}
+
 	/**
 	 * 解密
 	 *
@@ -133,6 +155,28 @@ public class RC4 implements Serializable {
 	}
 
 	/**
+	 * 解密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);
+	}
+
+
+	/**
 	 * 加密或解密指定值,调用此方法前需初始化密钥
 	 *
 	 * @param msg 要加密或解密的消息
@@ -216,4 +260,4 @@ public class RC4 implements Serializable {
 		sbox[j] = temp;
 	}
 	//----------------------------------------------------------------------------------------------------------------------- Private method end
-}
+}