public class AESUtils {
public static String aesDecrypt(String encryptStr, String secretKey) throws Exception {
byte[] decodeBytes = new BASE64Decoder().decodeBuffer(encryptStr);
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(secretKey.getBytes());
keyGenerator.init(128, random);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyGenerator.generateKey().getEncoded(), "AES"));
byte[] decryptBytes = cipher.doFinal(decodeBytes);
return new String(decryptBytes);
}
public static String aesEncrypt(String originStr, String secretKey) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(secretKey.getBytes());
keyGenerator.init(128, random);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyGenerator.generateKey().getEncoded(), "AES"));
byte[] encodeBytes = cipher.doFinal(originStr.getBytes("utf-8"));
String encryptStr = new BASE64Encoder().encode(encodeBytes);
return encryptStr;
}
}