我正在为安卓的PBE实现和AES加密引擎,我已经找到了两种方法来实现IV的创建,我想知道哪种方法更好,更安全,可以获得IvParameterSpec
方法#1:
SecureRandom randomSecureRandom = SecureRandom.getInstance("SHA1PRNG");
byte[] iv = new byte[cipher.getBlockSize()];
randomSecureRandom.nextBytes(iv);
IvParameterSpec ivParams = new IvParameterSpec(iv);
方法#2:
AlgorithmParameters params = cipher.getParameters();
byte[] iv2 = params.getParameterSpec(IvParameterSpec.class).getIV();
ivParams = new IvParameterSpec(iv2);
发布于 2015-03-26 06:39:46
我使用方法#1,因为Java API为只接受加密/解密模式和密钥的Cipher.init()
API指定了以下内容:
如果此密码实例需要指定密钥无法提供的任何算法参数或随机值,则此密码的底层实现将生成所需的参数(使用其提供程序或随机值)。
(强调我的)。
will 不会创建一个随机的IV -随机的IV创建似乎被注释掉了。
方法1也更透明,而且-在我看来-更容易看起来。
请注意,使用new SecureRandom()
通常更好,让系统找出哪个RNG是最好的。"SHA1PRNG"
没有很好的定义,在不同的实现中可能会有所不同,并且众所周知存在实现上的弱点,特别是在安卓上。
因此,最终结果应该类似于:
SecureRandom randomSecureRandom = new SecureRandom();
byte[] iv = new byte[cipher.getBlockSize()];
randomSecureRandom.nextBytes(iv);
IvParameterSpec ivParams = new IvParameterSpec(iv);
https://stackoverflow.com/questions/29267435
复制相似问题