java实现AES加密解密
public class EncryptionUtil {
//AES秘钥(长度必须为16)
private static final String key = "8347670AIM831234";
//AES 偏移量(长度必须为16)
private static final String iv = "OSU97XS6AQ641234";
/**
* AES加密
*
* @param password 待加密的密码
* @return
*/
public static String encrypt(String password) {
try {
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");//名称/类型/填充方式
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
byte[] encrypted = cipher.doFinal(password.getBytes());
return Base64.encodeBase64String(encrypted);
} catch (Exception ex) {
log.error("AES encrypt error {}",ex.getMessage());
}
return "";
}
/**
* AES解密
*
* @param encryptedPassword 待解密密码
* @return
*/
public static String decrypt(String encryptedPassword) {
try {
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");//名称/类型/填充方式
cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec);
byte[] original = cipher.doFinal(Base64.decodeBase64(encryptedPassword));
return new String(original);
} catch (Exception ex) {
log.error("AES decrypt error {}",ex.getMessage());
}
return "";
}
}