|
@@ -1,13 +1,15 @@
|
|
|
package cn.licoy.encryptbody.util;
|
|
package cn.licoy.encryptbody.util;
|
|
|
|
|
|
|
|
-import javax.crypto.Cipher;
|
|
|
|
|
-import javax.crypto.KeyGenerator;
|
|
|
|
|
-import javax.crypto.SecretKey;
|
|
|
|
|
|
|
+import javax.crypto.*;
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
|
|
+import java.security.InvalidKeyException;
|
|
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
import java.security.SecureRandom;
|
|
import java.security.SecureRandom;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* <p>AES加密处理工具类</p>
|
|
* <p>AES加密处理工具类</p>
|
|
|
|
|
+ *
|
|
|
* @author licoy.cn
|
|
* @author licoy.cn
|
|
|
* @version 2018/9/5
|
|
* @version 2018/9/5
|
|
|
*/
|
|
*/
|
|
@@ -15,50 +17,48 @@ public class AESEncryptUtil {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* AES加密
|
|
* AES加密
|
|
|
|
|
+ *
|
|
|
* @param content 字符串内容
|
|
* @param content 字符串内容
|
|
|
* @param password 密钥
|
|
* @param password 密钥
|
|
|
*/
|
|
*/
|
|
|
- public static String encrypt(String content, String password){
|
|
|
|
|
- return aes(content,password,Cipher.ENCRYPT_MODE);
|
|
|
|
|
|
|
+ public static String encrypt(String content, String password) throws Exception {
|
|
|
|
|
+ return aes(content, password, Cipher.ENCRYPT_MODE);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* AES解密
|
|
* AES解密
|
|
|
|
|
+ *
|
|
|
* @param content 字符串内容
|
|
* @param content 字符串内容
|
|
|
* @param password 密钥
|
|
* @param password 密钥
|
|
|
*/
|
|
*/
|
|
|
- public static String decrypt(String content, String password){
|
|
|
|
|
- return aes(content,password,Cipher.DECRYPT_MODE);
|
|
|
|
|
|
|
+ public static String decrypt(String content, String password) throws Exception {
|
|
|
|
|
+ return aes(content, password, Cipher.DECRYPT_MODE);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* AES加密/解密 公共方法
|
|
* AES加密/解密 公共方法
|
|
|
|
|
+ *
|
|
|
* @param content 字符串
|
|
* @param content 字符串
|
|
|
* @param password 密钥
|
|
* @param password 密钥
|
|
|
* @param type 加密:{@link Cipher#ENCRYPT_MODE},解密:{@link Cipher#DECRYPT_MODE}
|
|
* @param type 加密:{@link Cipher#ENCRYPT_MODE},解密:{@link Cipher#DECRYPT_MODE}
|
|
|
*/
|
|
*/
|
|
|
- private static String aes(String content, String password, int type) {
|
|
|
|
|
- try {
|
|
|
|
|
- KeyGenerator generator = KeyGenerator.getInstance("AES");
|
|
|
|
|
- SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
|
|
|
|
|
- random.setSeed(password.getBytes());
|
|
|
|
|
- generator.init(128, random);
|
|
|
|
|
- SecretKey secretKey = generator.generateKey();
|
|
|
|
|
- byte[] enCodeFormat = secretKey.getEncoded();
|
|
|
|
|
- SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
|
|
|
|
|
- Cipher cipher = Cipher.getInstance("AES");
|
|
|
|
|
- cipher.init(type, key);
|
|
|
|
|
- if (type == Cipher.ENCRYPT_MODE) {
|
|
|
|
|
- byte[] byteContent = content.getBytes("utf-8");
|
|
|
|
|
- return Hex2Util.parseByte2HexStr(cipher.doFinal(byteContent));
|
|
|
|
|
- } else {
|
|
|
|
|
- byte[] byteContent = Hex2Util.parseHexStr2Byte(content);
|
|
|
|
|
- return new String(cipher.doFinal(byteContent));
|
|
|
|
|
- }
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
|
|
+ private static String aes(String content, String password, int type) throws Exception {
|
|
|
|
|
+ KeyGenerator generator = KeyGenerator.getInstance("AES");
|
|
|
|
|
+ SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
|
|
|
|
|
+ random.setSeed(password.getBytes());
|
|
|
|
|
+ generator.init(128, random);
|
|
|
|
|
+ SecretKey secretKey = generator.generateKey();
|
|
|
|
|
+ byte[] enCodeFormat = secretKey.getEncoded();
|
|
|
|
|
+ SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
|
|
|
|
|
+ Cipher cipher = Cipher.getInstance("AES");
|
|
|
|
|
+ cipher.init(type, key);
|
|
|
|
|
+ if (type == Cipher.ENCRYPT_MODE) {
|
|
|
|
|
+ byte[] byteContent = content.getBytes("utf-8");
|
|
|
|
|
+ return Hex2Util.parseByte2HexStr(cipher.doFinal(byteContent));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ byte[] byteContent = Hex2Util.parseHexStr2Byte(content);
|
|
|
|
|
+ return new String(cipher.doFinal(byteContent));
|
|
|
}
|
|
}
|
|
|
- return null;
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|