Last active
December 15, 2015 03:06
-
-
Save cipriantarta/36d08aa3ccb9380cdc99 to your computer and use it in GitHub Desktop.
encrypt_decrypt.py
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 | |
from Crypto.Cipher import AES | |
from Crypto import Random | |
def encrypt_data(data): | |
pad = lambda s: s + (AES.block_size - len(s) % AES.block_size) * chr(AES.block_size - len(s) % AES.block_size) | |
message = pad('{}'.format(data)) | |
iv = Random.new().read(AES.block_size) | |
obj = AES.new("<<<SECRET KEY>>>", AES.MODE_CBC, iv) | |
return base64.b64encode(iv + obj.encrypt(message)).decode('utf-8') | |
def decrypt_data(data): | |
unpad = lambda s: s[:-ord(s[len(s) - 1:])] | |
message = base64.b64decode(data) | |
iv = message[:16] | |
chiper = AES.new("<<<SECRET KEY>>>", AES.MODE_CBC, iv) | |
return unpad(chiper.decrypt(message[16:])).decode('utf-8').split('|') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment