Skip to content

Instantly share code, notes, and snippets.

@wesleyit
Created November 19, 2019 21:20
Show Gist options
  • Save wesleyit/ae44531d79fef0cc521afe52cca7508e to your computer and use it in GitHub Desktop.
Save wesleyit/ae44531d79fef0cc521afe52cca7508e to your computer and use it in GitHub Desktop.
Easy functions to encrypt and decrypt using Boto3
import base64
import boto3
KEY = '12345678-abcd-efgh-ijkl-1234567890'
PROFILE = 'my-security-profile'
session = boto3.session.Session(profile_name=PROFILE)
def encrypt(secret, session=session, alias=KEY):
client = session.client('kms')
ciphertext = client.encrypt(KeyId=alias, Plaintext=secret.encode())
return base64.b64encode(ciphertext['CiphertextBlob']).decode()
def decrypt(secret, session=session):
client = session.client('kms')
plaintext = client.decrypt(CiphertextBlob=bytes(base64.b64decode(secret)))
return plaintext['Plaintext'].decode()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment