|
|
@@ -1,5 +1,7 @@
|
|
|
package com.dk.common.util;
|
|
|
|
|
|
+import org.apache.commons.codec.binary.Base64;
|
|
|
+import org.springframework.util.Base64Utils;
|
|
|
import sun.misc.BASE64Decoder;
|
|
|
import sun.misc.BASE64Encoder;
|
|
|
|
|
|
@@ -8,10 +10,10 @@ import javax.crypto.KeyGenerator;
|
|
|
import javax.crypto.SecretKey;
|
|
|
import javax.crypto.spec.IvParameterSpec;
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
+import java.nio.charset.Charset;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.security.SecureRandom;
|
|
|
|
|
|
-import org.apache.commons.codec.binary.Base64;
|
|
|
-
|
|
|
/**
|
|
|
* @author : 周兴
|
|
|
* @desc : 加密工具类
|
|
|
@@ -19,12 +21,17 @@ import org.apache.commons.codec.binary.Base64;
|
|
|
*/
|
|
|
public class AESUtil {
|
|
|
|
|
|
- private static final String sysKey = "qwertyuiopasdf12";
|
|
|
-
|
|
|
+ private static final String sysKey = "qwertyuiopasdf18";
|
|
|
// 使用AES-128-CBC加密模式,key需要为16位,key和iv可以相同!
|
|
|
- private static String KEY = "qwertyuiopasdf12";
|
|
|
+ private static String KEY = "qwertyuiopasdf18";
|
|
|
+
|
|
|
+ private static String IV = "qwertyuiopasdf18";
|
|
|
|
|
|
- private static String IV = "qwertyuiopasdf12";
|
|
|
+ private static final String ALG_AES_CBC_PKCS5 = "AES/CBC/PKCS5Padding";
|
|
|
+
|
|
|
+ private static final Charset UTF8 = StandardCharsets.UTF_8;
|
|
|
+
|
|
|
+ private static final String ENCODING = "UTF-8";
|
|
|
|
|
|
/**
|
|
|
* 加密方法
|
|
|
@@ -33,7 +40,7 @@ public class AESUtil {
|
|
|
* @return 加密的结果
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static String aesEncrypt(String data) throws Exception {
|
|
|
+ public static String aesEncrypt(String data) {
|
|
|
try {
|
|
|
String key = KEY;
|
|
|
String iv = IV;
|
|
|
@@ -56,14 +63,24 @@ public class AESUtil {
|
|
|
byte[] encrypted = cipher.doFinal(plaintext);
|
|
|
|
|
|
return new Base64().encodeToString(encrypted);
|
|
|
-
|
|
|
+// //对密码进行编码
|
|
|
+// byte[] bytes = KEY.getBytes(ENCODING);
|
|
|
+// //设置加密算法,生成秘钥
|
|
|
+// SecretKeySpec skeySpec = new SecretKeySpec(bytes, "AES");
|
|
|
+// // "算法/模式/补码方式"
|
|
|
+// Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
|
|
|
+// //选择加密
|
|
|
+// cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
|
|
|
+// //根据待加密内容生成字节数组
|
|
|
+// byte[] encrypted = cipher.doFinal(data.getBytes(ENCODING));
|
|
|
+// //返回base64字符串
|
|
|
+// return Base64Utils.encodeToString(encrypted);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 解密方法
|
|
|
*
|
|
|
@@ -75,14 +92,18 @@ public class AESUtil {
|
|
|
try {
|
|
|
String key = KEY;
|
|
|
String iv = IV;
|
|
|
- byte[] encrypted1 = new Base64().decode(data);
|
|
|
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
|
|
|
+// Cipher cipher = Cipher.getInstance(ALG_AES_CBC_PKCS5);
|
|
|
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
|
|
|
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
|
|
|
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
|
|
|
+// byte[] encrypted1 = new Base64().decode(data);
|
|
|
+ byte[] encrypted1 = Base64.decodeBase64(data);
|
|
|
byte[] original = cipher.doFinal(encrypted1);
|
|
|
- String originalString = new String(original, "utf-8");
|
|
|
- return originalString;
|
|
|
+// String originalString = new String(original, "utf-8");
|
|
|
+// return originalString;
|
|
|
+ // 输出utf8编码的字符串,输出字符串需要指定编码格式
|
|
|
+ return new String(original, UTF8);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return null;
|