top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Caeser Cipher Game: Encrypt and Decrypt a Message using Python

Writer: Aayushma PantAayushma Pant




In today's world, messages to be sent are modified and encrypted using different patterns so that they won't get into the wrong hand and only the sender knows the key to decrypt it. This pattern has been since ancient times, where kings used to communicate sending messages encrypted into a different pattern. And the enemy doesn't get the information from it.


So recalling this, today we will learn about encrypting and decrypting the message. Here we are concerned with the alphabets that we will shift and the receiver only needs the knowledge of shift to decrypt it.

1) Encrypt the message



def encrypt(text,shift):
    message=''
    for i in text:
        if (i.isupper()):
            message+=chr((ord(i)+shift-65)%26+65)
        elif (i.islower()):
            message+=chr((ord(i)+shift-97)%26+97)
        else: 
            message+=i
    return message

Here we put the message to be encrypted. Then each letter of the words is converted to ASCII numbers which are increased by the shift (the user provides). If the character is Uppercase, 65(ASCII of 'A') is added to convert the code back to string and for lowercase 97(ASCII of 'a') is added.

The output of the encryption is:



2) Decrypt the message



def decrypt(text,shift):
    message=''
    for i in text:
        if (i.isupper()):
            message+=chr((ord(i)-shift-65)%26+65)
        elif (i.islower()):
            message+=chr((ord(i)-shift-97)%26+97)
        else: 
            message+=i
    return message

Here in decryption, we should subtract the shift value from the ASCII code of the encrypted message to get the actual message.


The output of decryption can be seen as below:


You can get the repo from here.



 
 

Comments


COURSES, PROGRAMS & CERTIFICATIONS

 

Advanced Business Analytics Specialization

Applied Data Science with Python (University of Michigan)

Data Analyst Professional Certificate (IBM)

Data Science Professional Certificate (IBM)

Data Science Specialization (John Hopkins University)

Data Science with Python Certification Training 

Data Scientist Career Path

Data Scientist Nano Degree Program

Data Scientist Program

Deep Learning Specialization

Machine Learning Course (Andrew Ng @ Stanford)

Machine Learning, Data Science and Deep Learning

Machine Learning Specialization (University of Washington)

Master Python for Data Science

Mathematics for Machine Learning (Imperial College London)

Programming with Python

Python for Everybody Specialization (University of Michigan)

Python Machine Learning Certification Training

Reinforcement Learning Specialization (University of Alberta)

Join our mailing list

Data Insight participates in affiliate programs and may sometimes get a commission through purchases made through our links without any additional cost to our visitors.

bottom of page