EASY ENCRYPTION AND DECRYPTION IN PYTHON – 8

encryption and decryption in python

OVERVIEW

In this blog, we’ll take an example and revise all the topics that we have covered till now for python programming. Today you will learn,

  • What is encryption and decryption?
  • How to take input from the user and to encrypt it?
  • How to decrypt the message that you encrypted?

What is encryption and decryption?

Encryption is the process of taking plain text, like a text message, username or password, and scrambling it into an unreadable format called “cipher text.” This will help you protect the confidentiality of digital data either stored on computer systems or transmitted via network like the internet.

To understand it in more details, in this blog I’ll use one of the earliest and the simplest method of encryption technique know as the Caesar Cipher technique. Simply it is a type of substitution cipher, i.e., every letter of the input text is replaced by a new letter for some fixed number of positions down the alphabets.

For example, for an input “innovate” we’ll first calculate the index value of input of “i” as per the fixed data(say alphabets=”abcdefghijklmnopqrstuvwxyz”) and now we’ll add a fixed number(for example: 5) to this calculated index value. Now, whatever is the new value we’ll fetch the value from the alphabets and replace “i” with this value and now repeat the same process for all the letters in the input value.

Now, that you have the encrypted message with you, so to convert the encrypted message to the original message by applying the reverse technique is know as decryption. Now, let’s write the code and understand that how it works.

How to take input from the user and to encrypt it?

Initally we’ll create few objects that will hold the alphabets, user input, encryption and decryption message and key as given.

alphabets="abcdefghijklmnopqrstuvwxyz"              # Predefined data to update the message
message=input("Enter the message to encrypt: ")     # Ask the user to enter the message
encrypt,decrypt="",""            # initialize the empty string to save the encryption and decryption message
key=5                            # Fixed key to update the index value

Now, as I have explained above that how the encryption works so it’s time to apply that logic and to encrypt the message.

for letter in message:
new_position=(alphabets.find(letter)+key)%len(alphabets)
encrypt+=alphabets[new_position]
print("Encrypted message:",encrypt)

Output for the above code will be,

Enter the message to encrypt: innovateyouself
Encrypted message: nsstafyjdtzxjqk

How to decrypt the message that you encrypted?

With the above code you have encrypted the message. Now lets see how to decrypt the cipher text. Decryption is the reverse process of encryption. Here is the code to decrypt the cipher text and to convert it to the original text.

for letter in encrypt:
    new_position=(alphabets.find(letter)-key)%len(alphabets)
    decrypt+=alphabets[new_position]
print("Decrypted message:",decrypt)

Output for the above code will be,

Enter the message to encrypt: innovateyourself
Decrypted message: innovateyourself

Note:

If you also want to encrypt or decrypt the uppercase value, digits or special character, then do include them in the alphabets so as to convert it.

In this way you can encrypt and decrypt the full text that the user will give as an input.

Full Code:
'''
Code created by Ashish Saini from Innovate Yourself
Date: Feb 2, 2018
'''

alphabets="abcdefghijklmnopqrstuvwxyz"
message=input("Enter the message to encrypt: ")
encrypt,decrypt="",""
key=5

for letter in message:
    new_position=(alphabets.find(letter)+key)%len(alphabets)
    encrypt+=alphabets[new_position]

for letter in encrypt:
    new_position=(alphabets.find(letter)-key)%len(alphabets)
    decrypt+=alphabets[new_position]


print("Encrypted message:",encrypt)
print("Decrypted message:",decrypt)

Also check this video for more clarification,

Time to wrap up now. Hope you liked our content on encryption and decryption in python. See you in our next blog, thanks for reading our blog and do leave a comment below to help us improve the content to serve you all of our experience with you. Stay tuned with us for more python programming contents.

Also check out our other playlist Rasa Chatbot, Internet of things, Docker, Python Programming, etc.
Become a member of our social family on youtube here.

Leave a Reply

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