|
| 1 | +# AWS Encryption SDK for Java |
| 2 | + |
| 3 | +The AWS Encryption SDK enables secure client-side encryption by using 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, so you don't need to keep track of the DEKs for your data. The SDK supports master keys in the [AWS Key Management Service](https://aws.amazon.com/kms/) (AWS 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]. |
| 4 | + |
| 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 | + |
| 7 | +## Getting Started |
| 8 | + |
| 9 | +### Required Prerequisites |
| 10 | +To use this SDK you must have: |
| 11 | + |
| 12 | +* **A Java development environment** |
| 13 | +If you do not have one, go to [Java SE Downloads](https://www.oracle.com/technetwork/java/javase/downloads/index.html) and then download and install the Java SE Development Kit (JDK). |
| 14 | + |
| 15 | +* **Bouncy Castle** |
| 16 | +Bouncy Castle provides a cryptography API for Java. If you do not have Bouncy Castle, go to https://bouncycastle.org/latest_releases.html and then download the provider file that corresponds to your JDK. |
| 17 | + |
| 18 | +### Optional Prerequisites |
| 19 | + |
| 20 | +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. |
| 21 | + |
| 22 | +* **To sign up for AWS**, go to [Sign In or Create an AWS Account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) and choose **I am a new user.** Follow the instructions to sign up and create an AWS account. |
| 23 | + |
| 24 | +* **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. |
| 25 | + |
| 26 | +* **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 Developer Guide* and then follow the instructions on that page. |
| 27 | + |
| 28 | +### Download the SDK |
| 29 | + |
| 30 | +### Get Started |
| 31 | + |
| 32 | +The following code sample demonstrates how to get started: |
| 33 | + |
| 34 | +1. Instantiate the SDK |
| 35 | +2. Define the master key provider |
| 36 | +3. Encrypt and decrypt data |
| 37 | + |
| 38 | +```java |
| 39 | +// This sample code encrypts and then decrypts a string using a KMS master key. |
| 40 | +// You provide the KMS key ARN and plaintext string as arguments. |
| 41 | +package com.amazonaws.crypto.examples; |
| 42 | + |
| 43 | +import java.util.Collections; |
| 44 | +import java.util.Map; |
| 45 | + |
| 46 | +import com.amazonaws.encryptionsdk.AwsCrypto; |
| 47 | +import com.amazonaws.encryptionsdk.CryptoResult; |
| 48 | +import com.amazonaws.encryptionsdk.kms.KmsMasterKey; |
| 49 | +import com.amazonaws.encryptionsdk.kms.KmsMasterKeyProvider; |
| 50 | + |
| 51 | +public class StringExample { |
| 52 | + private static String keyArn; |
| 53 | + private static String data; |
| 54 | + |
| 55 | + public static void main(final String[] args) { |
| 56 | + keyArn = args[0]; |
| 57 | + data = args[1]; |
| 58 | + |
| 59 | + // Instantiate the SDK |
| 60 | + final AwsCrypto crypto = new AwsCrypto(); |
| 61 | + |
| 62 | + // Set up the master key provider |
| 63 | + final KmsMasterKeyProvider prov = new KmsMasterKeyProvider(keyArn); |
| 64 | + |
| 65 | + // Encrypt the data |
| 66 | + // |
| 67 | + // NOTE: Encrypted data should have associated encryption context |
| 68 | + // to protect integrity. For this example, just use a placeholder |
| 69 | + // value. For more information about encryption context, see |
| 70 | + // https://amzn.to/1nSbe9X (blogs.aws.amazon.com) |
| 71 | + final Map<String, String> context = Collections.singletonMap("Example", "String"); |
| 72 | + |
| 73 | + final String ciphertext = crypto.encryptString(prov, data, context).getResult(); |
| 74 | + System.out.println("Ciphertext: " + ciphertext); |
| 75 | + |
| 76 | + // Decrypt the data |
| 77 | + final CryptoResult<String, KmsMasterKey> decryptResult = crypto.decryptString(prov, ciphertext); |
| 78 | + // Check the encryption context (and ideally the master key) to |
| 79 | + // ensure this is the expected ciphertext |
| 80 | + if (!decryptResult.getMasterKeyIds().get(0).equals(keyArn)) { |
| 81 | + throw new IllegalStateException("Wrong key id!"); |
| 82 | + } |
| 83 | + |
| 84 | + // The SDK may add information to the encryption context, so check to |
| 85 | + // ensure all of the values are present |
| 86 | + for (final Map.Entry<String, String> e : context.entrySet()) { |
| 87 | + if (!e.getValue().equals(decryptResult.getEncryptionContext().get(e.getKey()))) { |
| 88 | + throw new IllegalStateException("Wrong Encryption Context!"); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // The data is correct, so output it. |
| 93 | + System.out.println("Decrypted: " + decryptResult.getResult()); |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +For more examples, look in the [examples directory][examples]. |
| 99 | + |
| 100 | +## FAQ |
| 101 | + |
| 102 | +See the [Frequently Asked Questions](https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/faq.html) page in the official documentation. |
| 103 | + |
| 104 | +[examples]: examples/com/amazonaws/crypto/examples/ |
0 commit comments