14
14
package com .amazonaws .crypto .examples ;
15
15
16
16
import com .amazonaws .encryptionsdk .AwsCrypto ;
17
+ import com .amazonaws .encryptionsdk .AwsCrypto .AwsCryptoConfig ;
17
18
import com .amazonaws .encryptionsdk .AwsCryptoInputStream ;
18
19
import com .amazonaws .encryptionsdk .keyrings .Keyring ;
19
20
import com .amazonaws .encryptionsdk .keyrings .StandardKeyrings ;
49
50
public class FileStreamingExample {
50
51
51
52
public static void main (String [] args ) throws IOException {
52
- final String srcFile = args [0 ];
53
+ final File srcFile = new File (args [0 ]);
54
+ final File encryptedFile = new File (args [1 ]);
55
+ final File decryptedFile = new File (args [2 ]);
53
56
54
- encryptAndDecrypt (srcFile );
57
+ encryptAndDecrypt (srcFile , encryptedFile , decryptedFile );
55
58
56
59
}
57
60
58
- static void encryptAndDecrypt (final String srcFile ) throws IOException {
61
+ static void encryptAndDecrypt (final File srcFile , final File encryptedFile , final File decryptedFile ) throws IOException {
59
62
// 1. Instantiate the SDK
60
63
final AwsCrypto crypto = new AwsCrypto ();
61
64
@@ -76,46 +79,40 @@ static void encryptAndDecrypt(final String srcFile) throws IOException {
76
79
final Map <String , String > encryptionContext = Collections .singletonMap ("Example" , "FileStreaming" );
77
80
78
81
// 5. Instantiate the AwsCryptoConfig input to AwsCrypto with the keyring and encryption context
79
- final AwsCrypto . AwsCryptoConfig config = AwsCrypto . AwsCryptoConfig .builder ()
82
+ final AwsCryptoConfig config = AwsCryptoConfig .builder ()
80
83
.keyring (keyring )
81
84
.encryptionContext (encryptionContext )
82
85
.build ();
83
86
84
87
// 6. Create the encrypting stream. Because the file might be too large to load into memory,
85
88
// we stream the data, instead of loading it all at once.
86
- final AwsCryptoInputStream encryptingStream =
87
- crypto .createEncryptingStream (config , new FileInputStream (srcFile ));
88
-
89
- // 7. Copy the encrypted data into a file.
90
- final File encryptedFile = new File (srcFile + ".encrypted" );
91
- try (FileOutputStream out = new FileOutputStream (encryptedFile )) {
92
- IOUtils .copy (encryptingStream , out );
93
- encryptingStream .close ();
89
+ try (final AwsCryptoInputStream encryptingStream =
90
+ crypto .createEncryptingStream (config , new FileInputStream (srcFile ))) {
91
+
92
+ // 7. Copy the encrypted data into the encrypted file.
93
+ try (FileOutputStream out = new FileOutputStream (encryptedFile )) {
94
+ IOUtils .copy (encryptingStream , out );
95
+ }
94
96
}
95
97
96
98
// 8. Create the decrypting stream.
97
- final AwsCryptoInputStream decryptingStream =
98
- crypto .createDecryptingStream (config , new FileInputStream (encryptedFile ));
99
+ try ( final AwsCryptoInputStream decryptingStream =
100
+ crypto .createDecryptingStream (config , new FileInputStream (encryptedFile ))) {
99
101
100
- // 9. Verify that the encryption context in the result contains the
101
- // encryption context supplied to the createEncryptingStream method.
102
- if (!"FileStreaming" .equals (decryptingStream .getAwsCryptoResult ().getEncryptionContext ().get ("Example" ))) {
103
- throw new IllegalStateException ("Bad encryption context" );
104
- }
102
+ // 9. Verify that the encryption context in the result contains the
103
+ // encryption context supplied to the createEncryptingStream method.
104
+ if (!"FileStreaming" .equals (decryptingStream .getAwsCryptoResult ().getEncryptionContext ().get ("Example" ))) {
105
+ throw new IllegalStateException ("Bad encryption context" );
106
+ }
105
107
106
- // 10. Copy the plaintext data to a file
107
- final File decryptedFile = new File (srcFile + ".decrypted" );
108
- try (FileOutputStream out = new FileOutputStream (decryptedFile )) {
109
- IOUtils .copy (decryptingStream , out );
110
- decryptingStream .close ();
108
+ // 10. Copy the plaintext data to a file
109
+ try (FileOutputStream out = new FileOutputStream (decryptedFile )) {
110
+ IOUtils .copy (decryptingStream , out );
111
+ }
111
112
}
112
113
113
114
// 11. Compare the decrypted file to the original
114
- compareFiles (decryptedFile , new File (srcFile ));
115
-
116
- // 12. Clean up the encrypted and decrypted files
117
- encryptedFile .delete ();
118
- decryptedFile .delete ();
115
+ compareFiles (decryptedFile , srcFile );
119
116
}
120
117
121
118
/**
0 commit comments