Commit 03e9ccd0 authored by 黎博's avatar 黎博

暂时提交

parent ddf60d4f
...@@ -30,37 +30,37 @@ public class AESUtil { ...@@ -30,37 +30,37 @@ public class AESUtil {
// 加密 // 加密
public static String encryptToBase64(String sSrc, String sKey) throws Exception { public static String encryptToBase64(String sSrc, String sKey) throws Exception {
if (sKey == null) { if (sKey == null) {
System.out.print("Key为空null"); // System.out.print("Key为空null");
return null; return null;
} }
// 判断Key是否为16位 // 判断Key是否为16位
if (sKey.length() != 16) { if (sKey.length() != 16) {
System.out.println(sKey); // System.out.println(sKey);
System.out.print("Key长度不是16位"); // System.out.print("Key长度不是16位");
return null; return null;
} }
byte[] raw = sKey.getBytes(ConfigureEncryptAndDecrypt.CHAR_ENCODING); byte[] raw = sKey.getBytes();
for (byte b : raw) { for (byte b : raw) {
System.out.println("======" + b); // System.out.println("======" + b);
} }
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance(ConfigureEncryptAndDecrypt.AES_ALGORITHM); Cipher cipher = Cipher.getInstance(ConfigureEncryptAndDecrypt.AES_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec); cipher.init(Cipher.ENCRYPT_MODE, skeySpec,new IvParameterSpec(raw));
byte[] encrypted = cipher.doFinal(sSrc.getBytes(ConfigureEncryptAndDecrypt.CHAR_ENCODING)); byte[] encrypted = cipher.doFinal(sSrc.getBytes());
// 此处使用BASE64做转码功能,同时能起到2次加密的作用。 // 此处使用BASE64做转码功能,同时能起到2次加密的作用。
return new String(Base64.getEncoder().encode(encrypted), ConfigureEncryptAndDecrypt.CHAR_ENCODING); return new String(Base64.getEncoder().encode(encrypted));
} }
// 解密 // 解密
public static String decryptFromBase64(String sSrc, String sKey) throws Exception { public static String decryptFromBase64(String sSrc, String sKey) throws Exception {
// 判断Key是否正确 // 判断Key是否正确
if (sKey == null) { if (sKey == null) {
System.out.print("Key为空null"); // System.out.print("Key为空null");
return null; return null;
} }
// 判断Key是否为16位 // 判断Key是否为16位
if (sKey.length() != 16) { if (sKey.length() != 16) {
System.out.print("Key长度不是16位"); // System.out.print("Key长度不是16位");
return null; return null;
} }
// byte[] raw = sKey.getBytes(ConfigureEncryptAndDecrypt.CHAR_ENCODING); // byte[] raw = sKey.getBytes(ConfigureEncryptAndDecrypt.CHAR_ENCODING);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment