JAVA工具【四】AESUtils加密工具
程序员文章站
2024-03-14 14:33:52
...
1、工具实现
public class AESCipher {
private static final String AES_NAME = "AES";
private static final String CHARSET = "UTF-8";
private static final String SECURE_RANDOM_NAME = "SHA1PRNG";
private AESBit aesBit;
private AESMode aesMode;
private AESPadding aesPadding;
private String cipherInstanceName;
private AESCipher() {
}
private AESCipher(AESBit aesBit, AESMode aesMode, AESPadding aesPadding) {
this.aesBit = aesBit;
this.aesMode = aesMode;
this.aesPadding = aesPadding;
this.cipherInstanceName = AES_NAME + "/" + this.aesMode.name() + "/" + this.aesPadding.name();
}
private AESCipher(AESBit aesBit) {
this.aesBit = aesBit;
this.aesMode = null;
this.aesPadding = null;
this.cipherInstanceName = AES_NAME;
}
public static AESCipher getInstance(AESBit aesBit, AESMode aesMode, AESPadding aesPadding) {
return new AESCipher(aesBit, aesMode, aesPadding);
}
public static AESCipher getDefaultInstance(AESBit aesBit) {
return new AESCipher(aesBit);
}
// ===============================加密 begin
public byte[] encrypt(String source, String key, byte[] iv) throws Exception {
return this.encrypt(source.getBytes(CHARSET), key, iv);
}
public byte[] encrypt(byte[] source, String key, byte[] iv) throws Exception {
byte[] encodeByte = encryptOrDecrypt(Cipher.ENCRYPT_MODE, source, key, iv);
return encodeByte;
}
public String encryptToHexString(byte[] source, String key, byte[] iv) throws Exception {
byte[] encodeByte = this.encrypt(source, key, iv);
return HexUtils.toHexString(encodeByte);
}
public String encryptToHexString(String source, String key, byte[] iv) throws Exception {
byte[] encodeByte = this.encrypt(source, key, iv);
return HexUtils.toHexString(encodeByte);
}
public String encryptToBase64String(String source, String key, byte[] iv) throws Exception {
byte[] encodeByte = this.encrypt(source, key, iv);
return Base64.getEncoder().encodeToString(encodeByte);
}
// ===============================加密 end
// ===============================解密 begin
/**
* 解密
* @param source
* @param key
* @param iv
* @return
* @throws Exception
*/
public byte[] decrypt(byte[] source, String key, byte[] iv) throws Exception {
byte[] decodeByte = encryptOrDecrypt(Cipher.DECRYPT_MODE, source, key, iv);
return decodeByte;
}
/**
*
* @param source 16进制
* @param key
* @param iv
* @return
* @throws Exception
*/
public byte[] decryptHex(String source, String key, byte[] iv) throws Exception {
return this.decrypt(HexUtils.fromHexString(source), key, iv);
}
public byte[] decryptBase64(String source, String key, byte[] iv) throws Exception {
return this.decrypt(Base64.getDecoder().decode(source), key, iv);
}
public String decryptToString(byte[] source, String key, byte[] iv) throws Exception {
byte[] decodeByte = decrypt(source, key, iv);
String decodeStr = new String(decodeByte, CHARSET);
return decodeStr;
}
/**
* @param source 16进制
* @param key
* @param iv
* @return
* @throws Exception
*/
public String decryptHexToString(String source, String key, byte[] iv) throws Exception {
byte[] decodeByte = decryptHex(source, key, iv);
String decodeStr = new String(decodeByte, CHARSET);
return decodeStr;
}
public String decryptBase64ToString(String source, String key, byte[] iv) throws Exception {
byte[] decodeByte = decryptBase64(source, key, iv);
String decodeStr = new String(decodeByte, CHARSET);
return decodeStr;
}
// ===============================解密 end
private byte[] encryptOrDecrypt(int mode, byte[] byteContent, String key, byte[] iv) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(AES_NAME);
//此处解决mac,linux报错
SecureRandom random = SecureRandom.getInstance(SECURE_RANDOM_NAME);
random.setSeed(key.getBytes());
keyGenerator.init(this.aesBit.getValue(), random);
SecretKey secretKey = keyGenerator.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec keySpec = new SecretKeySpec(enCodeFormat, AES_NAME);
Cipher cipher = Cipher.getInstance(this.cipherInstanceName);// 创建密码器
if (null != iv) {
//指定一个初始化向量 (Initialization vector,IV), IV 必须是16位
cipher.init(mode, keySpec, new IvParameterSpec(iv));
} else {
cipher.init(mode, keySpec);
}
byte[] result = cipher.doFinal(byteContent);
return result;
}
}
public class AESTypeConvert {
/**
* 字符串转换成十六进制字符串
*/
public static String str2HexStr(String str) {
char[] chars = "0123456789ABCDEF".toCharArray();
StringBuilder sb = new StringBuilder("");
byte[] bs = str.getBytes();
int bit;
for (int i = 0; i < bs.length; i++) {
bit = (bs[i] & 0x0f0) >> 4;
sb.append(chars[bit]);
bit = bs[i] & 0x0f;
sb.append(chars[bit]);
}
return sb.toString();
}
/**
* Convert hex string to byte[]
*
* @param hexString the hex string
* @return byte[]
*/
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
/**
* Convert char to byte
*
* @param c char
* @return byte
*/
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
/**
* 数组转换成十六进制字符串
* @param bArray byte[]
* @return HexString
*/
public static final String bytesToHexString(byte[] bArray) {
if (bArray == null || bArray.length==0){
return null;
}
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i < bArray.length; i++) {
sTemp = Integer.toHexString(0xFF & bArray[i]);
if (sTemp.length() < 2){
sb.append(0);
}
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}
/**
* 十六进制字符串转换成字符串
* @param hexStr
* @return String
*/
public static String hexStr2Str(String hexStr) {
String str = "0123456789ABCDEF";
char[] hexs = hexStr.toCharArray();
byte[] bytes = new byte[hexStr.length() / 2];
int n;
for (int i = 0; i < bytes.length; i++) {
n = str.indexOf(hexs[2 * i]) * 16;
n += str.indexOf(hexs[2 * i + 1]);
bytes[i] = (byte) (n & 0xff);
}
return new String(bytes);
}
/**
* @param hexString String str = "000AB"
* @return
*/
public static int hexString2Int(String hexString){
Integer num = Integer.valueOf(hexString,16);
return num;
}
/**
* 把byte转为字符串的bit
*/
public static String byteToBitString(byte b) {
return ""
+ (byte) ((b >> 7) & 0x1) + (byte) ((b >> 6) & 0x1)
+ (byte) ((b >> 5) & 0x1) + (byte) ((b >> 4) & 0x1)
+ (byte) ((b >> 3) & 0x1) + (byte) ((b >> 2) & 0x1)
+ (byte) ((b >> 1) & 0x1) + (byte) ((b >> 0) & 0x1);
}
/**
* 把byte转为字符串数组的bit
*/
public static String[] byteToBitStrings(byte b) {
String[] bit = new String[8];
bit[0] = ""+ (byte) ((b >> 7) & 0x1);
bit[1] = ""+ (byte) ((b >> 6) & 0x1);
bit[2] = ""+ (byte) ((b >> 5) & 0x1);
bit[3] = ""+ (byte) ((b >> 4) & 0x1);
bit[4] = ""+ (byte) ((b >> 3) & 0x1);
bit[5] = ""+ (byte) ((b >> 2) & 0x1);
bit[6] = ""+ (byte) ((b >> 1) & 0x1);
bit[7] = ""+ (byte) ((b >> 0) & 0x1);
return bit;
}
//base64字符串转byte[]
public static byte[] base64String2ByteFun(String base64Str){
return Base64.decodeBase64(base64Str);
}
//byte[]转base64
public static String byte2Base64StringFun(byte[] b){
return Base64.encodeBase64String(b);
}
}
public class AESUtils {
public static final AESCipher AES_DEFAULT_128 = AESCipher.getDefaultInstance(AESBit.AES_128);
public static final AESCipher AES_DEFAULT_192 = AESCipher.getDefaultInstance(AESBit.AES_192);
public static final AESCipher AES_DEFAULT_256 = AESCipher.getDefaultInstance(AESBit.AES_256);
public static final AESCipher AES128_CBC_NoPadding = AESCipher.getInstance(AESBit.AES_128, AESMode.CBC, AESPadding.NoPadding);
public static final AESCipher AES128_CBC_PKCS5Padding = AESCipher.getInstance(AESBit.AES_128, AESMode.CBC, AESPadding.PKCS5Padding);
public static final AESCipher AES128_CBC_ISO10126Padding = AESCipher.getInstance(AESBit.AES_128, AESMode.CBC, AESPadding.ISO10126Padding);
public static final AESCipher AES128_CFB_NoPadding = AESCipher.getInstance(AESBit.AES_128, AESMode.CFB, AESPadding.NoPadding);
public static final AESCipher AES128_CFB_PKCS5Padding = AESCipher.getInstance(AESBit.AES_128, AESMode.CFB, AESPadding.PKCS5Padding);
public static final AESCipher AES128_CFB_ISO10126Padding = AESCipher.getInstance(AESBit.AES_128, AESMode.CFB, AESPadding.ISO10126Padding);
public static final AESCipher AES128_ECB_NoPadding = AESCipher.getInstance(AESBit.AES_128, AESMode.ECB, AESPadding.NoPadding);
public static final AESCipher AES128_ECB_PKCS5Padding = AESCipher.getInstance(AESBit.AES_128, AESMode.ECB, AESPadding.PKCS5Padding);
public static final AESCipher AES128_ECB_ISO10126Padding = AESCipher.getInstance(AESBit.AES_128, AESMode.ECB, AESPadding.ISO10126Padding);
public static final AESCipher AES128_OFB_NoPadding = AESCipher.getInstance(AESBit.AES_128, AESMode.OFB, AESPadding.NoPadding);
public static final AESCipher AES128_OFB_PKCS5Padding = AESCipher.getInstance(AESBit.AES_128, AESMode.OFB, AESPadding.PKCS5Padding);
public static final AESCipher AES128_OFB_ISO10126Padding = AESCipher.getInstance(AESBit.AES_128, AESMode.OFB, AESPadding.ISO10126Padding);
public static final AESCipher AES128_PCBC_NoPadding = AESCipher.getInstance(AESBit.AES_128, AESMode.PCBC, AESPadding.NoPadding);
public static final AESCipher AES128_PCBC_PKCS5Padding = AESCipher.getInstance(AESBit.AES_128, AESMode.PCBC, AESPadding.PKCS5Padding);
public static final AESCipher AES128_PCBC_ISO10126Padding = AESCipher.getInstance(AESBit.AES_128, AESMode.PCBC, AESPadding.ISO10126Padding);
public static final AESCipher AES192_CBC_NoPadding = AESCipher.getInstance(AESBit.AES_192, AESMode.CBC, AESPadding.NoPadding);
public static final AESCipher AES192_CBC_PKCS5Padding = AESCipher.getInstance(AESBit.AES_192, AESMode.CBC, AESPadding.PKCS5Padding);
public static final AESCipher AES192_CBC_ISO10126Padding = AESCipher.getInstance(AESBit.AES_192, AESMode.CBC, AESPadding.ISO10126Padding);
public static final AESCipher AES192_CFB_NoPadding = AESCipher.getInstance(AESBit.AES_192, AESMode.CFB, AESPadding.NoPadding);
public static final AESCipher AES192_CFB_PKCS5Padding = AESCipher.getInstance(AESBit.AES_192, AESMode.CFB, AESPadding.PKCS5Padding);
public static final AESCipher AES192_CFB_ISO10126Padding = AESCipher.getInstance(AESBit.AES_192, AESMode.CFB, AESPadding.ISO10126Padding);
public static final AESCipher AES192_ECB_NoPadding = AESCipher.getInstance(AESBit.AES_192, AESMode.ECB, AESPadding.NoPadding);
public static final AESCipher AES192_ECB_PKCS5Padding = AESCipher.getInstance(AESBit.AES_192, AESMode.ECB, AESPadding.PKCS5Padding);
public static final AESCipher AES192_ECB_ISO10126Padding = AESCipher.getInstance(AESBit.AES_192, AESMode.ECB, AESPadding.ISO10126Padding);
public static final AESCipher AES192_OFB_NoPadding = AESCipher.getInstance(AESBit.AES_192, AESMode.OFB, AESPadding.NoPadding);
public static final AESCipher AES192_OFB_PKCS5Padding = AESCipher.getInstance(AESBit.AES_192, AESMode.OFB, AESPadding.PKCS5Padding);
public static final AESCipher AES192_OFB_ISO10126Padding = AESCipher.getInstance(AESBit.AES_192, AESMode.OFB, AESPadding.ISO10126Padding);
public static final AESCipher AES192_PCBC_NoPadding = AESCipher.getInstance(AESBit.AES_192, AESMode.PCBC, AESPadding.NoPadding);
public static final AESCipher AES192_PCBC_PKCS5Padding = AESCipher.getInstance(AESBit.AES_192, AESMode.PCBC, AESPadding.PKCS5Padding);
public static final AESCipher AES192_PCBC_ISO10126Padding = AESCipher.getInstance(AESBit.AES_192, AESMode.PCBC, AESPadding.ISO10126Padding);
public static final AESCipher AES256_CBC_NoPadding = AESCipher.getInstance(AESBit.AES_256, AESMode.CBC, AESPadding.NoPadding);
public static final AESCipher AES256_CBC_PKCS5Padding = AESCipher.getInstance(AESBit.AES_256, AESMode.CBC, AESPadding.PKCS5Padding);
public static final AESCipher AES256_CBC_ISO10126Padding = AESCipher.getInstance(AESBit.AES_256, AESMode.CBC, AESPadding.ISO10126Padding);
public static final AESCipher AES256_CFB_NoPadding = AESCipher.getInstance(AESBit.AES_256, AESMode.CFB, AESPadding.NoPadding);
public static final AESCipher AES256_CFB_PKCS5Padding = AESCipher.getInstance(AESBit.AES_256, AESMode.CFB, AESPadding.PKCS5Padding);
public static final AESCipher AES256_CFB_ISO10126Padding = AESCipher.getInstance(AESBit.AES_256, AESMode.CFB, AESPadding.ISO10126Padding);
public static final AESCipher AES256_ECB_NoPadding = AESCipher.getInstance(AESBit.AES_256, AESMode.ECB, AESPadding.NoPadding);
public static final AESCipher AES256_ECB_PKCS5Padding = AESCipher.getInstance(AESBit.AES_256, AESMode.ECB, AESPadding.PKCS5Padding);
public static final AESCipher AES256_ECB_ISO10126Padding = AESCipher.getInstance(AESBit.AES_256, AESMode.ECB, AESPadding.ISO10126Padding);
public static final AESCipher AES256_OFB_NoPadding = AESCipher.getInstance(AESBit.AES_256, AESMode.OFB, AESPadding.NoPadding);
public static final AESCipher AES256_OFB_PKCS5Padding = AESCipher.getInstance(AESBit.AES_256, AESMode.OFB, AESPadding.PKCS5Padding);
public static final AESCipher AES256_OFB_ISO10126Padding = AESCipher.getInstance(AESBit.AES_256, AESMode.OFB, AESPadding.ISO10126Padding);
public static final AESCipher AES256_PCBC_NoPadding = AESCipher.getInstance(AESBit.AES_256, AESMode.PCBC, AESPadding.NoPadding);
public static final AESCipher AES256_PCBC_PKCS5Padding = AESCipher.getInstance(AESBit.AES_256, AESMode.PCBC, AESPadding.PKCS5Padding);
public static final AESCipher AES256_PCBC_ISO10126Padding = AESCipher.getInstance(AESBit.AES_256, AESMode.PCBC, AESPadding.ISO10126Padding);
}
2、应用
public String encryptAES(String text, String secret, int radix, int bit) throws Exception {
switch (radix) {
case 16:
if (bit == 128) {
return AESUtils.AES_DEFAULT_128.encryptToHexString(text, secret, null);
} else if (bit == 192) {
return AESUtils.AES_DEFAULT_192.encryptToHexString(text, secret, null);
} else if (bit == 256) {
return AESUtils.AES_DEFAULT_256.encryptToHexString(text, secret, null);
}
case 64:
if (bit == 128) {
return AESUtils.AES_DEFAULT_128.encryptToBase64String(text, secret, null);
} else if (bit == 192) {
return AESUtils.AES_DEFAULT_192.encryptToBase64String(text, secret, null);
} else if (bit == 256) {
return AESUtils.AES_DEFAULT_256.encryptToBase64String(text, secret, null);
}
}
return null;
}
上一篇: Base16 应用与原理解析
下一篇: AES