|
@@ -7,6 +7,7 @@ import org.slf4j.LoggerFactory;
|
|
|
import javax.crypto.Cipher;
|
|
|
import javax.crypto.spec.IvParameterSpec;
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
@@ -19,7 +20,7 @@ import java.util.Map;
|
|
|
public class PrivacyAesUtil {
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(PrivacyAesUtil.class);
|
|
|
- private static byte[] Keys = {0x41, 0x72, 0x65, 0x79, 0x6F, 0x75, 0x6D, 0x79, 0x53, 0x6E, 0x6F, 0x77, 0x6D, 0x61, 0x6E, 0x3F};
|
|
|
+ private static final byte[] Keys = {0x41, 0x72, 0x65, 0x79, 0x6F, 0x75, 0x6D, 0x79, 0x53, 0x6E, 0x6F, 0x77, 0x6D, 0x61, 0x6E, 0x3F};
|
|
|
|
|
|
private final static String AesDecKey = "47FC4124CF94E070";
|
|
|
|
|
@@ -46,7 +47,7 @@ public class PrivacyAesUtil {
|
|
|
System.out.print("Key长度不是16位");
|
|
|
return null;
|
|
|
}
|
|
|
- byte[] raw = sKey.getBytes("utf-8");
|
|
|
+ byte[] raw = sKey.getBytes(StandardCharsets.UTF_8);
|
|
|
SecretKeySpec keySpec = new SecretKeySpec(raw, "AES");
|
|
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//"算法/模式/补码方式"
|
|
|
IvParameterSpec iv = new IvParameterSpec(ivKey.getBytes());//使用CBC模式,需要一个向量iv,可增加加密算法的强度
|
|
@@ -180,7 +181,7 @@ public class PrivacyAesUtil {
|
|
|
resultMap.put(PrivacyAesUtil.RESULT, encryptSortNum(sourceStr));
|
|
|
return resultMap;
|
|
|
}
|
|
|
- String subStr = sourceStr.substring(sourceStr.length()-4,sourceStr.length());
|
|
|
+ String subStr = sourceStr.substring(sourceStr.length()-4);
|
|
|
subStr = subStr.replaceAll("(x|X)", "A").replaceAll("[^0-9aA]", "B");
|
|
|
|
|
|
key += subStr;
|
|
@@ -300,11 +301,11 @@ public class PrivacyAesUtil {
|
|
|
}
|
|
|
}
|
|
|
try {
|
|
|
- byte[] raw = sKey.getBytes("utf-8");
|
|
|
+ byte[] raw = sKey.getBytes(StandardCharsets.UTF_8);
|
|
|
SecretKeySpec skySpec = new SecretKeySpec(raw, "AES"); // "算法/模式/补码方式"
|
|
|
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
|
|
cipher.init(Cipher.ENCRYPT_MODE, skySpec);
|
|
|
- byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8")); // 此处使用 BASE64 做转码功能,同时能起到 2 次加密的作用。
|
|
|
+ byte[] encrypted = cipher.doFinal(sSrc.getBytes(StandardCharsets.UTF_8)); // 此处使用 BASE64 做转码功能,同时能起到 2 次加密的作用。
|
|
|
return Base64.encodeBase64String(encrypted);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
@@ -322,7 +323,7 @@ public class PrivacyAesUtil {
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
- byte[] raw = sKey.getBytes("utf-8");
|
|
|
+ byte[] raw = sKey.getBytes(StandardCharsets.UTF_8);
|
|
|
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); // "算法/模式/补码方式"
|
|
|
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
|
|
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
|