Skip to content

Commit 475009b

Browse files
*Issue #, if available:* #108
*Description of changes:* Add a basic example for encrypting and decrypting with a KMS CMK. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. # Check any applicable: - [ ] Were any files moved? Moving files changes their URL, which breaks all hyperlinks to the files.
1 parent 3ef8958 commit 475009b

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except
5+
* in compliance with the License. A copy of the License is located at
6+
*
7+
* http://aws.amazon.com/apache2.0
8+
*
9+
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package com.amazonaws.crypto.examples;
15+
16+
import java.nio.charset.StandardCharsets;
17+
import java.util.Arrays;
18+
import java.util.Collections;
19+
import java.util.Map;
20+
21+
import com.amazonaws.encryptionsdk.AwsCrypto;
22+
import com.amazonaws.encryptionsdk.CryptoResult;
23+
import com.amazonaws.encryptionsdk.kms.KmsMasterKey;
24+
import com.amazonaws.encryptionsdk.kms.KmsMasterKeyProvider;
25+
26+
/**
27+
* <p>
28+
* Encrypts and then decrypts data using an AWS KMS customer master key.
29+
*
30+
* <p>
31+
* Arguments:
32+
* <ol>
33+
* <li>Key ARN: For help finding the Amazon Resource Name (ARN) of your KMS customer master
34+
* key (CMK), see 'Viewing Keys' at http://docs.aws.amazon.com/kms/latest/developerguide/viewing-keys.html
35+
* </ol>
36+
*/
37+
public class BasicEncryptionExample {
38+
39+
private static final byte[] EXAMPLE_DATA = "Hello World".getBytes(StandardCharsets.UTF_8);
40+
41+
public static void main(final String[] args) {
42+
final String keyArn = args[0];
43+
44+
encryptAndDecrypt(keyArn);
45+
}
46+
47+
static void encryptAndDecrypt(final String keyArn) {
48+
// 1. Instantiate the SDK
49+
final AwsCrypto crypto = new AwsCrypto();
50+
51+
// 2. Instantiate a KMS master key provider
52+
final KmsMasterKeyProvider prov = KmsMasterKeyProvider.builder().withKeysForEncryption(keyArn).build();
53+
54+
// 3. Create an encryption context
55+
//
56+
// Most encrypted data should have an associated encryption context
57+
// to protect integrity. This sample uses placeholder values.
58+
//
59+
// For more information see:
60+
// blogs.aws.amazon.com/security/post/Tx2LZ6WBJJANTNW/How-to-Protect-the-Integrity-of-Your-Encrypted-Data-by-Using-AWS-Key-Management
61+
final Map<String, String> context = Collections.singletonMap("ExampleContextKey", "ExampleContextValue");
62+
63+
// 4. Encrypt the data
64+
final CryptoResult<byte[], KmsMasterKey> encryptResult = crypto.encryptData(prov, EXAMPLE_DATA, context);
65+
final byte[] ciphertext = encryptResult.getResult();
66+
67+
// 5. Decrypt the data
68+
final CryptoResult<byte[], KmsMasterKey> decryptResult = crypto.decryptData(prov, ciphertext);
69+
70+
// 6. Before verifying the plaintext, verify that the customer master key that
71+
// was used in the encryption operation was the one supplied to the master key provider.
72+
if (!decryptResult.getMasterKeyIds().get(0).equals(keyArn)) {
73+
throw new IllegalStateException("Wrong key ID!");
74+
}
75+
76+
// 7. Also, verify that the encryption context in the result contains the
77+
// encryption context supplied to the encryptData method. Because the
78+
// SDK can add values to the encryption context, don't require that
79+
// the entire context matches.
80+
if (!context.entrySet().stream()
81+
.allMatch(e -> e.getValue().equals(decryptResult.getEncryptionContext().get(e.getKey())))) {
82+
throw new IllegalStateException("Wrong Encryption Context!");
83+
}
84+
85+
// 8. Verify that the decrypted plaintext matches the original plaintext
86+
assert Arrays.equals(decryptResult.getResult(), EXAMPLE_DATA);
87+
}
88+
}

0 commit comments

Comments
 (0)