Cryptography Caesar Shift Cipher

To understand well the concept of Cryptography we will look at a simple example and very famous that you will solve with Python 3, I am always doing emphasis on Python because it is a great language and easy to learn.

Cryptography is one of most interesting branches of programming. Studying its algorithms usually begins with the simple method named after famous Roman emperor Julius Caesar who used it for communicating his military secrets (and perhaps for love letters to Cleopatra).

We will practice deciphering encrypted messages in this problem.

The idea of the algorithm is simple. Each letter of the original text is substituted by another, by the following rule:

  • find the letter (which should be encrypted) in the alphabet;
  • move K
    positions further (down the alphabet);
  • take the new letter from here;
  • if “shifting” encountered the end of the algorithm, continue from its start.

For example, if K=3(shift value used by Caesar himself), then Abecomes D,  becomes E, W
becomes Zand Z becomes Cand so on, according to the following table:

There are many ways to solve this problem; then we have one of them if you invent a little more with python or ruby you can answer this with only three lines of code.

ls=[]
quantity=[]
quantity.append(input("""Write the number of words and the value of "K"
for example: 1 3: """))
quantity =" ".join(quantity)
quantity=quantity.replace(" ", ",")
i=0

while i<int(quantity[0]):
    ls.append(input("Write the words separated by space and end with 'point'. : "))
    i=i+1

ls=" ".join(ls)
ls=ls.replace("", ",")
ls=ls.split(",")
k=int(quantity[2])
lsn=[]

lsl=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
j=0
while j < len(ls):
    i=0
    while i < len(lsl):
        if ls[j]==lsl[i]:
            lsn.append(lsl[i-k])
        i=i+1
    if ls[j]==" ":
        lsn.append(" ")
    if ls[j]==".":
        lsn.append(". ")
    j=1+j
lsn="".join(lsn)
print ("decrypted message: ")
print(lsn)
exit = input("EXIT")

let’s look at what this code does in python.

if we receive the following message and they tell us K = 3

“LQIRVHFDGGLFWV LV WKH EHVW”

This is very simple, and the idea was to show how it works. Something a little more complex would be to decipher what would be the value K. the encryption methods are much more complicated than all the above.

Many tools decipher in seconds, and some use artificial intelligence.

Resources:

http://www.codeabbey.com/

 

Avatar

This post was written by Ruben Dario Caravajal Herrera