Skip to content

Instantly share code, notes, and snippets.

@fmartingr
Last active December 22, 2015 08:46
Show Gist options
  • Save fmartingr/4c38fbd3608bf33c88b3 to your computer and use it in GitHub Desktop.
Save fmartingr/4c38fbd3608bf33c88b3 to your computer and use it in GitHub Desktop.
import base64
from Crypto.Cipher import AES
class AESCipher:
def __init__(self, key, iv, block_size=16):
self.block_size = block_size
self._cipher = AES.new(key, AES.MODE_CBC, iv)
def _get_padding(self, s):
length = self.block_size - (len(s) % self.block_size)
return s + chr(length)*length
def encrypt(self, raw):
return base64.b64encode(self._cipher.encrypt(self._get_padding(raw)))
def decrypt(self, enc):
return self._cipher.decrypt(base64.b64decode(enc)).rstrip(self.padding)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment