|
| 1 | +/* |
| 2 | + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except |
| 5 | + * in compliance with the License. A copy of the License is located at |
| 6 | + * |
| 7 | + * http://aws.amazon.com/apache2.0 |
| 8 | + * |
| 9 | + * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package com.amazonaws.crypto.examples; |
| 15 | + |
| 16 | +import java.nio.charset.StandardCharsets; |
| 17 | +import java.util.Arrays; |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import com.amazonaws.encryptionsdk.AwsCrypto; |
| 22 | +import com.amazonaws.encryptionsdk.CryptoResult; |
| 23 | +import com.amazonaws.encryptionsdk.kms.KmsMasterKey; |
| 24 | +import com.amazonaws.encryptionsdk.kms.KmsMasterKeyProvider; |
| 25 | + |
| 26 | +/** |
| 27 | + * <p> |
| 28 | + * Encrypts and then decrypts data using an AWS KMS customer master key. |
| 29 | + * |
| 30 | + * <p> |
| 31 | + * Arguments: |
| 32 | + * <ol> |
| 33 | + * <li>Key ARN: For help finding the Amazon Resource Name (ARN) of your KMS customer master |
| 34 | + * key (CMK), see 'Viewing Keys' at http://docs.aws.amazon.com/kms/latest/developerguide/viewing-keys.html |
| 35 | + * </ol> |
| 36 | + */ |
| 37 | +public class BasicEncryptionExample { |
| 38 | + |
| 39 | + private static final byte[] EXAMPLE_DATA = "Hello World".getBytes(StandardCharsets.UTF_8); |
| 40 | + |
| 41 | + public static void main(final String[] args) { |
| 42 | + final String keyArn = args[0]; |
| 43 | + |
| 44 | + encryptAndDecrypt(keyArn); |
| 45 | + } |
| 46 | + |
| 47 | + static void encryptAndDecrypt(final String keyArn) { |
| 48 | + // 1. Instantiate the SDK |
| 49 | + final AwsCrypto crypto = new AwsCrypto(); |
| 50 | + |
| 51 | + // 2. Instantiate a KMS master key provider |
| 52 | + final KmsMasterKeyProvider masterKeyProvider = KmsMasterKeyProvider.builder().withKeysForEncryption(keyArn).build(); |
| 53 | + |
| 54 | + // 3. Create an encryption context |
| 55 | + // |
| 56 | + // Most encrypted data should have an associated encryption context |
| 57 | + // to protect integrity. This sample uses placeholder values. |
| 58 | + // |
| 59 | + // For more information see: |
| 60 | + // blogs.aws.amazon.com/security/post/Tx2LZ6WBJJANTNW/How-to-Protect-the-Integrity-of-Your-Encrypted-Data-by-Using-AWS-Key-Management |
| 61 | + final Map<String, String> encryptionContext = Collections.singletonMap("ExampleContextKey", "ExampleContextValue"); |
| 62 | + |
| 63 | + // 4. Encrypt the data |
| 64 | + final CryptoResult<byte[], KmsMasterKey> encryptResult = crypto.encryptData(masterKeyProvider, EXAMPLE_DATA, encryptionContext); |
| 65 | + final byte[] ciphertext = encryptResult.getResult(); |
| 66 | + |
| 67 | + // 5. Decrypt the data |
| 68 | + final CryptoResult<byte[], KmsMasterKey> decryptResult = crypto.decryptData(masterKeyProvider, ciphertext); |
| 69 | + |
| 70 | + // 6. Before verifying the plaintext, verify that the customer master key that |
| 71 | + // was used in the encryption operation was the one supplied to the master key provider. |
| 72 | + if (!decryptResult.getMasterKeyIds().get(0).equals(keyArn)) { |
| 73 | + throw new IllegalStateException("Wrong key ID!"); |
| 74 | + } |
| 75 | + |
| 76 | + // 7. Also, verify that the encryption context in the result contains the |
| 77 | + // encryption context supplied to the encryptData method. Because the |
| 78 | + // SDK can add values to the encryption context, don't require that |
| 79 | + // the entire context matches. |
| 80 | + if (!encryptionContext.entrySet().stream() |
| 81 | + .allMatch(e -> e.getValue().equals(decryptResult.getEncryptionContext().get(e.getKey())))) { |
| 82 | + throw new IllegalStateException("Wrong Encryption Context!"); |
| 83 | + } |
| 84 | + |
| 85 | + // 8. Verify that the decrypted plaintext matches the original plaintext |
| 86 | + assert Arrays.equals(decryptResult.getResult(), EXAMPLE_DATA); |
| 87 | + } |
| 88 | +} |
0 commit comments