Android 安卓AES加密192bit密钥
我想用192位的密钥加密数据Android 安卓AES加密192bit密钥,android,aes,Android,Aes,我想用192位的密钥加密数据 SecretKeySpec key = new SecretKeySpec(new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, "AES/CBC/NoPadding"); byte[] data = new b
SecretKeySpec key = new SecretKeySpec(new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, "AES/CBC/NoPadding");
byte[] data = new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
byte[] encrypted = null;
try {
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key);
encrypted = cipher.doFinal(data);
} catch (Exception e) {
System.out.println(e.getMessage());
}
但加密不是真的。而且每次数组的内容都是不同的。为什么?您正在使用CBC模式,这需要一个初始化向量(IV)。由于没有显式设置,因此每次加密时都会生成一个随机值。你必须:
- 使用静电IV(不推荐),或
- 将IV和密码文本一起发送到C++程序
这里是如何设置java中的IV,C++用于引用你正在使用的库的文档:
byte[] iv = generateIv(cipher.getBlockSize());
IvParameterSpec ivParams = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, key, ivParams);
“不真实”是什么意思?你如何输入你的密钥生成器?>你的意思是“不正确”的加密结果与C++中的结果不同。你如何输入你的密钥生成器?我没有生成密钥。