需要配置AES_MODE(模式和填充)、AES_KEY(密钥)、AES_IV(偏移量)
至于是128还是192还是256,是根据密钥的长度自动确定的,且密钥长度只有三种:16、24、32
128的16位key,16位iv 192的24位key,16位iv 256的32位key,16位iv
测试地址:http://www.ssleye.com/aes_cipher.html
import android.util.Base64;
import android.util.Log;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* author:wangjinqiang
* date:2019/12/12
*/
public class SecurityAES {
private static final String TAG = "SecurityAES";
/**
* 模式/填充
*/
private final static String AES_MODE = "AES/CBC/PKCS5PADDING";
/**
* AES加密时的秘钥
* 128的16位key,16位iv
* 192的24位key,16位iv
* 256的32位key,16位iv
*/
private final static String AES_KEY = "01234567890123456789012345678901";
/**
* 偏移量,长度必须16位,多了少了都不行
*/
private final static String AES_IV = "0123456789012345";
/**
* AES专用秘钥
*/
private static SecretKeySpec key = new SecretKeySpec(AES_KEY.getBytes(), "AES");
static {
Log.e(TAG, "数据块长度: " + AES_KEY.getBytes().length * 8);
}
/**
* 加密文本
*
* @param content 明文
* @return 加密后Base64的字符串
*/
public static String encrypt(String content) {
try {
// 创建AES密码器
Cipher cipher = Cipher.getInstance(AES_MODE);
// 将文本进行utf-8的编码
byte[] byteContent = content.getBytes("utf-8");
//偏移量
byte[] iv = AES_IV.getBytes();
IvParameterSpec ivSpec = new IvParameterSpec(iv);
// 初始化AES密码器为加密器
cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
// 进行AES加密
byte[] result = cipher.doFinal(byteContent);
// 将byte数组转为Base64字符串
return Base64.encodeToString(result, Base64.NO_WRAP);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return null;
}
/**
* 解码文本
*
* @param encryptContent 密文(Base64)
* @return 明文
*/
public static String decrypt(String encryptContent) {
try {
byte[] content = Base64.decode(encryptContent, Base64.NO_WRAP);
// 创建AES密码器
Cipher cipher = Cipher.getInstance(AES_MODE);
//偏移量
byte[] iv = AES_IV.getBytes();
IvParameterSpec ivSpec = new IvParameterSpec(iv);
// 初始化AES密码器为解密器
cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);// 初始化
// 进行AES解密
byte[] result = cipher.doFinal(content);
// 将二进制转为字符串
return new String(result);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return null;
}
/**
* 将byte数组转换成16进制的字符串
*
* @param buf
* @return 16进制的字符串
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
//将每个字节都转成16进制的
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
//为保证格式统一,用两位16进制的表示一个字节
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/**
* 将16进制的字符串转换为byte数组
*
* @param hexStr
* @return byte数组
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
//两个16进制表示一个字节,所以字节数组大小为hexStr.length() / 2
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
//每次获取16进制字符串中的两个转成10进制(0-255)
int num = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 2), 16);
//将10进制强转为byte
result[i] = (byte) num;
}
return result;
}
}