Skip to content

Commit 68ad171

Browse files
Update Readme for Keyrings
1 parent 1ee9643 commit 68ad171

File tree

1 file changed

+53
-40
lines changed

1 file changed

+53
-40
lines changed

README.md

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# AWS Encryption SDK for Java
22

3-
The AWS Encryption SDK enables secure client-side encryption. It uses cryptography best practices to protect your data and the encryption keys used to protect that data. Each data object is protected with a unique data encryption key (DEK), and the DEK is protected with a key encryption key (KEK) called a *master key*. The encrypted DEK is combined with the encrypted data into a single [encrypted message](https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/message-format.html), so you don't need to keep track of the DEKs for your data. The SDK supports master keys in [AWS Key Management Service](https://aws.amazon.com/kms/) (KMS), and it also provides APIs to define and use other master key providers. The SDK provides methods for encrypting and decrypting strings, byte arrays, and byte streams. For details, see the [example code][examples] and the [Javadoc](https://aws.github.io/aws-encryption-sdk-java/javadoc/).
3+
The AWS Encryption SDK is a client-side encryption library designed to make it easy for everyone to encrypt and decrypt data using industry standards and best practices. It enables you to focus on the core functionality of your application, rather than on how to best encrypt and decrypt your data.
44

55
For more details about the design and architecture of the SDK, see the [official documentation](https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/).
66

@@ -63,65 +63,78 @@ You can get the latest release from Maven:
6363
The following code sample demonstrates how to get started:
6464

6565
1. Instantiate the SDK.
66-
2. Define the master key provider.
66+
2. Setup an AWS KMS Keyring.
6767
3. Encrypt and decrypt data.
6868

6969
```java
70-
// This sample code encrypts and then decrypts a string using a KMS CMK.
71-
// You provide the KMS key ARN and plaintext string as arguments.
70+
// This sample code encrypts and then decrypts data using an AWS KMS customer master key (CMK).
7271
package com.amazonaws.crypto.examples;
7372

73+
import java.nio.charset.StandardCharsets;
74+
import java.util.Arrays;
7475
import java.util.Collections;
7576
import java.util.Map;
7677

7778
import com.amazonaws.encryptionsdk.AwsCrypto;
78-
import com.amazonaws.encryptionsdk.CryptoResult;
79-
import com.amazonaws.encryptionsdk.kms.KmsMasterKey;
80-
import com.amazonaws.encryptionsdk.kms.KmsMasterKeyProvider;
79+
import com.amazonaws.encryptionsdk.AwsCryptoResult;
80+
import com.amazonaws.encryptionsdk.DecryptRequest;
81+
import com.amazonaws.encryptionsdk.EncryptRequest;
82+
import com.amazonaws.encryptionsdk.keyrings.Keyring;
83+
import com.amazonaws.encryptionsdk.keyrings.StandardKeyrings;
84+
import com.amazonaws.encryptionsdk.kms.AwsKmsCmkId;
8185

82-
public class StringExample {
83-
private static String keyArn;
84-
private static String data;
86+
public class BasicEncryptionExample {
87+
88+
private static final byte[] EXAMPLE_DATA = "Hello World".getBytes(StandardCharsets.UTF_8);
8589

8690
public static void main(final String[] args) {
87-
keyArn = args[0];
88-
data = args[1];
91+
encryptAndDecrypt(AwsKmsCmkId.fromString(args[0]));
92+
}
8993

90-
// Instantiate the SDK
94+
static void encryptAndDecrypt(final AwsKmsCmkId keyArn) {
95+
// 1. Instantiate the SDK
9196
final AwsCrypto crypto = new AwsCrypto();
9297

93-
// Set up the master key provider
94-
final KmsMasterKeyProvider prov = new KmsMasterKeyProvider(keyArn);
98+
// 2. Instantiate an AWS KMS Keyring, supplying the key ARN as the generator for generating a
99+
// data key. While using a key ARN is a best practice, for encryption operations it is also
100+
// acceptable to use a CMK alias or an alias ARN.
101+
final Keyring keyring = StandardKeyrings.awsKms(keyArn);
95102

96-
// Encrypt the data
103+
// 3. Create an encryption context
97104
//
98-
// NOTE: Encrypted data should have associated encryption context
99-
// to protect integrity. For this example, just use a placeholder
100-
// value. For more information about encryption context, see
101-
// https://amzn.to/1nSbe9X (blogs.aws.amazon.com)
102-
final Map<String, String> context = Collections.singletonMap("Example", "String");
103-
104-
final String ciphertext = crypto.encryptString(prov, data, context).getResult();
105-
System.out.println("Ciphertext: " + ciphertext);
106-
107-
// Decrypt the data
108-
final CryptoResult<String, KmsMasterKey> decryptResult = crypto.decryptString(prov, ciphertext);
109-
// Check the encryption context (and ideally the master key) to
110-
// ensure this is the expected ciphertext
111-
if (!decryptResult.getMasterKeyIds().get(0).equals(keyArn)) {
112-
throw new IllegalStateException("Wrong key id!");
105+
// Most encrypted data should have an associated encryption context
106+
// to protect integrity. This sample uses placeholder values.
107+
//
108+
// For more information see: https://amzn.to/1nSbe9X (blogs.aws.amazon.com)
109+
final Map<String, String> encryptionContext = Collections.singletonMap("Example", "String");
110+
111+
// 4. Encrypt the data with the keyring and encryption context
112+
final AwsCryptoResult<byte[]> encryptResult = crypto.encrypt(
113+
EncryptRequest.builder()
114+
.keyring(keyring)
115+
.encryptionContext(encryptionContext)
116+
.plaintext(EXAMPLE_DATA).build());
117+
final byte[] ciphertext = encryptResult.getResult();
118+
119+
// 5. Decrypt the data. The same keyring may be used to encrypt and decrypt, but for decryption
120+
// the key IDs must be in the key ARN format.
121+
final AwsCryptoResult<byte[]> decryptResult = crypto.decrypt(
122+
DecryptRequest.builder()
123+
.keyring(keyring)
124+
.ciphertext(ciphertext).build());
125+
126+
// 6. The Keyring Trace may be inspected to verify which CMK was used for decryption.
127+
if(!decryptResult.getKeyringTrace().getEntries().get(0).getKeyName().equals(keyArn.toString())) {
128+
throw new IllegalStateException("Wrong key ID!");
113129
}
114130

115-
// The SDK may add information to the encryption context, so check to
116-
// ensure all of the values are present
117-
for (final Map.Entry<String, String> e : context.entrySet()) {
118-
if (!e.getValue().equals(decryptResult.getEncryptionContext().get(e.getKey()))) {
119-
throw new IllegalStateException("Wrong Encryption Context!");
120-
}
121-
}
131+
// 7. Verify that the encryption context in the result contains the
132+
// data that we expect. The SDK can add values to the encryption context,
133+
// so there may be additional keys in the result context.
134+
assert decryptResult.getEncryptionContext().get("Example").equals("String");
122135

123-
// The data is correct, so output it.
124-
System.out.println("Decrypted: " + decryptResult.getResult());
136+
// 8. Verify that the decrypted plaintext matches the original plaintext
137+
assert Arrays.equals(decryptResult.getResult(), EXAMPLE_DATA);
125138
}
126139
}
127140
```

0 commit comments

Comments
 (0)