Labels

Linux (28) Python (13) Raspberry Pi (5) Bugs (3) Install (3) C (2) Brainf**k (1) HTML (1) Maths (1) Sorts (1)

Sunday, 10 April 2016

Ceaser cipher in Python

A Ceaser cipher is a basic encryption originating from the 1st century, invented by Julius Ceaser. It works by moving each letter a fixed position down or up the alphabet.
Here it is better explained by a diagram:









Now for the code in python 2.7, Firstly we will create a function for encoding so open a new file:
def encode(text)


Create an array to hold the new characters:
new_text = []

Now we need to loop through each character in the input text the user will give us:
for char in text:

ord converts a character to its numbered value in ascii.
In this case we will shift all the letters by 3 forward in the ascii table however you can use whatever number you want as long as it lower than 127:
char = ord(char) + 3

To make sure that we stay in the range of ASCII values we need to implement an if statement
if(char > 127):
    char = char - 127


Now to convert the value back to a character:
char = chr(char)

Then add this character to the array:
new_text.append(char)


We have reached the end of this function and now have to return the array to the program:
return new_array

You should now have your first function, which put together look like this:
def encode(text):
        new_text = []
        for char in text:
                char = ord(char) + 3
                if(char > 127):
                        char = char - 127
                char = chr(char)
                new_text.append(char)
        return new_text

The decode function works the same you just need to the opposite to decode instead of encode. I have made the changes in bold below.
def decode(text):
        new_text = []
        for char in text:
                char = ord(char) - 3
                if(char < 0):
                        char = char + 127
                char = chr(char)
                new_text.append(char)
        return new_text


We now need to allow the user to input some text and decide to encode or decode.
This can be done with a simple while loop and if/elif statment:



choice = 0
while choice != 3:
        choice = raw_input("1:Encode, 2:Decode, 3:Exit ")
        if(choice == "1"):
                user_input = raw_input("Enter a word: ")
                new_text = encode(user_input)
                print ''.join(new_text)
        elif(choice == "2"):
                user_input = raw_input("Enter a word: ")
                new_text = decode(user_input)
                print ''.join(new_text) 
        elif(choice == "3"):
                print "Goodbye"
        else:
                print "Please enter a correct choice"



Now lets see if it works

Checking the encoding works:
Choose 1 for encode then enter "hello, world" it should give the output of "khoor/#zruog"

Checking the decoding works:
Choose 2 for decode then enter "khoor/#zruog" it should give the output of "hello, world"

4 comments:

  1. Awesome..You have clearly explained ...Its very useful for me to know about new things..Keep on blogging..
    SEO training in chennai

    ReplyDelete
  2. Thanks for sharing this valuable information to our vision. You have posted a worthy blog keep sharing.
    Digital Marketing Course In Kolkata
    Web Design Course In Kolkata
    SEO Course In Kolkata

    ReplyDelete