|
| 1 | +/* |
| 2 | + * Copyright 2020 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 com.amazonaws.encryptionsdk.AwsCrypto; |
| 17 | +import com.amazonaws.encryptionsdk.AwsCryptoResult; |
| 18 | +import com.amazonaws.encryptionsdk.DecryptRequest; |
| 19 | +import com.amazonaws.encryptionsdk.EncryptRequest; |
| 20 | +import com.amazonaws.encryptionsdk.keyrings.Keyring; |
| 21 | +import com.amazonaws.encryptionsdk.keyrings.StandardKeyrings; |
| 22 | + |
| 23 | +import javax.crypto.KeyGenerator; |
| 24 | +import javax.crypto.SecretKey; |
| 25 | +import javax.crypto.spec.SecretKeySpec; |
| 26 | +import java.nio.charset.StandardCharsets; |
| 27 | +import java.security.SecureRandom; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +/** |
| 33 | + * <p> |
| 34 | + * Encrypts and then decrypts data using the Raw AES keyring. |
| 35 | + */ |
| 36 | +public class RawAesKeyringExample { |
| 37 | + |
| 38 | + private static final byte[] EXAMPLE_DATA = "Hello World".getBytes(StandardCharsets.UTF_8); |
| 39 | + |
| 40 | + public static void main(final String[] args) { |
| 41 | + encryptAndDecrypt(); |
| 42 | + } |
| 43 | + |
| 44 | + static void encryptAndDecrypt() { |
| 45 | + // 1. Instantiate the SDK |
| 46 | + final AwsCrypto crypto = new AwsCrypto(); |
| 47 | + |
| 48 | + // 2. Get an encryption key. In this example, we generate a random key. |
| 49 | + // In practice, you would get a key from an existing key store |
| 50 | + final SecretKey cryptoKey = generateEncryptKey(); |
| 51 | + |
| 52 | + // 3. Instantiate a Raw AES keyring with the encryption key |
| 53 | + final Keyring keyring = StandardKeyrings.rawAesBuilder() |
| 54 | + .keyNamespace("ExampleKeyNamespace") |
| 55 | + .keyName("ExampleKeyName") |
| 56 | + .wrappingKey(cryptoKey).build(); |
| 57 | + |
| 58 | + // 4. Create an encryption context |
| 59 | + // |
| 60 | + // Most encrypted data should have an associated encryption context |
| 61 | + // to protect integrity. This sample uses placeholder values. |
| 62 | + // |
| 63 | + // For more information see: |
| 64 | + // blogs.aws.amazon.com/security/post/Tx2LZ6WBJJANTNW/How-to-Protect-the-Integrity-of-Your-Encrypted-Data-by-Using-AWS-Key-Management |
| 65 | + final Map<String, String> encryptionContext = Collections.singletonMap("ExampleContextKey", "ExampleContextValue"); |
| 66 | + |
| 67 | + // 5. Encrypt the data with the keyring and encryption context |
| 68 | + final AwsCryptoResult<byte[]> encryptResult = crypto.encrypt(EncryptRequest.builder() |
| 69 | + .keyring(keyring) |
| 70 | + .encryptionContext(encryptionContext) |
| 71 | + .plaintext(EXAMPLE_DATA).build()); |
| 72 | + final byte[] ciphertext = encryptResult.getResult(); |
| 73 | + |
| 74 | + // 6. Decrypt the data |
| 75 | + final AwsCryptoResult<byte[]> decryptResult = crypto.decrypt(DecryptRequest.builder() |
| 76 | + .keyring(keyring) |
| 77 | + .ciphertext(ciphertext).build()); |
| 78 | + |
| 79 | + // 7. Verify that the encryption context that was used to decrypt the data is the one that you expect. |
| 80 | + // This helps to ensure that the ciphertext that you decrypted was the one that you intended. |
| 81 | + // |
| 82 | + // When verifying, test that your expected encryption context is a subset of the actual encryption context, |
| 83 | + // not an exact match. When appropriate, the Encryption SDK adds the signing key to the encryption context. |
| 84 | + assert decryptResult.getEncryptionContext().get("ExampleContextKey").equals("ExampleContextValue"); |
| 85 | + |
| 86 | + // 8. Verify that the decrypted plaintext matches the original plaintext |
| 87 | + assert Arrays.equals(decryptResult.getResult(), EXAMPLE_DATA); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * In practice, this key would be saved in a secure location. |
| 92 | + * For this demo, we generate a new random key for each operation. |
| 93 | + */ |
| 94 | + private static SecretKey generateEncryptKey() { |
| 95 | + SecureRandom rnd = new SecureRandom(); |
| 96 | + byte[] rawKey = new byte[16]; // 128 bits |
| 97 | + rnd.nextBytes(rawKey); |
| 98 | + return new SecretKeySpec(rawKey, "AES"); |
| 99 | + } |
| 100 | +} |
0 commit comments