如何使用Bouncy Castle生成对称密钥?

时间:2013-07-23 01:29:39

标签: java cryptography key aes bouncycastle

如何使用Bouncy Castle生成对称密钥?  PrivateKeyFactoryPublicKeyFactory似乎都与AsymmetricKeyParameter相关。

我不想知道任何JCA / JCE API - 相反,我只对Bouncy Castle特定的API感兴趣。

可以(应该)我只生成一个随机字节吗?

1 个答案:

答案 0 :(得分:8)

AES没有任何弱键,所以直接随机生成应该没问题。

// 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");