Return Intermediate Result in a Chain of Operations
To return an intermediate result in a chain of operations:
Obtain an instance of a chained cipher.
Create a
ChainedOperations
object. Add the an operation to that object. An encryption operation is used in the below example. Because the example returns the encrypted text along with the MAC, you must add the addSend method to the encryption operation.Add the second operation. A MAC operation is used in the below example.
Seal the operations.
Pass the
ChainedOperations
object to thecipher.init
method.Call the
cipher.doFinal
method, passing the bytes of data you want to encrypt and MAC.
Code sample
The following code sample below demonstrates how to encrypt Hello World! with the key mykey, then MAC the encrypted text with the key mackey. This chain of operations returns the encrypted text concatenated with the MAC.
Cipher cipher = Cipher.getInstance("ChainedCipher", "IngrianProvider");
ChainedOperations ops = new ChainedOperations();
ops.addEncrypt(NAEKey.getSecretKey("mykey"), "AES/CBC/PKCS5Padding", iv);
ops.addSend();
ops.addMAC(NAEKey.getSecretKey("mackey"), "HmacSHA1");
ops.seal();
cipher.init(Cipher.ENCRYPT_MODE, ops);
byte[] ciphertext = cipher.doFinal("Hello World!".getBytes());