如何使用Bouncy Castle生成对称密钥? PrivateKeyFactory和PublicKeyFactory似乎都与AsymmetricKeyParameter相关。
我不想知道任何JCA / JCE API - 相反,我只对Bouncy Castle特定的API感兴趣。
可以(应该)我只生成一个随机字节吗?
答案 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");