Python program for symmetric encrypt and decrypt a message

Sowmiya K
1 min readMar 8, 2024

--

output
  1. It imports the Fernet class from the cryptography.fernet module.
  2. It generates a key using the generate_key() method of the Fernet class.
  3. It creates a Fernet object (f_obj) using the generated key.
  4. It defines a message (msg) to be encrypted. Note that the message is encoded as bytes (b”hello world”).
  5. It encrypts the message using the encrypt() method of the Fernet object (f_obj) and stores the result in encrypted_msg.
  6. It prints the encrypted message.
  7. It decrypts the encrypted message using the decrypt() method of the Fernet object (f_obj) and stores the result in decrypted_msg.
  8. It prints the decrypted message.

This code demonstrates how to use Fernet symmetric encryption provided by the cryptography library in Python. Fernet is a symmetric encryption algorithm, meaning that the same key is used for both encryption and decryption. It’s a convenient choice for encrypting small amounts of data like passwords or tokens.

--

--

Sowmiya K
Sowmiya K

Written by Sowmiya K

Working for HCL as a Senior Software Engineer | Interested in Networking | cybersecurity | Python

No responses yet