本文介绍了如何使用Bouncy Castle生成对称密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用Bouncy Castle生成对称密钥? PrivateKeyFactory 和 PublicKeyFactory 似乎与 AsymmetricKeyParameter .
How can I generate a symmetric key with Bouncy Castle? Both PrivateKeyFactory and PublicKeyFactory seem related to AsymmetricKeyParameter.
我不想知道任何JCA/JCE API,相反,我只对Bouncy Castle特定的API感兴趣.
I don't want to know any JCA/JCE API - instead I'm only interested in Bouncy Castle specific API.
(可以)我可以生成一个随机字节吗?
Can (should) I just generate a random bytes?
推荐答案
AES没有任何弱密钥,因此直接生成随机数应该没问题.
AES does not have any weak keys, so a straightforward random generation should be fine.
// SecureRandom is expensive to initialize (takes several milliseconds) –
// consider keeping the instance around if you are generating many keys.
SecureRandom random = new SecureRandom();
byte[] keyBytes = new byte[16];
random.nextBytes(keyBytes);
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
这篇关于如何使用Bouncy Castle生成对称密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!