Rijndael support in Java

前端 未结 4 1217
隐瞒了意图╮
隐瞒了意图╮ 2020-12-08 11:48

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

4条回答
  •  孤城傲影
    2020-12-08 11:53

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

    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).

提交回复
热议问题