Skip to content

Commit 8705f16

Browse files
Update Readme for Keyrings (#156)
* Update Readme for Keyrings
1 parent 8bdb1d4 commit 8705f16

File tree

1 file changed

+58
-42
lines changed

1 file changed

+58
-42
lines changed

README.md

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
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

5-
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/).
5+
For details about the design, architecture and usage of the SDK, see the [official documentation](https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/), [example code][examples] and the [Javadoc](https://aws.github.io/aws-encryption-sdk-java/javadoc/).
66

77
## Getting Started
88

@@ -54,7 +54,7 @@ You can get the latest release from Maven:
5454
<dependency>
5555
<groupId>com.amazonaws</groupId>
5656
<artifactId>aws-encryption-sdk-java</artifactId>
57-
<version>1.6.1</version>
57+
<version>1.7.0</version>
5858
</dependency>
5959
```
6060

@@ -63,65 +63,81 @@ 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 a 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 Key Management Service (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 a KMS keyring. Supply the key ARN for the generator key that generates a
99+
// data key. While using a key ARN is a best practice, for encryption operations you can also
100+
// use an alias name or alias ARN.
101+
final Keyring keyring = StandardKeyrings.awsKms(keyArn);
95102

96-
// Encrypt the data
103+
// 3. Create an encryption context
104+
//
105+
// Most encrypted data should have an associated encryption context
106+
// to protect integrity. This sample uses placeholder values.
97107
//
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!");
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. You can use the same keyring 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. To verify the CMK that was actually used in the decrypt operation, inspect the keyring trace.
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. To verify that the encryption context used to decrypt the data was the encryption context you expected,
132+
// examine the encryption context in the result. This helps to ensure that you decrypted the ciphertext that
133+
// you intended.
134+
//
135+
// When verifying, test that your expected encryption context is a subset of the actual encryption context,
136+
// not an exact match. The Encryption SDK adds the signing key to the encryption context when appropriate.
137+
assert decryptResult.getEncryptionContext().get("Example").equals("String");
122138

123-
// The data is correct, so output it.
124-
System.out.println("Decrypted: " + decryptResult.getResult());
139+
// 8. Verify that the decrypted plaintext matches the original plaintext
140+
assert Arrays.equals(decryptResult.getResult(), EXAMPLE_DATA);
125141
}
126142
}
127143
```

0 commit comments

Comments
 (0)