代码示例如下:
public class AESUtils {
// private static final Logger logger = LoggerFactory.getLogger(AESUtils.class);
public final static String CBC = "AES/CBC/NoPadding";//无填充模式加解密
/**
* 解密
* @param content
* @param key
* @param iv
* @return
* @throws Exception
*/
public static byte[] decrypt(byte[] content, byte[] iv, byte[] key) throws Exception {
Cipher cipher = Cipher.getInstance(CBC);
SecretKeySpec skeySpec = new SecretKeySpec(key,"AES");
IvParameterSpec ivPs = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivPs);
return cipher.doFinal(content);
}
/**
* 加密
* @param content
* @param strKey
* @param iv
* @return
* @throws Exception
*/
public static byte[] encrypt(byte[] content, byte[] iv, byte[] strKey) throws Exception {
Cipher cipher = Cipher.getInstance(CBC);
SecretKeySpec skeySpec = new SecretKeySpec(strKey, "AES");
IvParameterSpec ivPs = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivPs);
return cipher.doFinal(content);
}
}