Python cryptography example – Python Cryptography with Example

Cryptography

Python cryptography example: The art of communicating between two people via coded messages is known as cryptography. The science of cryptography originated with the primary goal of providing security to secret messages sent from one party to another.

Cryptography is described as the art and science of hiding a communication in order to introduce privacy and secrecy in information security.

Powerful encryption technology is also required to ensure that the encrypted text is considerably more difficult to hack through and that your information does not fall into the wrong hands.

Modern Cryptography’s Characteristics

Cryptography python example: The following are the fundamental characteristics of modern cryptography:

  • It works with bit sequences.
  • Cryptography secures information by the application of mathematical algorithms.
  • It necessitates the participation of parties interested in establishing a secure communication channel in order to achieve privacy.

Importance of Cryptography

The following are some of the reasons why cryptography is important:

  • Protecting sensitive information and communication data from unauthorized people and preventing them from gaining access to it.
  • Have digital signatures to help protect vital information from frauds.
  • It is also critical to protect the information’s integrity.

Before going to the implementation part we should first install the cryptography module

Installation

pip install cryptography

Output:

Collecting cryptography
Downloading cryptography-36.0.1-cp36-abi3-manylinux_2_24_x86_64.whl (3.6 MB)
|████████████████████████████████| 3.6 MB 4.9 MB/s 
Requirement already satisfied: cffi>=1.12 in /usr/local/lib/python3.7/dist-packages 
(from cryptography) (1.15.0)
Requirement already satisfied: pycparser in /usr/local/lib/python3.7/dist-packages
 (from cffi>=1.12->cryptography) (2.21)
Installing collected packages: cryptography
Successfully installed cryptography-36.0.1

Cryptography in Python

1)Import the Modules

# Import Fernet from fernet of the cryptography module
# using the import keyword
from cryptography.fernet import Fernet

2)Implementation

To implement cryptography, we will generate a Fernet key (sometimes known as the “secret key”) and then use the key to create a Fernet object.

This key is vital, and it must be kept secure. If someone discovers your key, he or she will be able to decode all of our secret messages; if we lose it, we will no longer be able to decrypt your own messages.

# Import Fernet from fernet of the cryptography module
from cryptography.fernet import Fernet
#Getting the value of the key using the generate_key() function
keyvalue = Fernet.generate_key()

The next step is to encrypt the text, which we do by calling the encrypt function and passing the message to it. The encrypted message will be returned by the function.

Along with this, we use the decrypt function to extract the decrypted message from the encrypted message and pass the encrypted message.

#Pass some random string by adding b as prefix to it for the encrypt function of the above fernet object 
#Here we get encrypted text
Encrypted_text = Fernet_object.encrypt(b"Hello Good morning this is BTechgeeks ")
#Getting the original Text using the decrypt function of the above fernet object 
Original_text= Fernet_object.decrypt(Encrypted_text)

3)Print the Result

Approach:

  • Import Fernet from fernet of the cryptography module
  • Getting the value of the key using the generate_key() function
  • Getting the fernet object by passing the above key value to the Fernet function
  • Pass some random string by adding b as the prefix to it for the encrypt function of the above fernet object
  • Here we get encrypted text
  • Getting the original Text using the decrypt function of the above fernet object
  • The Exit of the Program.

Full Implementation of Code

# Import Fernet from fernet of the cryptography module
from cryptography.fernet import Fernet
#Getting the value of the key using the generate_key() function
keyvalue = Fernet.generate_key()
#Getting the fernet object by passing the above key value to the Fernet function
Fernet_object= Fernet(keyvalue)
#Pass some random string by adding b as prefix to it for the encrypt function of the above fernet object 
#Here we get encrypted text
Encrypted_text = Fernet_object.encrypt(b"Hello Good morning this is BTechgeeks ")
#Getting the original Text using the decrypt function of the above fernet object 
Original_text= Fernet_object.decrypt(Encrypted_text)
print("The Encrypted Text of the Above String is:\n\n", Encrypted_text)
print("\nThe Original Text (Decrypted text) of the above string is:\n\n",Original_text)

Output:

The Encrypted Text of the Above String is:

 b'gAAAAABh-UQKPMVWcGWNdlZ5h2aeCsJIxQTTW40X9820cBKCgCcVPs2sSVT8lHLT8BgXeHLEJkJSCRQ0yPTkdw9iII_bZ3bTUA5mFKmBzAWaU_mr-7-oHrB4R-Uh3E2HysG_tTJ_O8hi'

The Original Text (Decrypted text) of the above string is:

 b'Hello Good morning this is BTechgeeks '