Java 中的 Rijndael 支持
我们需要用 Java 进行一些 Rijndael 开发.
We have a requirement to do some Rijndael development in Java.
对我们有帮助的文章、图书馆等有什么建议吗?
Any recommendations for articles, libraries etc. that would help us?
任何指向密钥库维护以及如何安全存储密钥的指针?
Any pointers to keystore maintenance and how store the keys securely?
它需要是开源的.本质上,它只是使用 Rijndael 对数据进行标准加密/解密.
It would need to be open source. Essentially, it's just standard encrypt / decrypt of data using Rijndael.
推荐答案
Java 包含开箱即用的 AES.Rijndael 是 AES.您不需要任何外部库.你只需要这样的东西:
Java includes AES out of the box. Rijndael is AES. You don't need any external libraries. You just need something like this:
byte[] sessionKey = null; //Where you get this from is beyond the scope of this post
byte[] iv = null ; //Ditto
byte[] plaintext = null; //Whatever you want to encrypt/decrypt
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//You can use ENCRYPT_MODE or DECRYPT_MODE
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(sessionKey, "AES"), new IvParameterSpec(iv));
byte[] ciphertext = cipher.doFinal(plaintext);
就是这样,用于加密/解密.如果您正在处理大量数据,那么您最好读取 16 字节的倍数的块并调用 update 而不是 doFinal(您只需在最后一个块上调用 doFinal).
And that's it, for encryption/decryption. If you are processing large amounts of data then you're better off reading chunks that are multiples of 16 bytes and calling update instead of doFinal (you just call doFinal on the last block).
相关文章