Skip to content

Instantly share code, notes, and snippets.

@readytheory
Created February 12, 2022 20:06
Show Gist options
  • Save readytheory/31157cca9f3c2aa02e0f0c4cef360f8a to your computer and use it in GitHub Desktop.
Save readytheory/31157cca9f3c2aa02e0f0c4cef360f8a to your computer and use it in GitHub Desktop.
import base64
import os
import boto3
kms = boto3.client('kms')
key_id = 'alias/kkey'
filename = "./example_b64_encoded_encrypted_file.txt"
kommand = f"aws kms encrypt --key-id {key_id} --plaintext fileb://{__file__} --query CiphertextBlob > {filename}"
print(f"{kommand=}", flush=True)
if os.system(kommand) != 0:
print("something went wrong")
exit(1)
# open the file in binary mode so that base64 function can turn it to a binary chunk, which
with open(filename, "rb") as r:
encoded = r.read()
bincontent = base64.b64decode(encoded)
decrypt_result = kms.decrypt(CiphertextBlob=bincontent)
print(f"this is the decrypted stuffs:\n {decrypt_result['Plaintext']}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment