Keep it Secret !

Implementing Caesar Cipher

Kishor Keshav

--

Encryption is a method of securing digital communication (message or data) exchanged between a sender and a receiver, using one or more techniques based on mathematical transformations. The key to decrypt this message is secretly shared with the recipient, to keep the message confidential. The original message is called as plaintext and the encrypted message is called ciphertext.

Caesar Cipher(substitution cipher) is a technique for encrypting and decrypting alphanumeric messages. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions away (+/-) from the alphabet being replaced. The method is named after Julius Caesar(a Roman general around 44 BC), who used it in his private correspondence to secure it.

Caesar cipher is a trivial encryption scheme, which can be easily cracked and provides hardly any security for the messages being transmitted. This is simply used in games for kids to have some fun and also by various forums and groups to discourage casual readers. It is also practiced by newbies in encryption world. In this article implementation of Caesar cipher is given in Python. It contains the functions for both encryption as well as decryption.

Implementation provided below uses two standard library function ord() and chr(). Function ord() returns Unicode of the character passed to it. In contrast chr() returns Unicode character of the value passed to it.

The steps of the algorithm implemented for the encryption is given below:

1. Traverse the given message character by character

2. for each character in the message, check if it is

  • a digit or
  • a lower case alphabet or
  • an upper case alphabet or
  • any other character

and replace the original character with the encrypted(transformed) character using the given key(here it is a shift value which is positive or negative).

For each type of character, the conversion goes from character to integer using ord() function and then adding key value(shift) and then converting this new integer value back to the character index(using modulus operator %) and then converting to encrypted character using chr() function.

For example the uppercase alphabet is converted using the following code:

Acode=ord(‘A’)

result = result + chr((ord(char) + s -Acode) % 26 + Acode)

Similar logic applies to all other character types.

Note that this implementation do not encrypt other characters such as punctuation marks and spaces, it simply keeps them as it is.

The resultant message is an empty string initially, and transformed characters are just joined to this string in the for loop, that traverse each character in the message. At the end of the loop your cipher text (encrypted message) is ready.

If you run this program with plaintext = ‘Cd5’, then you will get ciphertext as ‘Ab3’. Note that the key is -2, hence the characters are encoded leftwards by two places.

Decryption is a very trivial step, as we need to call encryption() function with the shift in the

opposite direction. For example if key=+2 during encryption, then key for decryption becomes -2.

Run the program using the given test cases and your own test cases as well with different shifts (keys) and see, how the message is encrypted and decrypted correctly by the algorithms.

#A python program to illustrate Caesar Cipher(substitution cipher)
#function for encryption
def encrypt(text,s):

result = '' #empty string

# traverse text character by character
for i in range(len(text)):
char = text[i]

# Encrypt digits
if(char.isdigit()):
Code0=ord('0') # get integer value for character '0'
result += chr((ord(char) + s-Code0) % 10 + Code0)

# Encrypt uppercase characters
elif (char.isupper()):
Acode=ord('A')
#convert 65:90 to 0:25 to A:Z
result += chr((ord(char) + s-Acode) % 26 + Acode)


# Encrypt lowercase characters
elif (char.islower()):
acode=ord('a')
#convert 97:122 to 0:25 to a:z
result += chr((ord(char) + s - acode) % 26 + acode)

# keep all other characters as it is
else:
result +=char #result=result + char

return result
#function for dycryption
#decrypt(encrypt(m))=m
def decrypt(text,s): #s stands for key/shift
return encrypt(text,-s)
#Main program
#check the above functions with following test cases
text = "ATTACK 77 AT DAWN!"
#text= "I stay in Nagpur."
#text="My Bank account number is 15628395010."
#text ="do not attack, simply create panic @441110."
#s is shift or key
s=3
print("Original message(plaintext) is:" + text)
print ("Shift(key) is : ", s )
cipher=encrypt(text,s)
print ("Ciphertext (encrypted message): " + cipher )
print ("Decrypted Message = " + decrypt(cipher,s) )

The output for uncommented test case is:

Original message(plaintext) is:ATTACK 77 AT DAWN !
Shift(key) is : 3
Ciphertext (encrypted message): DWWDFN 00 DW GDZQ !
Decrypted Message = ATTACK 77 AT DAWN !

That’s all ! Enjoy coding , have fun !

--

--