|
|
@ -17,8 +17,8 @@ public class AesEncryptUtil { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* 使用AES-128-CBC加密模式 key和iv可以相同 |
|
|
|
* 使用AES-128-CBC加密模式 key和iv可以相同 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private static String KEY = EncryptedString.key; |
|
|
|
private static final String KEY = EncryptedString.key; |
|
|
|
private static String IV = EncryptedString.iv; |
|
|
|
private static final String IV = EncryptedString.iv; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 加密方法 |
|
|
|
* 加密方法 |
|
|
@ -68,9 +68,9 @@ public class AesEncryptUtil { |
|
|
|
* @return 解密的结果 |
|
|
|
* @return 解密的结果 |
|
|
|
* @throws Exception |
|
|
|
* @throws Exception |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static String desEncrypt(String data, String key, String iv) throws Exception { |
|
|
|
public static String decrypt(String data, String key, String iv) throws Exception { |
|
|
|
//update-begin-author:taoyan date:2022-5-23 for:VUEN-1084 【vue3】online表单测试发现的新问题 6、解密报错 ---解码失败应该把异常抛出去,在外面处理
|
|
|
|
//update-begin-author:taoyan date:2022-5-23 for:VUEN-1084 【vue3】online表单测试发现的新问题 6、解密报错 ---解码失败应该把异常抛出去,在外面处理
|
|
|
|
byte[] encrypted1 = Base64.decode(data); |
|
|
|
byte[] encrypted = Base64.decode(data); |
|
|
|
|
|
|
|
|
|
|
|
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); |
|
|
|
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); |
|
|
|
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES"); |
|
|
|
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES"); |
|
|
@ -78,7 +78,7 @@ public class AesEncryptUtil { |
|
|
|
|
|
|
|
|
|
|
|
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec); |
|
|
|
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec); |
|
|
|
|
|
|
|
|
|
|
|
byte[] original = cipher.doFinal(encrypted1); |
|
|
|
byte[] original = cipher.doFinal(encrypted); |
|
|
|
String originalString = new String(original); |
|
|
|
String originalString = new String(original); |
|
|
|
//加密解码后的字符串会出现\u0000
|
|
|
|
//加密解码后的字符串会出现\u0000
|
|
|
|
return originalString.replaceAll("\\u0000", ""); |
|
|
|
return originalString.replaceAll("\\u0000", ""); |
|
|
@ -103,26 +103,26 @@ public class AesEncryptUtil { |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
* @throws Exception |
|
|
|
* @throws Exception |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static String desEncrypt(String data) throws Exception { |
|
|
|
public static String decrypt(String data) throws Exception { |
|
|
|
return desEncrypt(data, KEY, IV); |
|
|
|
return decrypt(data, KEY, IV); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
/** |
|
|
|
// * 测试
|
|
|
|
* 测试 |
|
|
|
// */
|
|
|
|
*/ |
|
|
|
// public static void main(String args[]) throws Exception {
|
|
|
|
public static void main(String args[]) throws Exception { |
|
|
|
// String test1 = "sa";
|
|
|
|
String test1 = "sa"; |
|
|
|
// String test =new String(test1.getBytes(),"UTF-8");
|
|
|
|
String test = new String(test1.getBytes(), "UTF-8"); |
|
|
|
// String data = null;
|
|
|
|
String data = null; |
|
|
|
// String key = KEY;
|
|
|
|
String key = KEY; |
|
|
|
// String iv = IV;
|
|
|
|
String iv = IV; |
|
|
|
// // /g2wzfqvMOeazgtsUVbq1kmJawROa6mcRAzwG1/GeJ4=
|
|
|
|
// /g2wzfqvMOeazgtsUVbq1kmJawROa6mcRAzwG1/GeJ4=
|
|
|
|
// data = encrypt(test, key, iv);
|
|
|
|
data = encrypt(test, key, iv); |
|
|
|
// System.out.println("数据:"+test);
|
|
|
|
System.out.println("数据:" + test); |
|
|
|
// System.out.println("加密:"+data);
|
|
|
|
System.out.println("加密:" + data); |
|
|
|
// String jiemi =desEncrypt(data, key, iv).trim();
|
|
|
|
String jiemi = decrypt("luLwvA8vet/J6MFGawa4OQ==", key, iv).trim(); |
|
|
|
// System.out.println("解密:"+jiemi);
|
|
|
|
System.out.println("解密:" + jiemi); |
|
|
|
// }
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|