Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 37ce803

Browse files
committedApr 23, 2024
updated ../../src/keyrings/raw_rsa_keyring_example.py
1 parent 6b88b54 commit 37ce803

File tree

1 file changed

+34
-45
lines changed

1 file changed

+34
-45
lines changed
 

‎examples/src/keyrings/raw_rsa_keyring_example.py

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,42 @@
5252
EXAMPLE_DATA: bytes = b"Hello World"
5353

5454

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.
5757
58-
Usage: generate_rsa_keyring()
58+
Usage: encrypt_and_decrypt_with_keyring()
5959
"""
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.
6184
# and are used by the Raw RSA keyring
6285
# For more information, see
6386
# https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-raw-rsa-keyring.html
6487
key_name_space = "Some managed raw keys"
6588
key_name = "My 4096-bit RSA wrapping key"
6689

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.
6891
ssh_rsa_exponent = 65537
6992
bit_strength = 4096
7093
key = rsa.generate_private_key(
@@ -86,7 +109,7 @@ def generate_rsa_keyring():
86109
format=crypto_serialization.PublicFormat.SubjectPublicKeyInfo
87110
)
88111

89-
# 3. Create a Raw RSA keyring
112+
# 5. Create a Raw RSA keyring
90113
mat_prov: AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders(
91114
config=MaterialProvidersConfig()
92115
)
@@ -103,64 +126,30 @@ def generate_rsa_keyring():
103126
input=keyring_input
104127
)
105128

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
141130
ciphertext, _ = client.encrypt(
142131
source=EXAMPLE_DATA,
143132
keyring=raw_rsa_keyring,
144133
encryption_context=encryption_context
145134
)
146135

147-
# 5. Demonstrate that the ciphertext and plaintext are different.
136+
# 7. Demonstrate that the ciphertext and plaintext are different.
148137
# (This is an example for demonstration; you do not need to do this in your own code.)
149138
assert ciphertext != EXAMPLE_DATA, \
150139
"Ciphertext and plaintext data are the same. Invalid encryption"
151140

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.
153142
plaintext_bytes, dec_header = client.decrypt(
154143
source=ciphertext,
155144
keyring=raw_rsa_keyring
156145
)
157146

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
159148
# (This is an example for demonstration; you do not need to do this in your own code.)
160149
for k, v in encryption_context.items():
161150
assert v == dec_header.encryption_context[k], \
162151
"Encryption context does not match expected values"
163152

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.
165154
# (This is an example for demonstration; you do not need to do this in your own code.)
166155
assert plaintext_bytes == EXAMPLE_DATA

0 commit comments

Comments
 (0)