Last active
December 22, 2015 08:46
-
-
Save fmartingr/4c38fbd3608bf33c88b3 to your computer and use it in GitHub Desktop.
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 | |
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