Cryptography¶

Cryptography¶
Cryptography is an actively developedlibrary that provides cryptographic recipes and primitives. It supportsPython 2.6-2.7, Python 3.3+ and PyPy.
Cryptography is divided into two layers of recipes and hazardous materials(hazmat). The recipes layer provides simple API for proper symmetricencryption and the hazmat layer provides low-level cryptographic primitives.
Installation¶
$ pip install cryptography
Example¶
Example code using high level symmetric encryption recipe:
fromcryptography.fernetimportFernetkey=Fernet.generate_key()cipher_suite=Fernet(key)cipher_text=cipher_suite.encrypt(b"A really secret message. Not for prying eyes.")plain_text=cipher_suite.decrypt(cipher_text)
PyCrypto¶
PyCrypto is another library,which provides secure hash functions and various encryption algorithms. Itsupports Python version 2.1 through 3.3.
Installation¶
$ pip install pycrypto
Example¶
fromCrypto.CipherimportAES# Encryptionencryption_suite=AES.new('This is a key123',AES.MODE_CBC,'This is an IV456')cipher_text=encryption_suite.encrypt("A really secret message. Not for prying eyes.")# Decryptiondecryption_suite=AES.new('This is a key123',AES.MODE_CBC,'This is an IV456')plain_text=decryption_suite.decrypt(cipher_text)