36
36
* <p>
37
37
* Arguments:
38
38
* <ol>
39
- * <li>Key ARN: To find the Amazon Resource Name of your KMS customer master key (CMK),
40
- * see 'Viewing Keys' at http://docs.aws.amazon.com/kms/latest/developerguide/viewing-keys.html
41
- * <li>File Name
39
+ * <li>Key ARN: For help finding the Amazon Resource Name (ARN) of your KMS customer master
40
+ * key (CMK), see 'Viewing Keys' at http://docs.aws.amazon.com/kms/latest/developerguide/viewing-keys.html
41
+ *
42
+ * <li>Name of file containing plaintext data to encrypt
42
43
* </ol>
43
44
*
44
- * AWS Key Management Service (KMS) is highly available. However, some organizations want to decrypt
45
- * their data offline and independent of KMS. This sample demonstrates one way to do this.
45
+ * You might use AWS Key Management Service (KMS) for most encryption and decryption operations, but
46
+ * still want the option of decrypting your data offline independently of KMS. This sample
47
+ * demonstrates one way to do this.
46
48
*
47
- * This program generates an "escrowed" RSA key pair. It stores the private key in a secure offline
48
- * location, such as an offline HSM, and distributes the public key to their developers. It also
49
- * creates a KMS customer master key (CMK). The organization encrypts their data with both the
50
- * KMS CMK and the public key, so that either key alone could decrypt it.
49
+ * The sample encrypts data under both a KMS customer master key (CMK) and an "escrowed" RSA key pair
50
+ * so that either key alone can decrypt it. You might commonly use the KMS CMK for decryption. However,
51
+ * at any time, you can use the private RSA key to decrypt the ciphertext independent of KMS.
52
+ *
53
+ * This sample uses the JCEMasterKey class to generate a RSA public-private key pair
54
+ * and saves the key pair in memory. In practice, you would store the private key in a secure offline
55
+ * location, such as an offline HSM, and distribute the public key to your development team.
51
56
*
52
- * The team usually uses the KMS CMK for decryption. However, the organization can, at any time
53
- * use the private escrowed RSA key to decrypt the ciphertext independent of KMS.
54
57
*/
55
58
public class EscrowedEncryptExample {
56
59
private static PublicKey publicEscrowKey ;
57
60
private static PrivateKey privateEscrowKey ;
58
61
59
62
public static void main (final String [] args ) throws Exception {
60
- // In practice, the organization would distribute the public key.
61
- // For this demo, we generate a new random key for each operation.
63
+ // This sample generates a new random key for each operation.
64
+ // In practice, you would distribute the public key and save the private key in secure
65
+ // storage.
62
66
generateEscrowKeyPair ();
63
67
64
68
final String kmsArn = args [0 ];
@@ -71,16 +75,16 @@ public static void main(final String[] args) throws Exception {
71
75
}
72
76
73
77
private static void standardEncrypt (final String kmsArn , final String fileName ) throws Exception {
74
- // Standard practice: encrypt with the KMS CMK and the escrowed public key
78
+ // Encrypt with the KMS CMK and the escrowed public key
75
79
// 1. Instantiate the SDK
76
80
final AwsCrypto crypto = new AwsCrypto ();
77
81
78
82
// 2. Instantiate a KMS master key provider
79
83
final KmsMasterKeyProvider kms = new KmsMasterKeyProvider (kmsArn );
80
84
81
- // 3. Instantiate a JCE master key provider
82
- // Because the standard user does not have access to the private
83
- // escrow key, they pass in "null" for the private key parameter.
85
+ // 3. Instantiate a JCE master key provider
86
+ // Because the user does not have access to the private escrow key,
87
+ // they pass in "null" for the private key parameter.
84
88
final JceMasterKey escrowPub = JceMasterKey .getInstance (publicEscrowKey , null , "Escrow" , "Escrow" ,
85
89
"RSA/ECB/OAEPWithSHA-512AndMGF1Padding" );
86
90
@@ -100,16 +104,17 @@ private static void standardEncrypt(final String kmsArn, final String fileName)
100
104
}
101
105
102
106
private static void standardDecrypt (final String kmsArn , final String fileName ) throws Exception {
103
- // Standard practice: enncrypt with the KMS CMK and the escrow public key
107
+ // Decrypt with the KMS CMK and the escrow public key. You can use a combined provider,
108
+ // as shown here, or just the KMS master key provider.
104
109
105
110
// 1. Instantiate the SDK
106
111
final AwsCrypto crypto = new AwsCrypto ();
107
112
108
113
// 2. Instantiate a KMS master key provider
109
114
final KmsMasterKeyProvider kms = new KmsMasterKeyProvider (kmsArn );
110
115
111
- // 3. Instantiate a JCE master key provider
112
- // Because the standard user does not have access to the private
116
+ // 3. Instantiate a JCE master key provider
117
+ // Because the user does not have access to the private
113
118
// escrow key, they pass in "null" for the private key parameter.
114
119
final JceMasterKey escrowPub = JceMasterKey .getInstance (publicEscrowKey , null , "Escrow" , "Escrow" ,
115
120
"RSA/ECB/OAEPWithSHA-512AndMGF1Padding" );
@@ -129,14 +134,14 @@ private static void standardDecrypt(final String kmsArn, final String fileName)
129
134
}
130
135
131
136
private static void escrowDecrypt (final String fileName ) throws Exception {
132
- // The organization can decrypt the stream using only the private escrow key.
133
- // This method does not call KMS.
137
+ // You can decrypt the stream using only the private key.
138
+ // This method does not call KMS.
134
139
135
140
// 1. Instantiate the SDK
136
141
final AwsCrypto crypto = new AwsCrypto ();
137
142
138
143
// 2. Instantiate a JCE master key provider
139
- // This method call uses the escrowed private key
144
+ // This method call uses the escrowed private key, not null
140
145
final JceMasterKey escrowPriv = JceMasterKey .getInstance (publicEscrowKey , privateEscrowKey , "Escrow" , "Escrow" ,
141
146
"RSA/ECB/OAEPWithSHA-512AndMGF1Padding" );
142
147
0 commit comments