Cryptography

Cryptography

Cryptography est une bibliothèque développée activement qui fournit des recettes et des primitives cryptographiques. Elle supporte Python 2,6-2,7, Python 3.3+ et PyPy.

Cryptography est divisé en deux couches: recettes et matières dangereuses (“hazardous material” ou hazmat). La couche de recettes fournit une API simple pour un chiffrement symétrique correct et la couche hazmat fournit des primitives cryptographiques de bas niveau.

Installation

$ pip install cryptography

Exemple

Code exemple utilisant la recette de chiffrement symétrique de haut niveau:

from cryptography.fernet import Fernet
key = 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 est une autre bibliothèque, qui fournit des fonctions de hash sécurisées et différents algorithmes de chiffrement. Elle supporte Python version 2.1 à 3.3.

Installation

$ pip install pycrypto

Exemple

from Crypto.Cipher import AES
# Encryption
encryption_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.")

# Decryption
decryption_suite = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
plain_text = decryption_suite.decrypt(cipher_text)