Cryptography- Shift Cipher
Tags: python, security, cybersecurity, javascript
Published: 2021-06-06 09:45:15


 
Shift cipher is the most basic and simple substitution cipher ever existed. It was used by numerous kings and common people in ancient times, because of its simplicity. It is also known as the Caesar cipher as he used a generalized form of it known as ROT-13. 


##How Shift Cipher Works:-
 
Shift cipher is nothing but substitution of letters to their left or right by some specific number of letters, and that number of letters will be known as the key of the cipher. Once the key is decided and the plaintext is encrypted to ciphertext, we can send the cipher text to whomever we want. If he/she will have the key, then he/she will easily decrypt the message and read it and can reply to that encrypting the message with the same key.

##Encrypting the text using Shift Cipher
 
1. Select a key, if you want to shift your letters to right, choose a number which is positive, a negative number will result in shifting of letters to left side.

2. Once the key is selected, convert the letters to their respective numeric positions, where A->1, B->2 and so on.

Now apply the given formula to every number:- 

```	   
C=(P+K)%26
```
Where P is your plaintext converted to numeric positions, K is the key and C is the numeric positions of the letters in ciphertext.

Now convert the numeric positions of ciphertext (C) to alphabets according to 0->Z, 1->A so on, and you have your plaintext encrypted!!!


Example:-
	
Let our plaintext to be:- 
“ ”

Then numeric positions of our plaintext will be:-



| k  | i | l  |  l |  t | h | e | k  | i | n  | g | t  | o  | n  | i | g | h | t  |
|----|---|----|:--:|:--:|---|---|----|---|----|---|----|----|----|---|---|---|----|
| 11 | 9 | 12 | 12 | 20 | 8 | 5 | 11 | 9 | 14 | 7 | 20 | 15 | 14 | 9 | 7 | 8 | 20 |


Let our key be 7, after using the formula for encryption, the number will look like:-


| 11 | 9 | 12 | 12 | 20 | 8 | 5 | 11 | 9 | 14 | 7 | 20 | 15 | 14 | 9 | 7 | 8 | 20 |
|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|
| 18 | 16 | 19 | 19 |  1 | 15 | 12 | 18 | 16 | 21 | 14 |  1 | 22 | 21 | 16 | 14 | 15 |  1 |


And finally, converting the numbers back to letters will give us our ciphertext,

| 18 | 16 | 19 | 19 |  1 | 15 | 12 | 18 | 16 | 21 | 14 |  1 | 22 | 21 | 16 | 14 | 15 |  1 |
|:--:|:--:|:--:|:--:|:-:|:--:|:--:|:--:|:--:|:--:|:--:|:-:|:--:|:--:|:--:|:--:|:--:|:-:|
|  R |  P |  S |  S | A |  O |  L |  R |  P |  U |  N | A |  V |  U |  P | N  |  O | A |



Letters after conversion are :-
> 'RPSSAOLRPUNAVUPNOA'

The gibberish above is ciphertext, it is often written without any spaces to add complexity to the ciphertext.



##Writing Python code for Shift Cipher


Writing code for this cipher is really easy, a one liner, some might say. Here's the code:--

```py
def shift_encrypt(plain_text: str, key: int):
	cipher_text = [] # stores encrtypted text
	plain_text = plain_text.lower().replace(' ','') # reduce complexity
	for i in list(plain_text):
		cipher_text.append(chr((ord(i)-97 + key)%26 + 97)) # real encryption happens here

	return "".join(cipher_text)
```

The code up here is pretty self explanatory, except a few lines. Let's go through all the lines on by one.

```py
cipher_text = [] # stores encrtypted text
plain_text = plain_text.lower().replace(' ','') # reduce complexity
```

These two lines define a list names `cipher_text` to store the text after encryption and reduces the complexity of the plain text by converting the text to lower case and removing all the whitespaces.

Then comes the most important line in the code:-
```py
cipher_text.append(chr((ord(i)-97 + key)%26 + 97))
```

First of all, this line of code converts the letters to their ASCII representation using the `ord` function, which means a will become 97, b will become 98, and so on. 

Then it will subtract 97 from the ASCII code, which will convert 97 to 0, hence placing 'a' at 0th position and placing 'z' at 25th position. This is done to simplify the operation of shifting.

After that is done, we proceed and add the `KEY` to shift and actually encrypt the data.

Then we do `mod 26` because after subtracting 97 our alphabets lies from 0 to 25, and doing `mod 26` makes sure that nothing goes out of range of our alphabets. 97 is added in end to convert the the shifted numbers back to their ASCII representations.

After this much is done, we convert the shifted ASCII codes back to characters using the `chr` function. And the encryption is done.

You can see that this line is inside a for loop, that is to ensure that the transformation in this line of code is applied to every single letter of the `plain_text`.


In the end, 
```py
return "".join(cipher_text)
```

We convert the encrypted characters to a string and return it.

Decryption part of the cipher is also pretty much the same, except a small thing.

```py
def shift_decrypt(cipher_text: str, key: int):
	plain_text = []
	cipher_text = cipher_text.lower().replace(' ','')
	for i in cipher_text:
		plain_text.append(chr((ord(i)-97 - key)%26 + 97))

	return "".join(plain_text)

```

Instead of adding `KEY`, we subtract it this time to perform a inverse of the shift operation we did in the encryption process.

You can try the code here:--

(JUST CLICK ON THE RUN(>) BUTTON)

{% replit @PranavPatel4/CryptoCode %}