Cryptography

https://d33wubrfki0l68.cloudfront.net/f1a879211bca220b40f3855a91acd2a079ae22a9/9dec9/_images/33907152824_bf91078cc1_k_d.jpg

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 a simple API for proper symmetricencryption and the hazmat layer provides low-level cryptographic primitives.

Installation

  1. $ pip install cryptography

Example

Example code using high level symmetric encryption recipe:

  1. from cryptography.fernet import Fernet
  2. key = Fernet.generate_key()
  3. cipher_suite = Fernet(key)
  4. cipher_text = cipher_suite.encrypt(b"A really secret message. Not for prying eyes.")
  5. plain_text = cipher_suite.decrypt(cipher_text)

GPGME bindings

The GPGME Python bindings provide Pythonic access to GPG Made Easy, a C API for the entire GNU Privacy Guard suite of projects, including GPG, libgcrypt, and gpgsm (the S/MIME engine). It supports Python 2.6, 2.7, 3.4, and above. Depends on the SWIG C interface for Python as well as the GnuPG software and libraries.

A more comprehensive GPGME Python Bindings HOWTO is available with the source, and an HTML version is available at http://files.au.adversary.org. Python 3 sample scripts from the examples in the HOWTO are also provided with the source and are accessible at gnupg.org.

Available under the same terms as the rest of the GnuPG Project: GPLv2 and LGPLv2.1, both with the “or any later version” clause.

Installation

Included by default when compiling GPGME if the configure script locates a supported python version (which it will if it’s in $PATH during configuration).

Example

  1. import gpg
  2.  
  3. # Encryption to public key specified in rkey.
  4. a_key = input("Enter the fingerprint or key ID to encrypt to: ")
  5. filename = input("Enter the filename to encrypt: ")
  6. with open(filename, "rb") as afile:
  7. text = afile.read()
  8. c = gpg.core.Context(armor=True)
  9. rkey = list(c.keylist(pattern=a_key, secret=False))
  10. ciphertext, result, sign_result = c.encrypt(text, recipients=rkey,
  11. always_trust=True,
  12. add_encrypt_to=True)
  13. with open("{0}.asc".format(filename), "wb") as bfile:
  14. bfile.write(ciphertext)
  15. # Decryption with corresponding secret key
  16. # invokes gpg-agent and pinentry.
  17. with open("{0}.asc".format(filename), "rb") as cfile:
  18. plaintext, result, verify_result = gpg.Context().decrypt(cfile)
  19. with open("new-{0}".format(filename), "wb") as dfile:
  20. dfile.write(plaintext)
  21. # Matching the data.
  22. # Also running a diff on filename and the new filename should match.
  23. if text == plaintext:
  24. print("Hang on ... did you say *all* of GnuPG? Yep.")
  25. else:
  26. pass

PyCrypto

PyCrypto is another library,which provides secure hash functions and various encryption algorithms. Itsupports Python version 2.1 through 3.3.

Installation

  1. $ pip install pycrypto

Example

  1. from Crypto.Cipher import AES
  2. # Encryption
  3. encryption_suite = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
  4. cipher_text = encryption_suite.encrypt("A really secret message. Not for prying eyes.")
  5.  
  6. # Decryption
  7. decryption_suite = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
  8. plain_text = decryption_suite.decrypt(cipher_text)

原文: https://docs.python-guide.org/scenarios/crypto/