暗号

暗号

Cryptography は、暗号レシピとプリミティブを提供する、積極的に開発されたライブラリです。 Python 2.6-2.7、Python 3.3+、PyPyをサポートしています。

暗号は、レシピと危険物(ハザード)の2つの層に分かれています。 レシピ層は適切な共通鍵暗号化のための簡単なAPIを提供し、ハザード層は低レベルの暗号プリミティブを提供します。

インストール

$ pip install cryptography

高レベルの共通鍵暗号レシピを使用したコード例:

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 は安全なハッシュ関数とさまざまな暗号化アルゴリズムを提供する別のライブラリです。 Python 2.1から3.3までのバージョンをサポートしています。

インストール

$ pip install pycrypto

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)