Skip to content

Instantly share code, notes, and snippets.

@a1994sc
Last active August 19, 2016 00:05
Show Gist options
  • Save a1994sc/867c95fc1047acfdb9cf648e42d0c4b4 to your computer and use it in GitHub Desktop.
Save a1994sc/867c95fc1047acfdb9cf648e42d0c4b4 to your computer and use it in GitHub Desktop.
from base64 import b64decode, b64encode
from Crypto.Cipher import AES
int_offset = 96
def pad(msg):
int_pad = abs((len(msg) % 16) - 16)
if int_pad != 16:
msg += chr(int_pad + int_offset) * int_pad
return msg
def unpad(str_msg):
chr_last = str_msg[(len(str_msg)-1):len(str_msg)]
int_remove = ord(chr_last) - int_offset
return str_msg[:(len(str_msg)-int_remove)]
def encryptMSG(str_key, str_IV, str_msg):
str_pad = pad(str_msg)
ea = AES.new(str_key, AES.MODE_CBC, str_IV)
ct = ea.encrypt(str_pad)
return b64encode(ct)
def decryptMSG(str_key, str_IV, byt_msg):
str_msg = b64decode(byt_msg)
ea = AES.new(str_key, AES.MODE_CBC, str_IV)
return unpad(ea.decrypt(str_msg).decode("utf-8"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment