Skip to content

Commit 69117d9

Browse files
committed
Sync code comments with SDK examples
1 parent 77d7094 commit 69117d9

File tree

3 files changed

+42
-36
lines changed

3 files changed

+42
-36
lines changed

src/examples/java/com/amazonaws/crypto/examples/EscrowedEncryptExample.java

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,33 @@
3636
* <p>
3737
* Arguments:
3838
* <ol>
39-
* <li>Key ARN: To find the Amazon Resource Name of your KMS customer master key (CMK),
40-
* see 'Viewing Keys' at http://docs.aws.amazon.com/kms/latest/developerguide/viewing-keys.html
41-
* <li>File Name
39+
* <li>Key ARN: For help finding the Amazon Resource Name (ARN) of your KMS customer master
40+
* key (CMK), see 'Viewing Keys' at http://docs.aws.amazon.com/kms/latest/developerguide/viewing-keys.html
41+
*
42+
* <li>Name of file containing plaintext data to encrypt
4243
* </ol>
4344
*
44-
* AWS Key Management Service (KMS) is highly available. However, some organizations want to decrypt
45-
* their data offline and independent of KMS. This sample demonstrates one way to do this.
45+
* You might use AWS Key Management Service (KMS) for most encryption and decryption operations, but
46+
* still want the option of decrypting your data offline independently of KMS. This sample
47+
* demonstrates one way to do this.
4648
*
47-
* This program generates an "escrowed" RSA key pair. It stores the private key in a secure offline
48-
* location, such as an offline HSM, and distributes the public key to their developers. It also
49-
* creates a KMS customer master key (CMK). The organization encrypts their data with both the
50-
* KMS CMK and the public key, so that either key alone could decrypt it.
49+
* The sample encrypts data under both a KMS customer master key (CMK) and an "escrowed" RSA key pair
50+
* so that either key alone can decrypt it. You might commonly use the KMS CMK for decryption. However,
51+
* at any time, you can use the private RSA key to decrypt the ciphertext independent of KMS.
52+
*
53+
* This sample uses the JCEMasterKey class to generate a RSA public-private key pair
54+
* and saves the key pair in memory. In practice, you would store the private key in a secure offline
55+
* location, such as an offline HSM, and distribute the public key to your development team.
5156
*
52-
* The team usually uses the KMS CMK for decryption. However, the organization can, at any time
53-
* use the private escrowed RSA key to decrypt the ciphertext independent of KMS.
5457
*/
5558
public class EscrowedEncryptExample {
5659
private static PublicKey publicEscrowKey;
5760
private static PrivateKey privateEscrowKey;
5861

5962
public static void main(final String[] args) throws Exception {
60-
// In practice, the organization would distribute the public key.
61-
// For this demo, we generate a new random key for each operation.
63+
// This sample generates a new random key for each operation.
64+
// In practice, you would distribute the public key and save the private key in secure
65+
// storage.
6266
generateEscrowKeyPair();
6367

6468
final String kmsArn = args[0];
@@ -71,16 +75,16 @@ public static void main(final String[] args) throws Exception {
7175
}
7276

7377
private static void standardEncrypt(final String kmsArn, final String fileName) throws Exception {
74-
// Standard practice: encrypt with the KMS CMK and the escrowed public key
78+
// Encrypt with the KMS CMK and the escrowed public key
7579
// 1. Instantiate the SDK
7680
final AwsCrypto crypto = new AwsCrypto();
7781

7882
// 2. Instantiate a KMS master key provider
7983
final KmsMasterKeyProvider kms = new KmsMasterKeyProvider(kmsArn);
8084

81-
// 3. Instantiate a JCE master key provider
82-
// Because the standard user does not have access to the private
83-
// escrow key, they pass in "null" for the private key parameter.
85+
// 3. Instantiate a JCE master key provider
86+
// Because the user does not have access to the private escrow key,
87+
// they pass in "null" for the private key parameter.
8488
final JceMasterKey escrowPub = JceMasterKey.getInstance(publicEscrowKey, null, "Escrow", "Escrow",
8589
"RSA/ECB/OAEPWithSHA-512AndMGF1Padding");
8690

@@ -100,16 +104,17 @@ private static void standardEncrypt(final String kmsArn, final String fileName)
100104
}
101105

102106
private static void standardDecrypt(final String kmsArn, final String fileName) throws Exception {
103-
// Standard practice: enncrypt with the KMS CMK and the escrow public key
107+
// Decrypt with the KMS CMK and the escrow public key. You can use a combined provider,
108+
// as shown here, or just the KMS master key provider.
104109

105110
// 1. Instantiate the SDK
106111
final AwsCrypto crypto = new AwsCrypto();
107112

108113
// 2. Instantiate a KMS master key provider
109114
final KmsMasterKeyProvider kms = new KmsMasterKeyProvider(kmsArn);
110115

111-
// 3. Instantiate a JCE master key provider
112-
// Because the standard user does not have access to the private
116+
// 3. Instantiate a JCE master key provider
117+
// Because the user does not have access to the private
113118
// escrow key, they pass in "null" for the private key parameter.
114119
final JceMasterKey escrowPub = JceMasterKey.getInstance(publicEscrowKey, null, "Escrow", "Escrow",
115120
"RSA/ECB/OAEPWithSHA-512AndMGF1Padding");
@@ -129,14 +134,14 @@ private static void standardDecrypt(final String kmsArn, final String fileName)
129134
}
130135

131136
private static void escrowDecrypt(final String fileName) throws Exception {
132-
// The organization can decrypt the stream using only the private escrow key.
133-
// This method does not call KMS.
137+
// You can decrypt the stream using only the private key.
138+
// This method does not call KMS.
134139

135140
// 1. Instantiate the SDK
136141
final AwsCrypto crypto = new AwsCrypto();
137142

138143
// 2. Instantiate a JCE master key provider
139-
// This method call uses the escrowed private key
144+
// This method call uses the escrowed private key, not null
140145
final JceMasterKey escrowPriv = JceMasterKey.getInstance(publicEscrowKey, privateEscrowKey, "Escrow", "Escrow",
141146
"RSA/ECB/OAEPWithSHA-512AndMGF1Padding");
142147

src/examples/java/com/amazonaws/crypto/examples/FileStreamingExample.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232
/**
3333
* <p>
3434
* Encrypts and then decrypts a file under a random key.
35-
*
35+
*
3636
* <p>
3737
* Arguments:
3838
* <ol>
39-
* <li>fileName
39+
* <li>Name of file containing plaintext data to encrypt
4040
* </ol>
41-
*
41+
*
4242
* <p>
4343
* This program demonstrates using a standard Java {@link SecretKey} object as a {@link MasterKey} to
4444
* encrypt and decrypt streaming data.
@@ -62,8 +62,8 @@ public static void main(String[] args) throws IOException {
6262
// Create an encryption context to identify this ciphertext
6363
Map<String, String> context = Collections.singletonMap("Example", "FileStreaming");
6464

65-
// Because the file might be to large to load into memory, we use
66-
// streaming, then encrypt the file stream.
65+
// Because the file might be to large to load into memory, we stream the data, instead of
66+
//loading it all at once.
6767
FileInputStream in = new FileInputStream(srcFile);
6868
CryptoInputStream<JceMasterKey> encryptingStream = crypto.createEncryptingStream(masterKey, in, context);
6969

@@ -89,12 +89,12 @@ public static void main(String[] args) throws IOException {
8989

9090
/**
9191
* In practice, this key would be saved in a secure location.
92-
* For this demo we'll generate a new random key for each operation.
92+
* For this demo, we generate a new random key for each operation.
9393
*/
9494
private static SecretKey retrieveEncryptionKey() {
9595
SecureRandom rnd = new SecureRandom();
9696
byte[] rawKey = new byte[16]; // 128 bits
9797
rnd.nextBytes(rawKey);
9898
return new SecretKeySpec(rawKey, "AES");
9999
}
100-
}
100+
}

src/examples/java/com/amazonaws/crypto/examples/StringExample.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828
* <p>
2929
* Arguments:
3030
* <ol>
31-
* <li>Key ARN: To find the Amazon Resource Name of your KMS customer master key (CMK),
32-
* see 'Viewing Keys' at http://docs.aws.amazon.com/kms/latest/developerguide/viewing-keys.html
31+
* <li>Key ARN: For help finding the Amazon Resource Name (ARN) of your KMS customer master
32+
* key (CMK), see 'Viewing Keys' at http://docs.aws.amazon.com/kms/latest/developerguide/viewing-keys.html
33+
* <li>String to encrypt
3334
* </ol>
3435
*/
3536
public class StringExample {
@@ -49,7 +50,7 @@ public static void main(final String[] args) {
4950
// Encrypt the data
5051
//
5152
// Most encrypted data should have an associated encryption context
52-
// to protect integrity. Here, we'll just use a placeholder value.
53+
// to protect integrity. This sample uses placeholder values.
5354
//
5455
// For more information see:
5556
// blogs.aws.amazon.com/security/post/Tx2LZ6WBJJANTNW/How-to-Protect-the-Integrity-of-Your-Encrypted-Data-by-Using-AWS-Key-Management
@@ -61,23 +62,23 @@ public static void main(final String[] args) {
6162
// Decrypt the data
6263
final CryptoResult<String, KmsMasterKey> decryptResult = crypto.decryptString(prov, ciphertext);
6364

64-
// Before returning the plaintext, verify that the customer master key that
65+
// Before returning the plaintext, verify that the customer master key that
6566
// was used in the encryption operation was the one supplied to the master key provider.
6667
if (!decryptResult.getMasterKeyIds().get(0).equals(keyArn)) {
6768
throw new IllegalStateException("Wrong key id!");
6869
}
6970

7071
// Also, verify that the encryption context in the result contains the
7172
// encryption context supplied to the encryptString method. Because the
72-
// SDK can add values to the encryption context, we don't require that
73+
// SDK can add values to the encryption context, don't require that
7374
// the entire context matches.
7475
for (final Map.Entry<String, String> e : context.entrySet()) {
7576
if (!e.getValue().equals(decryptResult.getEncryptionContext().get(e.getKey()))) {
7677
throw new IllegalStateException("Wrong Encryption Context!");
7778
}
7879
}
7980

80-
// Now that we know we have the correct data, we can return it.
81+
// Now we can return the plaintext data
8182
System.out.println("Decrypted: " + decryptResult.getResult());
8283
}
8384
}

0 commit comments

Comments
 (0)