Data Encryption Decryption in python 2022

Data Encryption Decryption in python 2022

Data Encryption Decryption in Python



Encryption Python Key

Encryption is the process of encoding the data. i.e converting plain text into ciphertext. This conversion is done with a key called an encryption key.

Decryption Python Key

Decryption is the process of decoding the encoded data. Converting the ciphertext into plain text. This process requires a key that we used for encryption.

We require a key for encryption. There are two main types of keys used for encryption and decryption. They are Symmetric-key and Asymmetric-key.

Symmetric-key Encryption

Symmetric-key encryption, the data is encoded and decoded with the same key. This is the easiest way of encryption, but also less secure. The receiver needs the key for decryption, so a safe way need for transferring keys.

How to Symmetric-key Encryption in Python?

from cryptography.fernet import Fernet
message = "Hello Python Programmer"
key = Fernet.generate_key()
fernet = Fernet(key)
encMessage = fernet.encrypt(message.encode())
print("original string: ", message)
print("encrypted string: ", encMessage)
decMessage = fernet.decrypt(encMessage).decode()
print("decrypted string: ", decMessage)


Asymmetric-key Encryption

Asymmetric-key Encryption, we use two keys a public key and a private key. The public key is used to encrypt the data and the private key is used to decrypt the data. By the name, the public key can be public (can be sent to anyone who needs to send data). No one has your private key, so no one in the middle can read your data.


How to Asymmetric-key Encryption in Python?

import rsa
publicKey, privateKey = rsa.newkeys(512)
message = "Hello Python Programmer"
encMessage = rsa.encrypt(message.encode(),publicKey)

print("original string: ", message)
print("encrypted string: ", encMessage)
decMessage = rsa.decrypt(encMessage, privateKey).decode()

print("decrypted string: ", decMessage)



Post a Comment

0 Comments