Encryption and Decryption of String in Python

Hello Friends How are you, Today in this post "Encryption and Decryption of String in Python" I am going to teach you how you can encrypt and decrypt you message in Python using very simple lines of code. Encryption is very important fact in the field of Computer and Technology because many times we have to secure our message for security reasons so that no one can read it.

Now i am going to explain step by step. To get the complete knowledge go thorough this post.

Step 1: Install Library
To make this program first you need to install a library called cryptography so open command window and type the command pip install cryptography and press enter to install it to your system.

Step 2: Import Library
Here i am using cryptography library of python to encrypt and decrypt the message so i have imported cryptography  library.

from cryptography.fernet import Fernet

Here in the above code you can see that i have imported class Fernet from library cryptography which is used to encrypt and decrypt the message.

Step 3: Generate Key
Here in this step i am going to generate a key and i will store it into a file and the file name is secret.key. Following is the code to generate key in python.
Note: Here this key is very important because if you lost this key then you can't recover or decrypt your original message.

def generateKey():
key=Fernet.generate_key()
with open("secret.key","wb") as keyFile:
keyFile.write(key)

generate_key() is a predefined function of class Fernet which is used to generate key so i have generated a key and then i have opened a file with name secret.key and then stored generated key into it.

Step 4: Encrypt Message
Here i have defined a message with name encryptMessage() to perform the encryption operation. It will take a string as a parameter then this function will encrypt it. Here is the complete code of this function.

def encryptMessage(msg):
#load key
key=open("secret.key","rb").read()
#encode message
encodedMessage=msg.encode()
#pass key to Fernet
f=Fernet(key)
#encrypt message
encryptedMessage=f.encrypt(encodedMessage)
#print encrypted message
print(encryptedMessage)

Here in the above code first secret key is loaded then message is encoded using encode() function then it is encrypted using  a function encrypt().

Complete code of Encryption

Here is the complete code for message encryption.

#import library
from cryptography.fernet import Fernet
#define function to generate a secret key
def generateKey():
#generate key
key=Fernet.generate_key()
#open file secret.key
with open("secret.key","wb") as keyFile:
#store key in file secret.key
keyFile.write(key)
#define function to encrypt message
def encryptMessage(msg):
#load key
key=open("secret.key","rb").read()
#encode message
encodedMessage=msg.encode()
#pass key to Fernet
f=Fernet(key)
#encrypt message
encryptedMessage=f.encrypt(encodedMessage)
#print encrypted message
print(encryptedMessage)
#call function to generate key
generateKey()
#message for encryption
encryptMessage("I love Python")

Output:

decryptMessage(b'gAAAAABf-II2Ny3eYFKNW2Hx0PNXMHsyENT-usNadrJ4F4Bd5VYolaSwjgSG7oFhBqYSLp0NWLIDILSfjET51EeHxXJ_xYakTQ=='

This is the encrypted output of String I love Python.

Complete code of Decryption

Here to decrypt the encrypted message just load the previous generated key and pass it to Fernet than by using this secret key just decrypt the  message using decrypt() function and then decode the obtained message using decode() function to get the original message.

#import library
from cryptography.fernet import Fernet
#define function to decrypt message
def decryptMessage(emsg):
#load key
key=open("secret.key","rb").read()
#pass key to Fernet
f=Fernet(key)
#decrypt message
decryptedMessage=f.decrypt(emsg)
#print decrypted message
print(decryptedMessage.decode())
#function calling with encrypted message
decryptMessage(b'gAAAAABf-II2Ny3eYFKNW2Hx0PNXMHsyENT-usNadrJ4F4Bd5VYolaSwjgSG7oFhBqYSLp0NWLIDILSfjET51EeHxXJ_xYakTQ==')
"""
Output
I love Python
"""

I hope now you can  create "Encryption and Decryption of String in Python". If you have any doubt regarding this post  or you want something more in this post then let me know by comment below i will work on it definitely.
 

Request:-If you found this post helpful then let me know by your comment and share it with your friend. 
 
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

If you like my post then share it with your friends. Thanks😊Happy Coding.

Post a Comment

0 Comments