PyCrypto - The Python Cryptography Toolkit is a package that contains various cryptographic modules for the Python programming language.
Warning: PyCrypto seems not maintained anymore
It seems that the PyCrypto project has not been maintained since 2014. The developers are not doing any activity on the official repository during these years and they are not providing support about issues. This is why this post suggests to replace it with the PyCryptodome library, which still creates a Crypto
package with an almost identical API and can be used with most software, although there are some exceptions. For more details about compatibility between these two packages visit this page.
About PyCrypto
From the PyCrypto PyPi page:
This is a collection of both secure hash functions (such as SHA256 and RIPEMD160), and various encryption algorithms (AES, DES, RSA, ElGamal, etc.). The package is structured to make adding new modules easy.
And from the PyCrypto GitHub repository:
One possible application of the modules is writing secure administration tools. Another application is in writing daemons and servers. Clients and servers can encrypt the data being exchanged and mutually authenticate themselves; daemons can encrypt private data for added security. Python also provides a pleasant framework for prototyping and experimentation with cryptographic algorithms; thanks to its arbitrary-length integers, public key algorithms are easily implemented.
Official resources
- PyCrypto homepage
- PyCrypto alternative homepage
- PyCrypto API documentation
- PyCrypto PyPi page
- PyCrypto GitHub repository
Installation
As the PyCrypto PyPi page suggests, an easy way to install PyCrypto is by using the following command.
pip install pycrypto
Examples
From the PyCrypto GitHub repository:
An example usage of the SHA256 module is:
>>> from Crypto.Hash import SHA256 >>> hash = SHA256.new() >>> hash.update('message') >>> hash.digest() '\xabS\n\x13\xe4Y\x14\x98+y\xf9\xb7\xe3\xfb\xa9\x94\xcf\xd1\xf3\xfb"\xf7\x1c\xea\x1a\xfb\xf0+F\x0cm\x1d'
An example usage of an encryption algorithm (AES, in this case) is:
>>> from Crypto.Cipher import AES >>> obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456') >>> message = "The answer is no" >>> ciphertext = obj.encrypt(message) >>> ciphertext '\xd6\x83\x8dd!VT\x92\xaa`A\x05\xe0\x9b\x8b\xf1' >>> obj2 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456') >>> obj2.decrypt(ciphertext) 'The answer is no'