Python 2.5 and encryption — PyCrypto under Windows

[Edit: 8-13-08] – Upon request I have provided a link to an installer for PyCrypto-2.0.1 that is compiled for Python 2.5. You can download it here . I didn’t post it originally because I was not sure how long I could provide a link for. This one should be good for about a year … 🙂 .

Note: The steps listed here will only work on Python 2.5 and above as that is when they added support for allowing MinGW to compile code

I mentioned in a previous post that I was looking for a way to get public/private key encryption in Python and I was having a bugger of a time until I found ezPyCrypto. I don’t know if I have mentioned this before, but I have been an Linux user for about 4 years now. Specifically Gentoo Linux ,and I would never run another OS now that I have been a Linux user. At any rate that’s not the point of this post. Since the script I was developing had to run on Windows I needed to install PyCrypto (it’s a dependency for ezPyCrypto). Well this would be trivial except that PyCrypto has C code that needs to be compiled. There are binary builds on the PyCrypto site for both Python 2.3 and 2.4, but not 2.5 which I am running. I wasn’t going to let this stop me however. The rest of this post will explain what you need to do in order to get PyCrypto working under Windows with Python 2.5. After completing this you will also be able to build your own installer that you could package with your scripts to let people who don’t want to install a C compiler run your code 🙂

The first thing you are going to want to do is install MinGW . MinGW will give you a nice C and C++ compiler for Windows. Just follow the instructions on their site and you will be good to go. After you have MinGW installed you will probably want to add entries to the Windows PATH variable so that when you are in a terminal you will be able to directly access MinGW. [As a note I still run Windows XP so these instructions will be done with that in mind] Doing this is quite simple:

  1. Right Click My Computer –> Properties –> Advanced –> Environment Variables.
  2. You will see User and System variables. If you want all users to use MinGW edit the Path entry in there. Otherwise add a Path variable under User variables.
  3. Go to the Path variable from step 2. Add an entry to the Path that points to MinGW’s bin directory. For me this was “C:\MinGW\bin” but it all depends on where you installed MinGW. Note: Entires in Windows Environment variables are separated by ‘;’
  4. Click ok a bunch of times

While you are changing environment variables you will want to add the main directory for Python to your path. For me this was: “C:\Python25”. This made my final User Path entry: “C:\MinGW\bin;C:\Python25”

Now that you are done changing environment variables you will want to reboot your computer so that Windows gets your changes. Really… you will regret it if you don’t reboot!

Now download PyCrypto and unpackage it. This turned out to be problem enough because not many people keep programs around that open .tar.gz files. Since am a Linux user I just unpacked them in Linux and transferred them to Windows (I run Windows XP under VirtualBox ). However, if that is not an option you can download and install IZarc Archiver. IZarc has been my favorite [Windows] extraction tool for a long time and it supports just about every archive format imaginable.I would recommend extracting the contents of PyCrypto to your desktop. Now is when the first starts!

First, open up a terminal. We will only be using a few commands and you don’t have to be a terminal wiz kid to do this. I normally open a terminal by going: Start –> Run –> “cmd” –> Press Enter

Now that you have a terminal up you want get to the PyCrypto directory. To move between directories in the terminal we use the “cd” command.


So, for example here is the command I used to get to where I had PyCrypto:

cd C:\Documents and Settings\Jinto\Desktop\pycrypto-2.0.1

Of course that command will only work for you if your user name is Jinto and you extracted it to the desktop exactly like I did 😉 So modify it to fit your needs. Once you are in the pycrypto directory do a quick:

dir /w

To make sure you see files. Specially make sure is there. We need to run that setup script and tell it to build the libraries. To do this issue the following command:

python build -c mingw32

If all goes well it should build without any troubles and all that is left is to install it by issuing the following command:

python install

You should be all set to use PyCrypto now, or to install ezPyCrypto which uses PyCrypto. As a bonus feature if you are looking to make an executable installer for PyCrypto you can run the following command:

python bdist_wininst

This will leave you with a .exe file located in the “dist” folder.

I hope this helps everyone get PyCrypto running under Windows. For those of you Linux users who think I am leaving you out check your distro specific respository system they probably already have a package for PyCrypto!


12 Responses to Python 2.5 and encryption — PyCrypto under Windows

  1. bob says:

    Excellent Tut! Worked like a charm… After doing all that though.. I read the part about having a installer exe… wish you could have just linked that and let me use that! 🙂

  2. jintoreedwine says:

    @bob : The problem with linking the .exe was that I did not have a good place store it to link to 🙂 I thought about throwing something together, but currently the only place I can store files and link to is my college, and I will only have access there for another year. I will consider linking there for the time being though. Either way, I’m glad you were able to get this working.

  3. will says:

    nice! thanks!

  4. vishalsodani says:

    thanks, gud tutorial.helped me out

  5. beanluc says:

    You don’t have to reboot after setting PATH.

    You can just
    SET %PATH = %PATH%;C:\MinGW\bin

    Doing it the first way sets the path permanently.
    Doing it the second way sets the path immediately.
    If you do both, you don’t have to reboot before enjoying your new PATH.

  6. beanluc says:

    Also: If you already have MSYS, you can run tar -xzvf there. The rigamaroles of moving untarred files between systems or of installing a special tgz-aware unarchiver aren’t necessary either.

    • beanluc says:

      MSYS is one of the options you can choose when installing MinGW. Basically it gives you Windows versions of bash and many GNU utils, including tar and gz.

  7. […] A C++ compiler. I already have Visual Studio on my pc so I don´t need to install anything. If you don´t have one you can use Mingw32, for details take a look at Python 2.5 and encryption — PyCrypto under Windows […]

  8. Cathal says:

    I followed these instructions to build for python 3.3 using the latest MinGW, and hit an error:
    C:\MinGW\bin\gcc.exe -mno-cygwin -mdll -Wall -std=c99 -03 -fomit-frame-pointer -Isrc/ -IC:\Python33\include -IC:\Python33\include -c src/winrand.c -o build\temp.cc1.exe: error: unrecognised command line option `-mno-cygwin`
    error: command `gcc` failed with exit status 1

    To fix this error, I had to edit the “” file in \Lib\distutils\ like so:
    # self.set_executables(compiler=’gcc -mno-cygwin -O -Wall’,
    # compiler_so=’gcc -mno-cygwin -mdll -O -Wall’,
    # compiler_cxx=’g++ -mno-cygwin -O -Wall’,
    # linker_exe=’gcc -mno-cygwin’,
    # linker_so=’%s -mno-cygwin %s %s’
    # % (self.linker_dll, shared_option,
    # entry_point))
    self.set_executables(compiler=’gcc -O -Wall’,
    compiler_so=’gcc -mdll -O -Wall’,
    compiler_cxx=’g++ -O -Wall’,
    linker_so=’%s %s %s’
    % (self.linker_dll, shared_option,

    Removing the references to the outdated “-mno-cygwin” option fixed the problem; apparently it’s unnecessary.

    PyCrypto then built successfully, and while the tests failed with one obscure error, it seems to work fine so far.

    Thanks! Saved me from downloading 4.3GB of useless VC++ closed source crap, using MingGW was far more comfortable. 🙂

  9. I actually want to know precisely why you titled this specific post, “Python 2.
    5 and encryption – PyCrypto under Windows Jinto Reedwine’s Blog”. In either case I actually admired the post!Thanks,Desiree

    • jintoreedwine says:

      To be honest, the only reason I titled it that way is because when I first wrote this post that was the most recent version of Python. I didn’t foresee it being so useful in the future… 🙂 .

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: