Home > How to > Installing and Using PyCrypto on Windows 7

Installing and Using PyCrypto on Windows 7

I was looking for a simple way to encrypt and decrypt string, but wanted something a little more sophisticated than base64 encoding. I found this great post on how to implement PyCrypto AES Encryption in Python Using PyCrypto but it relies heavily on anonymous functions (lambda in Python jargon). Since I’m no very fond of lamda functions I decided to write a simple class to take care of encryption and decryption of strings.



1. Download pycrypto-2.6.tar.gz
2. Extract the content to a temporary folder (I used 7-Zip to unzip it)
3. Open a commad window
4. Move (cd) to the folder you unzipped pycrypto.
5. Type:

python setup.py build

6. Type

python setup.py install

7. Type

yolk -l

You should see pycrypto on the list

pyPdf           - 1.13         - active
pycrypto        - 2.6          - active
pyenchant       - 1.6.5        - active


Now use the following code to test the encryption and decryption algorithm (the gist for this code is at https://gist.github.com/3131325).

import os, base64
from Crypto.Cipher import AES
import hashlib

class Encryptor(object):
# the block size for the cipher object; must be 16, 24, or 32 for AES
block_size = 32
padding = '{'
def __init__(self, secret):
self.secret = hashlib.sha256(secret).digest()
self.clear_secret = secret
self.cipher = AES.new(self.secret)

def __pad__(self, clear_string):
return clear_string + (self.block_size - len(clear_string) % self.block_size) * self.padding
def encode(self, clear_string):
return base64.b64encode(self.cipher.encrypt(self.__pad__(clear_string)))
def decode(self, encoded_string):
return self.cipher.decrypt(base64.b64decode(encoded_string)).rstrip(self.padding)

if __name__ == '__main__':
secrets = ['Do or do not there is no try', 'I love Python !!!!']
for secret in secrets:
enc = Encryptor(secret)
cs = "#Almyqspnlg0"
print "Padding" , enc.__pad__(cs)
print "Secret" , enc.secret
print "Clear Secret" , enc.clear_secret
es = enc.encode(cs)
print "Encoded: " , es
ds = enc.decode(es)
print "Decoded: " , ds
print '-' *50

Your ouytput should be something like this:

Padding #Almyqspnlg0{{{{{{{{{{{{{{{{{{{{
Secret !m�$��%qS�;�>�q$��.�g8��6�c�*
Clear Secret Do or do not there is no try
Encoded:  7wygPnc+TOV1KiyrT5Aw8Ac0svYDXCvJIjvTS6hldws=
Decoded:  #Almyqspnlg0
Padding #Almyqspnlg0{{{{{{{{{{{{{{{{{{{{
Secret 6�XK˧�>n�_MT�������=�l�Ȝ�3"
Clear Secret I love Python !!!!
Encoded:  N8SQgE5YgcPus2P8D1IMCAl70JkroHqFnrXSX53tO1I=
Decoded:  #Almyqspnlg0
Categories: How to Tags: ,
  1. May 28, 2016 at 6:26 am

    win write : python setup.py build
    show me error: Unable to find vcvarsall.bat
    how to fix it ..

    • September 16, 2016 at 4:55 am

      Sorry I’ve never ran into an issue like that

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: