Skip to content

Commit da0fe89

Browse files
josecorellajuneb
andauthored
chore(docs): update README get started example (#423)
Co-authored-by: June Blender <[email protected]>
1 parent f8ada9b commit da0fe89

File tree

1 file changed

+40
-38
lines changed

1 file changed

+40
-38
lines changed

README.md

+40-38
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
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).
3+
The AWS Encryption SDK enables secure client-side encryption. It uses cryptography best practices to protect your data and protect the encryption keys that protect your data. Each data object is protected with a unique data encryption key, and the data encryption key is protected with a key encryption key called a *wrapping key* or *master key*. The encryption method returns a single, portable [encrypted message](https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/message-format.html) that contains the encrypted data and the encrypted data key, so you don't need to keep track of the data encryption keys for your data. You can use KMS keys in [AWS Key Management Service](https://aws.amazon.com/kms/) (AWS KMS) as wrapping keys. The AWS Encryption SDK also provides APIs to define and use encryption keys from other key providers.
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+
The AWS Encryption SDK for Java 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).
6+
7+
For more details about the design and architecture of the AWS Encryption SDK, see the [AWS Encryption SDK Developer Guide](https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/).
68

79
[Security issue notifications](./CONTRIBUTING.md#security-issue-notifications)
810

@@ -11,7 +13,7 @@ See [Support Policy](./SUPPORT_POLICY.rst) for for details on the current suppor
1113
## Getting Started
1214

1315
### Required Prerequisites
14-
To use this SDK you must have:
16+
To use the AWS Encryption SDK for Java you must have:
1517

1618
* **A Java 8 or newer development environment**
1719

@@ -29,29 +31,25 @@ To use this SDK you must have:
2931
If you do not have Bouncy Castle, go to https://bouncycastle.org/latest_releases.html, then download the provider file that corresponds to your JDK.
3032
Or, you can pick it up from Maven (groupId: `org.bouncycastle`, artifactId: `bcprov-ext-jdk15on`).
3133

32-
Beginning in version 1.6.1,
33-
the AWS Encryption SDK also works with Bouncy Castle FIPS (groupId: `org.bouncycastle`, artifactId: `bc-fips`)
34-
as an alternative to non-FIPS Bouncy Castle.
35-
For help installing and configuring Bouncy Castle FIPS properly, see [BC FIPS documentation](https://www.bouncycastle.org/documentation.html),
36-
in particular, **User Guides** and **Security Policy**.
34+
Beginning in version 1.6.1, the AWS Encryption SDK for Java also works with Bouncy Castle FIPS (groupId: `org.bouncycastle`, artifactId: `bc-fips`)
35+
as an alternative to non-FIPS Bouncy Castle. For help installing and configuring Bouncy Castle FIPS, see [BC FIPS documentation](https://www.bouncycastle.org/documentation.html), in particular, **User Guides** and **Security Policy**.
3736

3837
### Optional Prerequisites
3938

4039
#### AWS Integration
41-
You don't need an Amazon Web Services (AWS) account to use this SDK, but some of the [example code][examples] requires an AWS account, a customer master key (CMK) in AWS KMS, and the AWS SDK for Java.
40+
You don't need an Amazon Web Services (AWS) account to use the AWS Encryption SDK, but some of the [example code][examples] require an AWS account, an AWS KMS key, and the AWS SDK for Java 1.x. (The AWS Encryption SDK for Java does not support the AWS SDK for Java 2.x.)
4241

4342
* **To create an AWS account**, go to [Sign In or Create an AWS Account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) and then choose **I am a new user.** Follow the instructions to create an AWS account.
4443

45-
* **To create a CMK in AWS KMS**, go to [Creating Keys](https://docs.aws.amazon.com/kms/latest/developerguide/create-keys.html) in the KMS documentation and then follow the instructions on that page.
44+
* **To create a symmetric encryption KMS key in AWS KMS**, see [Creating Keys](https://docs.aws.amazon.com/kms/latest/developerguide/create-keys.html).
4645

47-
* **To download and install the AWS SDK for Java**, go to [Installing the AWS SDK for Java](https://docs.aws.amazon.com/AWSSdkDocsJava/latest/DeveloperGuide/java-dg-install-sdk.html) in the AWS SDK for Java documentation and then follow the instructions on that page.
46+
* **To download and install the AWS SDK for Java 1.x**, see [Installing the AWS SDK for Java 1.x](https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/getting-started.html).
4847

4948
#### Amazon Corretto Crypto Provider
5049
Many users find that the Amazon Corretto Crypto Provider (ACCP) significantly improves the performance of the AWS Encryption SDK.
51-
For help installing and using ACCP, see the [ACCP GitHub Respository](https://github.com/corretto/amazon-corretto-crypto-provider) .
52-
53-
### Download
50+
For help installing and using ACCP, see the [amazon-corretto-crypto-provider repository](https://github.com/corretto/amazon-corretto-crypto-provider).
5451

52+
### Download the AWS Encryption SDK for Java
5553
You can get the latest release from Maven:
5654

5755
```xml
@@ -63,18 +61,19 @@ You can get the latest release from Maven:
6361
```
6462

6563
### Get Started
64+
To get started with the AWS Encryption SDK for Java
6665

67-
The following code sample demonstrates how to get started:
68-
69-
1. Instantiate the SDK.
66+
1. Instantiate the AWS Encryption SDK.
7067
2. Define the master key provider.
7168
3. Encrypt and decrypt data.
7269

7370
```java
74-
// This sample code encrypts and then decrypts a string using a KMS CMK.
71+
// This sample code encrypts and then decrypts a string using an AWS KMS key.
7572
// You provide the KMS key ARN and plaintext string as arguments.
7673
package com.amazonaws.crypto.examples;
7774

75+
import java.nio.charset.StandardCharsets;
76+
import java.util.Arrays;
7877
import java.util.Collections;
7978
import java.util.Map;
8079

@@ -86,47 +85,50 @@ import com.amazonaws.encryptionsdk.kms.KmsMasterKeyProvider;
8685

8786
public class StringExample {
8887
private static String keyArn;
89-
private static String data;
88+
private static String plaintext;
9089

9190
public static void main(final String[] args) {
9291
keyArn = args[0];
93-
data = args[1];
92+
plaintext = args[1];
9493

9594
// Instantiate the SDK
9695
final AwsCrypto crypto = AwsCrypto.standard();
9796

9897
// Set up the master key provider
9998
final KmsMasterKeyProvider prov = KmsMasterKeyProvider.builder().buildStrict(keyArn);
10099

101-
// Encrypt the data
102-
//
100+
// Set up the encryption context
103101
// NOTE: Encrypted data should have associated encryption context
104-
// to protect integrity. For this example, just use a placeholder
105-
// value. For more information about encryption context, see
106-
// https://amzn.to/1nSbe9X (blogs.aws.amazon.com)
107-
final Map<String, String> context = Collections.singletonMap("Example", "String");
102+
// to protect its integrity. This example uses placeholder values.
103+
// For more information about the encryption context, see
104+
// https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
105+
final Map<String, String> context = Collections.singletonMap("ExampleContextKey", "ExampleContextValue");
108106

109-
final String ciphertext = crypto.encryptString(prov, data, context).getResult();
110-
System.out.println("Ciphertext: " + ciphertext);
107+
// Encrypt the data
108+
//
109+
final CryptoResult<byte[], KmsMasterKey> encryptResult = crypto.encryptData(prov, plaintext.getBytes(StandardCharsets.UTF_8), context);
110+
final byte[] ciphertext = encryptResult.getResult();
111+
System.out.println("Ciphertext: " + Arrays.toString(ciphertext));
111112

112113
// Decrypt the data
113-
final CryptoResult<String, KmsMasterKey> decryptResult = crypto.decryptString(prov, ciphertext);
114-
// Check the encryption context (and ideally the master key) to
115-
// ensure this is the expected ciphertext
114+
final CryptoResult<byte[], KmsMasterKey> decryptResult = crypto.decryptData(prov, ciphertext);
115+
// Your application should verify the encryption context and the KMS key to
116+
// ensure this is the expected ciphertext before returning the plaintext
116117
if (!decryptResult.getMasterKeyIds().get(0).equals(keyArn)) {
117118
throw new IllegalStateException("Wrong key id!");
118119
}
119120

120-
// The SDK may add information to the encryption context, so check to
121-
// ensure all of the values are present
122-
for (final Map.Entry<String, String> e : context.entrySet()) {
123-
if (!e.getValue().equals(decryptResult.getEncryptionContext().get(e.getKey()))) {
121+
// The AWS Encryption SDK may add information to the encryption context, so check to
122+
// ensure all of the values that you specified when encrypting are *included* in the returned encryption context.
123+
if (!context.entrySet().stream
124+
.allMatch( e -> e.getValue().equals(decryptResult.getEncryptionContext().get(e.getKey())))) {
124125
throw new IllegalStateException("Wrong Encryption Context!");
125-
}
126126
}
127127

128-
// The data is correct, so output it.
129-
System.out.println("Decrypted: " + decryptResult.getResult());
128+
assert Arrays.equals(decryptResult.getResult(), data.getBytes(StandardCharsets.UTF_8));
129+
130+
// The data is correct, so return it.
131+
System.out.println("Decrypted: " + new String(decryptResult.getResult(), StandardCharsets.UTF_8));
130132
}
131133
}
132134
```

0 commit comments

Comments
 (0)