本文介绍了更好的方式来创建AES密钥,比种子SecureRandom的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 c>SHA1PRNG未实现时,

  • SecureRandom 即使从一个Sun JDK到另一个;

  • Oracle提供的实现SHA1PRNG使用初始种子作为种子,其他人可以将种子添加到随机池。

    1. SecureRandom's main function is to generate random values, it should not be used as a generator for a key stream;
    2. SecureRandom, when instantiated with "SHA1PRNG" does not implement a well defined algorithm, and the algorithm has actually be known to change, even from one Sun JDK to another;
    3. The Oracle provided implementation of "SHA1PRNG" uses the initial seed as only seed, others may just add the seed to the random pool.

    使用SHA1PRNG作为密钥导出函数已知会在Android的多个版本上产生问题,并且可能在任何其他Java RE 。

    Using "SHA1PRNG" as key derivation function has been known to produce issues on several versions of Android, and may fail on any other Java RE.

    那么你应该怎么办呢?


    1. 使用 new SecureRandom()或更好,生成一个真正随机的密钥,而不需要随机数生成器一个全新的随机键;

    2. 直接提供 SecretKeySpec 的已知键的 byte [] code>,或使用十六进制解码器从十六进制解码它(注意, String 实例很难从内存中删除,所以只有这样做,如果没有其他方式);

    3. 如果要从密码创建密钥,请使用 (使用比链接中提供的更高的迭代计数);

    4. 如果要从一个键种子创建多个键,请使用真正的基于键的键派生机制,例如使用HKDF(见下文)。

    1. Use new SecureRandom() or even better, KeyGenerator to generate a truly random key, without seeding the random number generator if you need a brand new random key;
    2. Directly provide a byte[] of a known key to SecretKeySpec, or use a hexadecimal decoder to decode it from hexadecimals (note that String instances are hard to delete from memory, so only do this if there is no other way);
    3. Use PBKDF2 if you want to create a key from a password (use a higher iteration count than the one provided in the link though);
    4. Use a true Key Based Key Derivation Mechanism if you want to create multiple keys from one key seed, e.g. use HKDF (see below).

    如果种子是由密钥协议算法,如Diffie-Hellman或ECDH。

    Option 4 would be preferred if the seed was generated by e.g. a key agreement algorithm such as Diffie-Hellman or ECDH.

    请注意,对于选项3,PBKDF2,只保留ASCII密码。这是因为Oracle的PBKDF2实现不使用UTF-8编码。

    Note that for option 3, PBKDF2, you would be wise to keep to ASCII passwords only. This is due to the fact that the PBKDF2 implementation by Oracle does not use UTF-8 encoding.

    对于选项4 ,我已经帮助将所有好的KBKDF添加到,所以没有如果您可以将Bouncy Castle添加到类路径和/或已安装的安全提供程序列表,则需要自行实施KBKDF。可能目前最好的KBKDF是HKDF。如果你不能添加Bouncy城​​堡到你的类路径,那么你可能想使用SHA-256输出的最左边的字节作为穷人的KDF的派生数据。

    As for option 4, I've helped with adding all good KBKDF's to the Bouncy Castle libraries so there isn't a need to implement a KBKDF yourself if you can add Bouncy Castle to your classpath and/or list of installed security providers. Probably the best KBKDF at the moment is HKDF. If you cannot add Bouncy Castle to your classpath then you might want to use the leftmost bytes of SHA-256 output over the derivation data as a "poor man's" KDF.

    这篇关于更好的方式来创建AES密钥,比种子SecureRandom的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-23 14:41