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 ;
@@ -53,8 +54,11 @@ public static void main(String[] args) throws IOException {
5354 // that this client will only decrypt encrypted messages that were created with a committing algorithm suite.
5455 // This is the default commitment policy if you build the client with `AwsCrypto.builder().build()`
5556 // or `AwsCrypto.standard()`.
57+ // This also chooses to encrypt with an algorithm suite that doesn't include signing for faster decryption,
58+ // since this use case assumes that the contexts that encrypt and decrypt are equally trusted.
5659 final AwsCrypto crypto = AwsCrypto .builder ()
5760 .withCommitmentPolicy (CommitmentPolicy .RequireEncryptRequireDecrypt )
61+ .withEncryptionAlgorithm (CryptoAlgorithm .ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY )
5862 .build ();
5963
6064 // Create an encryption context to identify this ciphertext
@@ -71,14 +75,16 @@ public static void main(String[] args) throws IOException {
7175 out .close ();
7276
7377 // Decrypt the file. Verify the encryption context before returning the plaintext.
78+ // Since we encrypted using an unsigned algorithm suite, we can use the recommended
79+ // createUnsignedMessageDecryptingStream method that only accepts unsigned messages.
7480 in = new FileInputStream (srcFile + ".encrypted" );
75- CryptoInputStream <JceMasterKey > decryptingStream = crypto .createDecryptingStream (masterKey , in );
81+ CryptoInputStream <JceMasterKey > decryptingStream = crypto .createUnsignedMessageDecryptingStream (masterKey , in );
7682 // Does it contain the expected encryption context?
7783 if (!"FileStreaming" .equals (decryptingStream .getCryptoResult ().getEncryptionContext ().get ("Example" ))) {
7884 throw new IllegalStateException ("Bad encryption context" );
7985 }
8086
81- // Return the plaintext data
87+ // Write the plaintext data to disk.
8288 out = new FileOutputStream (srcFile + ".decrypted" );
8389 IOUtils .copy (decryptingStream , out );
8490 decryptingStream .close ();
0 commit comments