//加密算法
private static final String ENCRY_ALGORITHM = "AES";//加密算法/加密模式/填充类型
private static final String CIPHER_MODE = "AES/CBC/PKCS7Padding";//设置iv偏移量,ECB加密模式不需要设置 iv 偏移量
private static final String IV = "0000000000000000";//设置加密字符集
private static final String CHARACTER = "UTF-8";//加密密码长度。默认 16 byte * 8 = 128 bit
private static final int PWD_SIZE = 16;static{//添加 AES/CBC/PKCS7Padding 支持
Security.addProvider(newBouncyCastleProvider());
}public static voidmain(String[] args) {
String str= "NiHao";byte[] encryptAES = encryptAES(str, "1234567899874563");byte[] decryptAES = decryptAES(encryptAES, "1234567899874563");
System.out.println(newString(decryptAES));
}/*** 密码长度不足补"0"*/
private static byte[] pwdHandler(String password) throwsUnsupportedEncodingException {byte[] data = null;if (password == null) {
password= "";
}
StringBuffer sb= newStringBuffer(PWD_SIZE);
sb.append(password);while (sb.length()
sb.append("0");
}if (sb.length() >PWD_SIZE) {
sb.setLength(PWD_SIZE);
}
data=sb.toString().getBytes(CHARACTER);returndata;
}/*** AES 加密
*
*@paramcleartext 明文
*@paramkey 密钥
*@return
*/
public static byte[] encryptAES(String cleartext, String key) {try{//获取加密密钥
SecretKeySpec keySpec = newSecretKeySpec(pwdHandler(key), ENCRY_ALGORITHM);//获取Cipher实例
Cipher cipher =Cipher.getInstance(CIPHER_MODE);//查看数据块位数 默认为16(byte) * 8 =128 bit//System.out.println("数据块位数(byte):" + cipher.getBlockSize());//初始化Cipher实例。设置执行模式以及加密密钥
cipher.init(Cipher.ENCRYPT_MODE, keySpec, newIvParameterSpec(IV.getBytes(CHARACTER)));//执行
byte[] cipherTextBytes =cipher.doFinal(cleartext.getBytes(CHARACTER));returncipherTextBytes;
}catch(NoSuchPaddingException e) {
e.printStackTrace();
}catch(NoSuchAlgorithmException e) {
e.printStackTrace();
}catch(BadPaddingException e) {
e.printStackTrace();
}catch(IllegalBlockSizeException e) {
e.printStackTrace();
}catch(InvalidKeyException e) {
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}return null;
}/*** AES 解密
*
*@paramciphertext 密文
*@paramkey 密钥
*@return
*/
public static byte[] decryptAES(byte[] ciphertext, String key) {try{//获取解密密钥
SecretKeySpec keySpec = newSecretKeySpec(pwdHandler(key), ENCRY_ALGORITHM);//获取Cipher实例
Cipher cipher =Cipher.getInstance(CIPHER_MODE);//查看数据块位数 默认为16(byte) * 8 =128 bit//System.out.println("数据块位数(byte):" + cipher.getBlockSize());//初始化Cipher实例。设置执行模式以及加密密钥
cipher.init(Cipher.DECRYPT_MODE, keySpec, newIvParameterSpec(IV.getBytes(CHARACTER)));//执行
byte[] clearTextBytes =cipher.doFinal(ciphertext);returnclearTextBytes;
}catch(NoSuchAlgorithmException e) {
e.printStackTrace();
}catch(InvalidKeyException e) {
e.printStackTrace();
}catch(NoSuchPaddingException e) {
e.printStackTrace();
}catch(BadPaddingException e) {
e.printStackTrace();
}catch(IllegalBlockSizeException e) {
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}//解密错误 返回 null
return null;
}