Cryptography package in Python
In the following tutorial, we will learn about the cryptography package with the help of different examples in the Python programming language.
So, let's get begun.
Understanding the cryptography package
Cryptography is the practice of securing useful information during the transmission of data from one computer to another or storing data on a computer. Cryptography deals with the encryption of plain text into ciphertext and decryption of ciphertext into plain text. Python provides support to the cryptography package that allows us to encrypt and decrypt data. The fernet module of the cryptography package has core functions for the generation of the key, encryption of the plain text into ciphertext, and decryption of the ciphertext into plain text with the help of the encrypt() and decrypt() methods, respectively.
How to Install the Python cryptography package?
In order to install the Python package, we need 'pip', a framework to manage packages required to install the modules from the trusted public repositories. Once we have 'pip', we can install the cryptography library using the command from a Windows command prompt (CMD) or terminal as shown below:
Verifying the Installation
Once the module is installed, we can verify it by creating an empty Python program file and writing an import statement as follows:
Now, save the above file and execute it using the following command in a terminal:
If the above Python program file does not return any error, the module is installed properly. However, in the case where an exception is raised, try reinstalling the library, and it is also recommended to refer to the official documentation of the library.
Understanding Fernet (symmetric encryption)
Fernet is an implementation of symmetric (also called "secret key") authenticated cryptography. Fernet guarantees that we cannot manipulate or read the data encrypted using it any further without the key. Fernet also provides support for the implementation of key rotation through MultiFernet.
Let us now see the Fernet class and some methods in order to understand the concept of Cryptography in detail.
This class offers facilities for encryption as well as decryption. This class accepts the key as an input parameter. This key parameter is a URL-safe base64-encoded 32-byte key. This must be kept secret. Anyone with this key is able to create and read messages.
Now, before we see some of the methods of the Fernet class, let us consider a simple example demonstrating its use of it.
Encrypted Message: b'gAAAAABiZAj1lcmLXPbRJng9wxgowFB731MLUFu-nstC8Sdnzn24y_lhu_h1QmR5N68d_DdpH8mIGNF6Y-7PgSmgUYkxwouw7R80lWk1k9IPp7MiKtv5O3OWmG6gk4rK4k5iNzE5sPd-L_ns0Zn8nmG2Zr--QDUi2Q==' Decrypted Message: b'Hello, Students! Welcome to Python tutorial at Javatpoint.com'
In the above snippet of code, we have imported the Fernet class from the fernet module of the cryptography package. We have then used the class method called generate_key() in order to generate a key. We then used this key to convert the plain text into ciphertext with the help of the encrypt() method and printed the encrypted message. We then again used this key to convert the ciphertext into plain text with the help of the decrypt() method and printed the decrypted message for the user.
We can observe that the decrypted output has a 'b' in front of the original message, which signifies the byte format. However, we can remove this with the help of the decode() method while printing the original message. Let us consider the following example demonstrating the implementation of the decode() method.
Encrypted Message: b'gAAAAABiZrxrUg61HeVbOke04ftXTKnl17z3e8LjHfLMiCPQ2ZQ4Hz43bL1twbcS4cXcsclsUZKT-q3oyWVecKH39hgEgh1GQaB4E1gkMJr1Q6LbC93N97yeFq0kME_ttc-BVrXCGZBzNPRdW1zl45v7ow-d893kkMiab-y1OSMjbVFL9nK_iZE=' Decrypted Message: This example demonstrates the implementation of the decode() method.
In the above snippet of code, we have imported the Fernet class from the fernet module of the cryptography package. We have then used the class method called generate_key() in order to generate a key. We then used this key to convert the plain text into ciphertext with the help of the encrypt() method and printed the encrypted message. We then again used this key to convert the ciphertext into plain text with the help of the decrypt() method. At last, we have used the decode() method to convert the message from byte to string and printed it for the users.
Let us now understand the working of the method that we used earlier.