Create Chained Operations
To create a chained encryption operation:
Set the IVs you'll need for the encryption.
Obtain an instance of a chained cipher.
Create a ChainedOperations object.
Use
addEncrypt()to add the encryption operations to theChainedOperationsobject. Specify the key name, padding, andIvParameterSpecobject.Use the
seal()method to seal theChainedOperationsobject.Initialize the Cipher object, pass the
ChainedOperationsobject as a parameter. For all chained operations, the first argument ofcipher.initis ignored.Perform the operations by calling the
doFinal()method on the Cipher object. Pass the bytes of data you want to encrypt.
Code sample
The following code sample demonstrates how to encrypt Hello World! with mykey1 and then encrypt the result with mykey2.
byte[] iv = ... /* set iv byte array to 16 byte value of the IV for mykey1 */
byte[] iv2 = ... /* set iv byte array to 16 byte value of the IV for mykey2 */
Cipher cipher = Cipher.getInstance("ChainedCipher");
ChainedOperations ops = new ChainedOperations();
ops.addEncrypt(NAEKey.getSecretKey("mykey1"), "AES/CBC/PKCS5Padding",
new IvParameterSpec(iv));
ops.addEncrypt(NAEKey.getSecretKey("mykey2"), "AES/CBC/PKCS5Padding", newIvParameterSpec(iv2));
ops.seal();
cipher.init(Cipher.ENCRYPT_MODE, ops);
byte[] ciphertext = cipher.doFinal("Hello World!".getBytes());