|
1 | 1 | # AWS Encryption SDK for Java
|
2 | 2 |
|
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. |
4 | 4 |
|
5 | 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/).
|
6 | 6 |
|
@@ -63,65 +63,78 @@ You can get the latest release from Maven:
|
63 | 63 | The following code sample demonstrates how to get started:
|
64 | 64 |
|
65 | 65 | 1. Instantiate the SDK.
|
66 |
| -2. Define the master key provider. |
| 66 | +2. Setup an AWS KMS Keyring. |
67 | 67 | 3. Encrypt and decrypt data.
|
68 | 68 |
|
69 | 69 | ```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). |
72 | 71 | package com.amazonaws.crypto.examples;
|
73 | 72 |
|
| 73 | +import java.nio.charset.StandardCharsets; |
| 74 | +import java.util.Arrays; |
74 | 75 | import java.util.Collections;
|
75 | 76 | import java.util.Map;
|
76 | 77 |
|
77 | 78 | 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; |
81 | 85 |
|
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); |
85 | 89 |
|
86 | 90 | public static void main(final String[] args) {
|
87 |
| - keyArn = args[0]; |
88 |
| - data = args[1]; |
| 91 | + encryptAndDecrypt(AwsKmsCmkId.fromString(args[0])); |
| 92 | + } |
89 | 93 |
|
90 |
| - // Instantiate the SDK |
| 94 | + static void encryptAndDecrypt(final AwsKmsCmkId keyArn) { |
| 95 | + // 1. Instantiate the SDK |
91 | 96 | final AwsCrypto crypto = new AwsCrypto();
|
92 | 97 |
|
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); |
95 | 102 |
|
96 |
| - // Encrypt the data |
| 103 | + // 3. Create an encryption context |
97 | 104 | //
|
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!"); |
113 | 129 | }
|
114 | 130 |
|
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"); |
122 | 135 |
|
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); |
125 | 138 | }
|
126 | 139 | }
|
127 | 140 | ```
|
|
0 commit comments