1414import javax .crypto .spec .SecretKeySpec ;
1515
1616import com .amazonaws .encryptionsdk .AwsCrypto ;
17+ import com .amazonaws .encryptionsdk .CryptoAlgorithm ;
1718import com .amazonaws .encryptionsdk .CryptoInputStream ;
1819import com .amazonaws .encryptionsdk .MasterKey ;
1920import com .amazonaws .encryptionsdk .jce .JceMasterKey ;
@@ -49,7 +50,12 @@ public static void main(String[] args) throws IOException {
4950
5051 // Instantiate the SDK with a specific commitment policy.
5152 // ForbidEncryptAllowDecrypt is the only available policy in 1.7.0.
52- final AwsCrypto crypto = AwsCrypto .builder ().withCommitmentPolicy (CommitmentPolicy .ForbidEncryptAllowDecrypt ).build ();
53+ // This also chooses to encrypt with an algorithm suite that doesn't include signing for faster decryption,
54+ // since this use case assumes that the contexts that encrypt and decrypt are equally trusted.
55+ final AwsCrypto crypto = AwsCrypto .builder ()
56+ .withCommitmentPolicy (CommitmentPolicy .ForbidEncryptAllowDecrypt )
57+ .withEncryptionAlgorithm (CryptoAlgorithm .ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY )
58+ .build ();
5359
5460 // Create an encryption context to identify this ciphertext
5561 Map <String , String > context = Collections .singletonMap ("Example" , "FileStreaming" );
@@ -65,14 +71,16 @@ public static void main(String[] args) throws IOException {
6571 out .close ();
6672
6773 // Decrypt the file. Verify the encryption context before returning the plaintext.
74+ // Since we encrypted using an unsigned algorithm suite, we can use the recommended
75+ // createUnsignedMessageDecryptingStream method that only accepts unsigned messages.
6876 in = new FileInputStream (srcFile + ".encrypted" );
69- CryptoInputStream <JceMasterKey > decryptingStream = crypto .createDecryptingStream (masterKey , in );
77+ CryptoInputStream <JceMasterKey > decryptingStream = crypto .createUnsignedMessageDecryptingStream (masterKey , in );
7078 // Does it contain the expected encryption context?
7179 if (!"FileStreaming" .equals (decryptingStream .getCryptoResult ().getEncryptionContext ().get ("Example" ))) {
7280 throw new IllegalStateException ("Bad encryption context" );
7381 }
7482
75- // Return the plaintext data
83+ // Write the plaintext data to disk.
7684 out = new FileOutputStream (srcFile + ".decrypted" );
7785 IOUtils .copy (decryptingStream , out );
7886 decryptingStream .close ();
0 commit comments