MCPcopy Create free account
hub / github.com/awsdocs/aws-doc-sdk-examples / encrypt

Method encrypt

python/example_code/kms/key_encryption.py:38–62  ·  view source on GitHub ↗

Encrypts text by using the specified key. :param key_id: The ARN or ID of the key to use for encryption. :param text: The text to encrypt. :return: The encrypted version of the text.

(self, key_id: str, text: str)

Source from the content-addressed store, hash-verified

36
37 # snippet-start:[python.example_code.kms.Encrypt]
38 def encrypt(self, key_id: str, text: str) -> bytes:
39 """
40 Encrypts text by using the specified key.
41
42 :param key_id: The ARN or ID of the key to use for encryption.
43 :param text: The text to encrypt.
44 :return: The encrypted version of the text.
45 """
46 try:
47 response = self.kms_client.encrypt(KeyId=key_id, Plaintext=text.encode())
48 print(
49 f"The string was encrypted with algorithm {response['EncryptionAlgorithm']}"
50 )
51 return response["CiphertextBlob"]
52 except ClientError as err:
53 if err.response["Error"]["Code"] == "DisabledException":
54 logger.error(
55 "Could not encrypt because the key %s is disabled.", key_id
56 )
57 else:
58 logger.error(
59 "Couldn't encrypt text. Here's why: %s",
60 err.response["Error"]["Message"],
61 )
62 raise
63
64 # snippet-end:[python.example_code.kms.Encrypt]
65

Callers 6

key_encryptionFunction · 0.95
kms_scenarioMethod · 0.80
encryptDataAsyncMethod · 0.80
putItemInTableMethod · 0.80
encryptDataMethod · 0.80
mainMethod · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected