So I needed pycrypto for some AES bits I was doing.

Unfortunately I was playing in windows so it wasn’t as easy as apt-get install.

Download pycrypto from here. Once downloaded extract it and then you’ll need to run ‘python setup.py build’. Unfortunately python 2.7.5 looks for visual studio 2007 so you need to trick it into using the version of visual studio you having installed, in my case it’s VS2010.

1
2
3
SET VS90COMNTOOLS=%VS100COMNTOOLS%
python setup.py build
python setup.py install

Simple when you know how….

Example of AES encode and decode (note this isn’t that secure as we’re not using a IV anywhere, it’s just a dmeo)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from Crypto.Cipher import AES
key="YELLOW SUBMARINE" #needs to be 16bytes
plaintext="Goodness great balls of fire!"

#Encode (string and key needs to be multiple of 16bytes)
padding = 16 - (len(plaintext) % 16)
plaintext += chr(3)*padding # add 'End of Text' padding to plaintext

encryptor = AES.new(key, AES.MODE_ECB)
ciphertext = encryptor.encrypt(plaintext)
print ciphertext

#Decode
decryptor = AES.new(key, AES.MODE_ECB)
plaintext = decryptor.decrypt(ciphertext)
plaintext = plaintext.strip(chr(3)) # remove 'End of Text' padding
print plaintext

Leave a Reply