Th2 242017

Today I want to talk to you about ciphers, more in particular Caesar Ciphers. Encryption and Decryption of secret messages date back a long time before computers. Countless Nations have utilized ciphers to communicate secrets across distances. For example, lets say I have soldier on the other side of the planet and I am relaying them a message that might put their life at risk. Do we want that message falling into the wrong hands? No. So what can we do? Well, the simple answer is to encrypt the message. Today almost every message is encrypted and packet into protocols several of times. For example, your computer encrypts packets it sends back and forth with your WiFi router all the time. Computers, for better or worse, require us to come up with new encryption methods on a daily basis. However, everyone has to start somewhere. Before you can plunge in and understand things like AES and RSA encryption, we need to start in something basic, like Caesar Ciphers.


Caesar Cipher is pretty simple. It is a substitution cipher that people like to think was user by Julius Caesar. In this cipher we follow a formula to encrypt a message and another to decrypt. Let us take a look at what that means mathematically.

Here is our encryption formula:


And here is decryption:


And thats it folks. Wait, what does this actually mean? Well, lets first start with out assumptions:

  1. The alphabet we are working with contains 26 letters (English)
  2. n is is a number less than or equal to 25.

These assumptions can be modified for any alphabet language of you choosing. If the alphabet contains m letters, than n has to be less than or equal to m-1. As long as you keep both conditions, you will be fine. Here is a general outline and picture of how the algorithm works. You should be aware that normally n is set to 3, but it will work with any number.

  1. Assign numeric values to each letter in the alphabet.
  2. Encrypt:
    • for each letter in plain_text
      • new letter is letter_numeric_value + n
  3. Decrypt:
    • for each letter in cipher
      • plain_text_letter is letter_numeric_value – n

For it to make some sense, here is a visual picture from Wikipedia:



So the letter E in our message (plain text) is 5 and n=3, so our encrypted value of E is B. Now that we have some background and a simple example, let us implement it in Python. Like always, we will use Python 2.7. Here is how Caesar Cipher looks like in Python:

First lets look at a simple encryption module:

import sys

def main(argv):
	if (len(sys.argv) != 2):
		sys.exit('Usage: <k>')
	plaintext = list(raw_input('Enter message: '))
	alphabet = list('abcdefghijklmnopqrstuvwxyz')
	k = int(sys.argv[1])
	cipher = ''
	for c in plaintext:
		if c in alphabet:
			cipher += alphabet[(alphabet.index(c)+k)%(len(alphabet))]
	print 'Your encrypted message is: ' + cipher

if __name__ == "__main__":

In this example we define the alphabet and make use of Python List and Indexing. We ask the user to pass in a key of length k and preform the substation. Here is an example run:


Now let us up the stakes to something more complex. After all, this is an encryption that people used to do by hand all the time. Take a look at this program:

1 import sys
3 def encrypt(k):
4     plaintext = raw_input('Enter plain text message: ')
5     cipher = ''
7     for each in plaintext:
8         c = (ord(each)+k) % 126
10         if c < 32:
11             c+=31
13         cipher += chr(c)
15     print 'Your encrypted message is: ' + cipher
17 def decrypt(k):
18     cipher = raw_input('Enter encrypted message: ')
19     plaintext = ''
21     for each in cipher:
22         p = (ord(each)-k) % 126
24         if p < 32:
25             p+=95
27         plaintext += chr(p)
29     print 'Your plain text message is: ' + plaintext
31 def main(argv):
32     if (len(sys.argv) != 3):
33         sys.exit('Usage: <k> <mode>')
35     if sys.argv[2] == 'e':
36         encrypt(int(sys.argv[1]))
37     elif sys.argv[2] == 'd':
38         decrypt(int(sys.argv[1]))
39     else:
40         sys.exit('Error in mode type')
43 if __name__ == "__main__":
44     main(sys.argv[1:])

The first question you might ask is why is the mod 126? Well, this version of Caesar Cipher was adapted to take full use of the ASCII Printable Character map, minus the first 32 characters that are not printable. With this program, we can chose a larger n and create a “harder” to break ciphers. You will also notice that now we can do Encryption and Decryption in the same program. Let us try to decrypt ‘khoor’ and see what we get:


Sweet. Now let’s try to encrypt something a little longer with a different key.


And Decryption:


Caesar ciphers are nice to play around with and to learn basic concepts, but is really impractical. The number of combination you can get is really small compared to what the computer can generate. Brute force attacks can always crack the code with very little human intervention. Consider a 26 letter alphabet. There are only 25 options for selecting a unique n. That means that if I can generate all the 25 permutations, 1 of them has to be the correct plain text. Let us look brute force braking using the following script:

1 import sys
3 def decrypt(k,cipher):
4     plaintext = ''
6     for each in cipher:
7         p = (ord(each)-k) % 126
9         if p < 32:
10             p+=95
12         plaintext += chr(p)
14     print plaintext
16 def main(argv):
17     if (len(sys.argv) != 1):
18         sys.exit('Usage:')
20     cipher = raw_input('Enter message: ')
22     for i in range(1,95,1):
23         decrypt(i,cipher)
25 if __name__ == "__main__":
26     main(sys.argv[1:])

I will spare you the entire 94 lines that got printed out, here are a few. Can you guess what the message originally was?


Here is another one:


I will let you come to your own conclusions as to how “hard” it is to brake Caesar Ciphers. Personally, I wouldn’t use them for anything simple. Than again, I do not know how many people would check if you used it. I hoped you gain some understanding to how cipher works, substitution ciphers in particular. Next time I might dig a little farther into security algorithms and play around with another step up in encryption-decryption algorithms.

Happy Hacking
Captain DeadBones

 Posted by at 2:27 chiều

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>