package com.utils;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class AESUtils {
private static String aesKey = "SALENUMBERAESMSG";
/**
* AES算法128位加密
*
* @param cleartext
* @return
*/
public static String encrypt(String cleartext) {
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKeySpec skeySpec = new SecretKeySpec(aesKey.getBytes("UTF-8"), "AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(cleartext.getBytes("UTF-8"));
return Base64.getEncoder().encodeToString(encrypted);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* AES算法128位解密
*
* @param encrypMsg
* @return
*/
public static String decrypt(String encrypMsg) {
try {
Cipher cipher = Cipher.getInstance("AES");
SecretKeySpec keySpec = new SecretKeySpec(aesKey.getBytes(), "AES");
cipher.init(Cipher.DECRYPT_MODE, keySpec);
byte[] decbbdt = cipher.doFinal(Base64.getDecoder().decode(encrypMsg));
return new String(decbbdt, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}