#!/usr/bin/env python | |
import base64 | |
from Crypto import Random | |
from Crypto.Cipher import AES | |
BS = 16 | |
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS) | |
unpad = lambda s : s[0:-ord(s[-1])] | |
class AESCipher: | |
def __init__( self, key ): | |
self.key = key | |
def encrypt( self, raw ): | |
raw = pad(raw) | |
iv = Random.new().read( AES.block_size ) | |
cipher = AES.new( self.key, AES.MODE_CBC, iv ) | |
return base64.b64encode( iv + cipher.encrypt( raw ) ) | |
def decrypt( self, enc ): | |
enc = base64.b64decode(enc) | |
iv = enc[:16] | |
cipher = AES.new(self.key, AES.MODE_CBC, iv ) | |
return unpad(cipher.decrypt( enc[16:] )) | |
cipher = AESCipher('mysecretpassword') | |
encrypted = cipher.encrypt('Secret Message A') | |
decrypted = cipher.decrypt(encrypted) | |
print encrypted | |
print decrypted |
pycrypto==2.6.1 |
This comment has been minimized.
This comment has been minimized.
This only works because the 'mysecretpassword' is 16 bytes. If it were a different (not dividable by 16) amount of bytes you'd get |
This comment has been minimized.
This comment has been minimized.
Very minor changes to make it python 3 compatible https://gist.github.com/mguezuraga/257a662a51dcde53a267e838e4d387cd |
This comment has been minimized.
This comment has been minimized.
lambda removed(pep 8 support) |
This comment has been minimized.
This comment has been minimized.
In Python 3 using the modifications of Craz1k0ek it still doesn't work with Unicode. For example the input Edit: found a working version: https://stackoverflow.com/a/44212550 |
This comment has been minimized.
This comment has been minimized.
i think this is aes 128, we have a standard blocksize of 16 bytes (128bit) |
This comment has been minimized.
This comment has been minimized.
i can't seem to find how to do aes256 |
This comment has been minimized.
This comment has been minimized.
Please provide the JAVA code equivalent to above which is in python. |
This comment has been minimized.
This comment has been minimized.
Key size decides if it is aes 256 or 128, block size is 16 in both of em |
This comment has been minimized.
AWESOMESAUCE.
like a boss