32

I am looking for a (preferably pure) python library to do AES 256 encryption and decryption.

This library should support the CBC cipher mode and use PKCS7 padding according to the answer to an earlier question of mine.

The library should at least work on Mac OS X (10.4) and Windows XP. Ideally just by dropping it into the source directory of my project. I have seen this by Josh Davis, but am not sure about how good it is and if it does the required CBC cipher mode... Scanning the source suggests it doesn't

Community
  • 1
  • 1
Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
  • 3
    As of March 2019: PyCrypto is no longer maintained (see https://github.com/dlitz/pycrypto/issues/173 and numerous other issues). Both [`pycryptodome`](https://github.com/Legrandin/pycryptodome) and [`cryptography`](https://github.com/pyca/cryptography/) are actively maintained. Both have C dependencies. – Brad Solomon Mar 10 '19 at 20:38

5 Answers5

13

PyCrypto should be the one for you.

Edit 02/10/2020: unfortunately I cannot delete this post, since it's the accepted answer. As people pointed out in the comments, this library is not mantained anymore and probably also vulnerable from a security point of view. So please, take a look to the below answers instead.

Community
  • 1
  • 1
friol
  • 6,996
  • 4
  • 44
  • 81
  • There is also a wrapper called ezPyCrypto. Alas, both are in a sad state, as PyCrypto is currently switching maintainers and the new guy won't release. – Daren Thomas Oct 08 '08 at 18:35
  • PyCrypto has since had a release 2.1.0. – Craig McQueen Jan 03 '10 at 03:56
  • 4
    PyCrypto requires GMP which requires Make and GCC and probably some other libraries and programs – Seth Jul 30 '11 at 16:13
  • I don't see PCKS7 padding in PyCrypto. OTOH, the PKCS7 pad/strip code in SlowAES would be easy to add. – JLundell Mar 19 '12 at 15:15
  • 3
    PyCrypto now appears to be dead (404 on www.pycrypto.org). – MarioVilas Jul 01 '12 at 20:42
  • 4
    @MarioVilas http://pycrypto.org now redirects to https://www.dlitz.net/software/pycrypto. – Matt Ball Sep 01 '12 at 03:22
  • 6
    Please consider revising this answer. As of March 2019: PyCrypto is no longer maintained (see https://github.com/dlitz/pycrypto/issues/173 and numerous other issues). Both [`pycryptodome`](https://github.com/Legrandin/pycryptodome) and [`cryptography`](https://github.com/pyca/cryptography/) are actively maintained. Both have C dependencies. – Brad Solomon Mar 10 '19 at 20:38
  • 4
    This should be removed. This package is listed as having unpatched vulnerabilities by Github's scans. There is no excuse to keep on pointing people towards old libraries, especially not in the security space. – JL Peyret Mar 23 '19 at 17:14
13

https://github.com/caller9/pythonaes

That is pure python with PKCS7 padding. Supports CBC, CFB, and OFB modes.

The problem is that python is not super fast for this type of thing. The code from serprex's fork is a little bit inscrutable, but much faster than mine due to using all kinds of tricks to squeeze every last bit of speed out of Python.

Really though, the best libraries for this are compiled and hook into SSE/MMX stuff.

Also Intel is baking in AES instructions since the Core(tm) line of chips.

I wrote my version to get a true pure Python version out there to be able to run on any architecture, cross-platform, and with 3.x as well as 2.7.

caller9
  • 2,207
  • 1
  • 18
  • 11
5

Since I found this question when searching for the same thing I would like to add another one to the list:

SlowAEShttp://code.google.com/p/slowaes/
It's a development of Josh Davis' code, with the help of some other people. It seems to work fine.

Blixt
  • 49,547
  • 13
  • 120
  • 153
4

How about ncrypt? It's not pure python but it is a lot faster as a result. It is basically a nice python wrapper on OpenSSL, so you know there's quality code behind it.

Dan Lenski
  • 76,929
  • 13
  • 76
  • 124
1

PyCrypto is not clearly pythonic so you can get troubles compiling it on some platforms (AIX, HP-UX etc)

Denis Barmenkov
  • 2,276
  • 1
  • 15
  • 20