14
14
import javax .crypto .spec .SecretKeySpec ;
15
15
16
16
import com .amazonaws .encryptionsdk .AwsCrypto ;
17
+ import com .amazonaws .encryptionsdk .CryptoAlgorithm ;
17
18
import com .amazonaws .encryptionsdk .CryptoInputStream ;
18
19
import com .amazonaws .encryptionsdk .MasterKey ;
19
20
import com .amazonaws .encryptionsdk .jce .JceMasterKey ;
@@ -49,7 +50,12 @@ public static void main(String[] args) throws IOException {
49
50
50
51
// Instantiate the SDK with a specific commitment policy.
51
52
// 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 ();
53
59
54
60
// Create an encryption context to identify this ciphertext
55
61
Map <String , String > context = Collections .singletonMap ("Example" , "FileStreaming" );
@@ -65,14 +71,16 @@ public static void main(String[] args) throws IOException {
65
71
out .close ();
66
72
67
73
// 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.
68
76
in = new FileInputStream (srcFile + ".encrypted" );
69
- CryptoInputStream <JceMasterKey > decryptingStream = crypto .createDecryptingStream (masterKey , in );
77
+ CryptoInputStream <JceMasterKey > decryptingStream = crypto .createUnsignedMessageDecryptingStream (masterKey , in );
70
78
// Does it contain the expected encryption context?
71
79
if (!"FileStreaming" .equals (decryptingStream .getCryptoResult ().getEncryptionContext ().get ("Example" ))) {
72
80
throw new IllegalStateException ("Bad encryption context" );
73
81
}
74
82
75
- // Return the plaintext data
83
+ // Write the plaintext data to disk.
76
84
out = new FileOutputStream (srcFile + ".decrypted" );
77
85
IOUtils .copy (decryptingStream , out );
78
86
decryptingStream .close ();
0 commit comments