前言
做AES加密的时候本来想着直接把后台给的AES代码copy过来发现有个很奇葩的问题:我直接写main方法运行解密是OK的然后app运行却不可以每次秘文都是改变的。各种百度也不行好像是jvm什么的不兼容安卓这边,于是后台大佬做了下兼容发给我了。亲测可用。
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class AESCoder {
private static final String KEY_ALGORITHM = "AES";
// 16 bytes IV
static String initVector = "kUs7PF3m8NfVDEOa";
/**
* 加解密
*
* @param content 待处理数据
* @param salt 密钥
* @param mode 加解密mode
* @param isCheck 是否抛出异常
* @return
*/
public static byte[] doAES(String content, String salt, int mode, boolean isCheck) {
try {
//判断是加密还是解密
boolean encrypt = mode == Cipher.ENCRYPT_MODE;
byte[] byteContent;
//true 加密内容 false 解密内容
if (encrypt) {
byteContent = content.getBytes("utf-8");
} else {
byteContent = parseHexStr2Byte(content);
}
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
SecretKeySpec key = new SecretKeySpec(salt.getBytes("utf-8"), KEY_ALGORITHM);
// 创建密码器
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
// 初始化
cipher.init(mode, key, iv);
// 加密
byte[] result = cipher.doFinal(byteContent);
return result;
} catch (Exception e) {
if (!isCheck) {
// log.info("消息加解密失败!", e);
// throw new CapturedException(ResponseStatus.PASSWORD_DECRYPT_FAILED.getCode(), ResponseStatus.PASSWORD_DECRYPT_FAILED.getMessage());
}
//当是检查是否是aes加密的场景下,直接抛出异常,不打印日志
throw new RuntimeException();
}
}
/**
* 函数名称 : parseByte2HexStr
* 功能描述 :
* 参数及返回值说明:
*
* @param buf
* @return 修改记录:
* 日期 :2019年4月09日 下午13:25:04
* 描述 :将二进制转换成16进制
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/**
* 函数名称 : parseHexStr2Byte
* 功能描述 :
* 参数及返回值说明:
*
* @param hexStr
* @return 修改记录:
* 日期 :2019年4月09日 下午13:19:45
* 描述 :将16进制转换为二进制
*/
public static byte[] parseHexStr2Byte(String hexStr) {
try {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 函数名称 : getEncryptResult
* 功能描述 :
* 参数及返回值说明:
*
* @param content
* @param passKey
* @return 修改记录:
* 日期 :2019年4月09日 下午13:24:19
* 描述 :
*/
public static String getEncryptResult(String content, String passKey) {
return parseByte2HexStr(doAES(content, passKey, Cipher.ENCRYPT_MODE, false));
}
/**
* 函数名称 : getDecryptResult
* 功能描述 :
* 参数及返回值说明:
*
* @param content
* @param passKey
* @return 修改记录:
* 日期 :2019年4月09日 下午14:24:26
* 描述 :
*/
public static String getDecryptResult(String content, String passKey) {
String str = new String(doAES(content, passKey, Cipher.DECRYPT_MODE, false));
return str;
}
}
唯一需要注意的是: // 16 bytes IV
static String initVector = “qwd7PF3m8NfVDasd”;这个必须是16位的
同时密钥也必须是16位。具体原因也不知道,先占个楼后面看看为啥