# Index Caesar Cipher in Python

In this tutorial, we’ll learn about data security using Index Caesar Cipher in Python.

It is an encryption technique method which is the earliest and simplest one.

In this method, each letter is replaced by another letter after performing a shift for a particular number of times. For example, if we shift A by 1 then it will become B, shift B by 3 then it will become E and so on.

Note: If we shift Z by 1 then it’ll become A due to the cyclic property of encryption. Therefore, after performing shift we’ll apply modulo operation on it.

We’ll use the index of alphabets of English letters to perform the shifts. English alphabets are indexed as follows:

A-1, B-2, C-3, and so on till Z-26.

Here, we must have to remember a few things as mentioned below:

- Indexing in programming is used from 0, so we’ll consider indexes for alphabets from 0 to 25 rather than 1 to 26
- ASCII value of upper-case letters starts from 65
- ASCII value of lower-case letters starts from 97

Since ASCII values start from 65 or 97 so we’ll have to make our result 65 or 97 based indexed after performing shift.

Let ‘c’ be a character and ‘n’ be shift value then the encrypted value would be

Encrypted value = (c+n)%26

But as discussed above indexing isn’t 0-based so we’ll first subtract 65 or 97 from ASCII value of character c then add n, perform modulo operation and finally add 65 or 97 to it.

## Algorithm:

- Extract the ASCII value of the character
- Subtract 65(for Upper-case letter) or 97 (for lower-case letter)
- Add shift value
- Perform modulo operation
- Add 65 or 97

Now, we’ll write a function in Python for encryption.

def encryption(text,shift): encrypted = "" for val in text: # To encrypt uppercase characters if (val.isupper()): encrypted += chr((ord(val) -65 + shift) % 26 + 65) # To encrypt lowercase characters else: encrypted += chr((ord(val) - 97 + shift ) % 26 + 97) return encrypted

Here,

- text: input text to be encrypted
- shift: shift value for encryption
- ord(): extract the ASCII value of the character
- chr(): to convert the value into char data type from the numerical value

Now, we’ll take some input and see the result.

text = "Attack" shift = 2 print( "Input text : " + text ) print( "Shift by : " + str(shift) ) print( "Encrypted text: " + encryption(text,shift) )

The corresponding result is as follows:

Input text : Attack Shift by : 2 Encrypted text: Cvvcem

Now, we’ll perform decryption. Decryption by ‘n’ is nothing but encryption by 26-’n’. So, here the entire logic we’ll be the same except the shift value will be 26-shift value.

We can use the same function for decryption but we’ll define another function for sake of convenience.

def decryption(text,shift): decrypted = "" for val in text: # To decrypt uppercase characters if (val.isupper()): decrypted += chr((ord(val) -65 + shift) % 26 + 65) # To decrypt lowercase characters else: decrypted += chr((ord(val) - 97 + shift ) % 26 + 97) return decrypted

Now, we’ll take the result of encryption to test our decryption function.

text = "Cvvcem" shift = 2 print( "Input text : " + text ) print( "Shift by : " + str(26 - s) ) print( "Decrypted text: " + decryption(text,26-s) )

The corresponding result is as follows:

Input text : Cvvcem Shift by : 24 Decrypted text: Attack

Wow!, we succeed.

I hope you enjoyed this tutorial.

## Leave a Reply