How to Encrypt and Decrypt Strings in Python?

In this article, we will find out with regards to Encryption, Decryption and carry out them with Python.

Encryption:

Encryption is the most common way of encoding the information. i.e changing over plain text into ciphertext. This transformation is finished with a key called an encryption key.

Unscrambling:

Unscrambling is a course of interpreting the encoded information. Changing over the ciphertext into plain text. This cycle requires a key that we utilized for encryption.

We require a key for encryption. There two principle sorts of keys utilized for encryption and unscrambling. They are Symmetric-key and Asymmetric-key.

Symmetric-key Encryption:

In symmetric-key encryption, the information is encoded and decoded with a similar key. This is the most straightforward method of encryption, yet in addition less secure. The beneficiary necessities the key for unscrambling, so a protected way need for moving keys. Anybody with the key can peruse the information in the center.

Example:

Install the python cryptography library with the following command.

pip install cryptography

Table of Contents

Steps:

  • Import Fernet
  • Then, at that point, create an encryption key, that can be utilized for encryption and unscrambling.
  • Convert the string to byte string, with the goal that it tends to be scrambled.
  • Occurrence the Fernet class with the encryption key.
  • Then, at that point, scramble the string with Fernet example.
  • Then, at that point, it tends to be decoded with Fernet class occurrence and it ought to be instanced with a similar key utilized for encryption.
from cryptography.fernet import Fernet
# we will be encryting the below string.
message = "hello geeks"
# generate a key for encryptio and decryption
# You can use fernet to generate
# the key or use random key generator
# here I'm using fernet to generate key
key = Fernet.generate_key()
# Instance the Fernet class with the key
fernet = Fernet(key)
# then use the Fernet class instance
# to encrypt the string string must must
# be encoded to byte string before encryption
encMessage = fernet.encrypt(message.encode())
print("original string: ", message)
print("encrypted string: ", encMessage)
# decrypt the encrypted string with the
# Fernet instance of the key,
# that was used for encrypting the string
# encoded byte string is returned by decrypt method,
# so decode it to string with decode methods
decMessage = fernet.decrypt(encMessage).decode()
print("decrypted string: ", decMessage)

Output:

 

Python

Awry key Encryption:

In Asymmetric-key Encryption, we utilize two keys a public key and private key. The public key is utilized to encode the information and the private key is utilized to unscramble the information. By the name, the public key can be public (can be shipped off any individual who needs to send information). Nobody has your private key, so nobody the center can peruse your information.

Example:

Install the python rsa library with the following command.

pip install rsa

Steps:

  • Import rsa library
  • Create public and private keys with rsa.newkeys() strategy.
  • Encode the string to byte string.
  • Then, at that point, encode the byte string with the public key.
  • Then, at that point, the encoded string can be unscrambled with the private key.
  • The public key must be utilized for encryption and the private must be utilized for unscrambling.
import rsa
# generate public and private keys with
# rsa.newkeys method,this method accepts
# key length as its parameter
# key length should be atleast 16
publicKey, privateKey = rsa.newkeys(512)
# this is the string that we will be encrypting
message = "hello geeks"
# rsa.encrypt method is used to encrypt
# string with public key string should be
# encode to byte string before encryption
# with encode method
encMessage = rsa.encrypt(message.encode(),
                         publicKey)
print("original string: ", message)
print("encrypted string: ", encMessage)
# the encrypted message can be decrypted
# with ras.decrypt method and private key
# decrypt method returns encoded byte string,
# use decode method to convert it to string
# public key cannot be used for decryption
decMessage = rsa.decrypt(encMessage, privateKey).decode()
print("decrypted string: ", decMessage)

Output:

Python

Also ReadReading an Excel File using Python

Leave a Reply

Your email address will not be published. Required fields are marked *