安卓AES加密使用192位密钥

3

我想使用一个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 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());
}

但加密并不是真正的。而且每次数组的内容都不同。为什么?


2
“not true” 是什么意思? - Heiko Rupp
1
你如何初始化你的KeyGenerator? - olshevski
“not true”是什么意思? 加密结果与C++中的结果不同。你如何初始化你的KeyGenerator? 我没有生成密钥。 - Александр Мартынцев
1个回答

1

您正在使用CBC模式,该模式需要初始化向量(IV)。由于您没有显式设置IV,因此每次加密时都会生成一个随机的IV。您必须执行以下操作之一:

  • 使用静态IV(不建议),或
  • 将IV与密码文本一起发送到C++程序

以下是如何在Java中设置IV,有关您正在使用的库的C++文档,请参阅文档:

 byte[] iv = generateIv(cipher.getBlockSize());
 IvParameterSpec ivParams = new IvParameterSpec(iv);
 cipher.init(Cipher.ENCRYPT_MODE, key, ivParams);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接