16

I'm writing a program in python 3 which needs encryption functions (at least aes and rsa). I've found PyCrypto which seems to work only on 2.x versions.

Is there any good tool available for python 3 or should I rather start translating my program to be compatible with python 2 (or any other solution) ?

Thank you


Update as mentioned below, PyCrypto is now available on py3k

Martin Trigaux
  • 5,311
  • 9
  • 45
  • 58

9 Answers9

17

PyCrypto 2.4.1 and later now work on Python 3.x (see changelog diff).

David Alber
  • 17,624
  • 6
  • 65
  • 71
dlitz
  • 5,679
  • 3
  • 19
  • 13
  • 3
    Instead of PyCrypto I would now recommend [PyCryptodome](http://pycryptodome.readthedocs.org/en/latest/) as a drop-in replacement for PyCrypto that's still being maintained actively. – sethmlarson Mar 25 '16 at 20:42
5

Although Python 3 itself is ready for primetime, the lack of libraries that support it is a hindrance. The best you can do is of course to help port PyCrypto to Python 3, although as it has a lot of C-extension modules that is probably not entirely trivial, and will be a couple of days work, I would think. Maybe the current maintainer is interested in porting or already half-way there, you should contact him and ask.

There is an rsa module written in Python which looks to have fairly clean and easily portable code, but for aes it seems like PyCrypto is the module to use. So it is probably easier to make your software run under Python 2 instead.

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
  • There is a discussion about the support of py3 in the mailing list. Even if they are talking about keeping backward compatibility (until py2.1) which seems hard to me. http://lists.dlitz.net/pipermail/pycrypto/2010q4/000305.html – Martin Trigaux Jan 03 '11 at 10:33
  • 1
    Heh. Python 2.1 support today seems rather crazy. :) It hasn't been supported since 2003. – Lennart Regebro Jan 03 '11 at 10:54
  • I agree with you :) Check this out : https://github.com/yorickdowne/pycrypto/tree/py3k somebody has started a fork of pycrypto for version 3 only – Martin Trigaux Jan 03 '11 at 11:00
5

Crytographic Libraries are mostly numeric calculations and I don't know why py3k versions are not available yet.

  1. Here is pyDES available for Python 3.
  2. Here is AES algorithm implementation in Python 3. Ported from this py2k version
  3. Here is RSA algorithm implementation in Python 3. I ported it from this py2k version.

Please use them with caution as they just development programs implemented following the algorithm text. (That is, I am not sure of the rigor in the original python2 version). Also, all of them are pure python libraries, they would be slower than anything written using C-extensions ( and perhaps that is the reason, why py3k versions are getting delayed).

Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131
4

i have written a wrapper library simple-crypt that provides encryption and decryption in python 3, delegating the work to pycrypto.

advantages of using this over pycrypto directly include:

  • much simpler interface:

    data = encrypt(password, text)
    text = decrypt(password, data).decode('utf8')
    
  • key expansion to make the use of passphrases more secure

  • use of an hmac to check for modification of the data

  • a versioned header that should allow me to switch the implementation to google's keyczar once that moves to python 3 (since that should be better maintained - i only wrote this out of apparent necessity).

you can install the package using:

easy_install simple_crypt

more info available on the github page for the project.

andrew cooke
  • 45,717
  • 10
  • 93
  • 143
3

Cryptography (documentation) claims to address multiple shortcomings of PyCrypto, M2Crypto, and PyOpenSSL. They don't mention simple-crypt, but a primary goal of the project is to provide high-level, safe, easy to use interfaces along with a set of lower-level interfaces (available from a 'hazmat' module) allowing finer-grained control for developers who understand the pitfalls.

Having not used the other libraries I can't comment on whether cryptography is better than the alternatives, but it has certainly met my needs so far.

Dave
  • 3,834
  • 2
  • 29
  • 44
2

I'm not aware of any reasonable crypto library for python (regardless of the version). Everything I'm aware of (including pycrypto) is just a toy. If you want to implement a serious application then you should look for a wrapper to a real library such as m2crypto. Pycrypto itself does follow many standards. Especially, RSA needs a good padding scheme to be secure. Since pycrypto is at least currently not using a padding, this makes its RSA implementation both rather insecure and incompatible with other crypto libraries.

Answer to Martins question: Obviously this question is open to a lot of opinions. One proposal would be to use Java instead of python. Java has a well defined cryptographic interface, and there are different providers that implement the interface. This has the rather big advantage, that one can implement a solution independent of the provider, so that one can easily switch between different providers. I personally like openssl, but I'm aware that it is rather difficult to use.

Accipitridae
  • 3,136
  • 19
  • 9
  • 1
    Thanks I didn't know m2crypto, looks interesting. But still seems to be compatible to python 2.x only. But if python is not good, which language do you advice to do an app using cryptology functions and nice in networks ? – Martin Trigaux Jan 04 '11 at 12:40
2

Charm is a framework for rapidly prototyping advanced cryptosystems from Johns Hopkins. It supports elliptic curve operations and pairing based cryptography.

The documentation is available on Charm-Crypto 0.50 documentation.

apaderno
  • 28,547
  • 16
  • 75
  • 90
Gary
  • 187
  • 3
  • 7
2

My Python wrapper around LibTomCrypt now supports Python 3, and it has both AES and RSA.

See: https://github.com/mikeboers/PyTomCrypt#readme

Mike Boers
  • 6,665
  • 3
  • 31
  • 40
1

pycrypto has Py3k branch (in https://github.com/dlitz/pycrypto/tree/py3k)

Taha Jahangir
  • 4,774
  • 2
  • 42
  • 49