52
52
EXAMPLE_DATA : bytes = b"Hello World"
53
53
54
54
55
- def generate_rsa_keyring ():
56
- """Creates a new RSA keyring along with generating new keys
55
+ def encrypt_and_decrypt_with_keyring ():
56
+ """Demonstrate an encrypt/decrypt cycle using a Raw RSA keyring.
57
57
58
- Usage: generate_rsa_keyring ()
58
+ Usage: encrypt_and_decrypt_with_keyring ()
59
59
"""
60
- # 1. The key namespace and key name are defined by you.
60
+ # 1. Instantiate the encryption SDK client.
61
+ # This builds the client with the REQUIRE_ENCRYPT_REQUIRE_DECRYPT commitment policy,
62
+ # which enforces that this client only encrypts using committing algorithm suites and enforces
63
+ # that this client will only decrypt encrypted messages that were created with a committing
64
+ # algorithm suite.
65
+ # This is the default commitment policy if you were to build the client as
66
+ # `client = aws_encryption_sdk.EncryptionSDKClient()`.
67
+ client = aws_encryption_sdk .EncryptionSDKClient (
68
+ commitment_policy = CommitmentPolicy .REQUIRE_ENCRYPT_REQUIRE_DECRYPT
69
+ )
70
+
71
+ # 2. Create encryption context.
72
+ # Remember that your encryption context is NOT SECRET.
73
+ # For more information, see
74
+ # https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
75
+ encryption_context : Dict [str , str ] = {
76
+ "encryption" : "context" ,
77
+ "is not" : "secret" ,
78
+ "but adds" : "useful metadata" ,
79
+ "that can help you" : "be confident that" ,
80
+ "the data you are handling" : "is what you think it is" ,
81
+ }
82
+
83
+ # 3. The key namespace and key name are defined by you.
61
84
# and are used by the Raw RSA keyring
62
85
# For more information, see
63
86
# https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-raw-rsa-keyring.html
64
87
key_name_space = "Some managed raw keys"
65
88
key_name = "My 4096-bit RSA wrapping key"
66
89
67
- # 2 . Generate a 4096-bit RSA key to use with your keyring.
90
+ # 4 . Generate a 4096-bit RSA key to use with your keyring.
68
91
ssh_rsa_exponent = 65537
69
92
bit_strength = 4096
70
93
key = rsa .generate_private_key (
@@ -86,7 +109,7 @@ def generate_rsa_keyring():
86
109
format = crypto_serialization .PublicFormat .SubjectPublicKeyInfo
87
110
)
88
111
89
- # 3 . Create a Raw RSA keyring
112
+ # 5 . Create a Raw RSA keyring
90
113
mat_prov : AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders (
91
114
config = MaterialProvidersConfig ()
92
115
)
@@ -103,64 +126,30 @@ def generate_rsa_keyring():
103
126
input = keyring_input
104
127
)
105
128
106
- return raw_rsa_keyring
107
-
108
-
109
- def encrypt_and_decrypt_with_keyring ():
110
- """Demonstrate an encrypt/decrypt cycle using a Raw RSA keyring.
111
-
112
- Usage: encrypt_and_decrypt_with_keyring()
113
- """
114
- # 1. Instantiate the encryption SDK client.
115
- # This builds the client with the REQUIRE_ENCRYPT_REQUIRE_DECRYPT commitment policy,
116
- # which enforces that this client only encrypts using committing algorithm suites and enforces
117
- # that this client will only decrypt encrypted messages that were created with a committing
118
- # algorithm suite.
119
- # This is the default commitment policy if you were to build the client as
120
- # `client = aws_encryption_sdk.EncryptionSDKClient()`.
121
- client = aws_encryption_sdk .EncryptionSDKClient (
122
- commitment_policy = CommitmentPolicy .REQUIRE_ENCRYPT_REQUIRE_DECRYPT
123
- )
124
-
125
- # 2. Create encryption context.
126
- # Remember that your encryption context is NOT SECRET.
127
- # For more information, see
128
- # https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
129
- encryption_context : Dict [str , str ] = {
130
- "encryption" : "context" ,
131
- "is not" : "secret" ,
132
- "but adds" : "useful metadata" ,
133
- "that can help you" : "be confident that" ,
134
- "the data you are handling" : "is what you think it is" ,
135
- }
136
-
137
- # 3. Create a Raw RSA keyring
138
- raw_rsa_keyring = generate_rsa_keyring ()
139
-
140
- # 4. Encrypt the data for the encryptionContext
129
+ # 6. Encrypt the data for the encryptionContext
141
130
ciphertext , _ = client .encrypt (
142
131
source = EXAMPLE_DATA ,
143
132
keyring = raw_rsa_keyring ,
144
133
encryption_context = encryption_context
145
134
)
146
135
147
- # 5 . Demonstrate that the ciphertext and plaintext are different.
136
+ # 7 . Demonstrate that the ciphertext and plaintext are different.
148
137
# (This is an example for demonstration; you do not need to do this in your own code.)
149
138
assert ciphertext != EXAMPLE_DATA , \
150
139
"Ciphertext and plaintext data are the same. Invalid encryption"
151
140
152
- # 6 . Decrypt your encrypted data using the same keyring you used on encrypt.
141
+ # 8 . Decrypt your encrypted data using the same keyring you used on encrypt.
153
142
plaintext_bytes , dec_header = client .decrypt (
154
143
source = ciphertext ,
155
144
keyring = raw_rsa_keyring
156
145
)
157
146
158
- # 7 . Demonstrate that the encryption context is correct in the decrypted message header
147
+ # 9 . Demonstrate that the encryption context is correct in the decrypted message header
159
148
# (This is an example for demonstration; you do not need to do this in your own code.)
160
149
for k , v in encryption_context .items ():
161
150
assert v == dec_header .encryption_context [k ], \
162
151
"Encryption context does not match expected values"
163
152
164
- # 8 . Demonstrate that the decrypted plaintext is identical to the original plaintext.
153
+ # 10 . Demonstrate that the decrypted plaintext is identical to the original plaintext.
165
154
# (This is an example for demonstration; you do not need to do this in your own code.)
166
155
assert plaintext_bytes == EXAMPLE_DATA
0 commit comments