Created
November 19, 2019 21:20
-
-
Save wesleyit/ae44531d79fef0cc521afe52cca7508e to your computer and use it in GitHub Desktop.
Easy functions to encrypt and decrypt using Boto3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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