Hello coder!! In this tutorial, we’ll learn about what Caesar cipher is and how to implement the same in Python. If you have learned about cryptography then you must have heard about the term Caesar cipher.

*Recommended**Read: Cryptography in Python – Step-By-Step Implementation*

Caesar cipher is one of many symmetric-key cryptographies, and it’s one of the oldest and easiest ways to implement cryptography. Encrypting and decrypting using Caesar cipher is easy, as the function is fixed and no key is required.

Let’s say we have the text “journaldev” which we need to encrypt. Then what we can do is replace each letter present in the text with another letter having a fixed difference. Let’s say we want to right shift the letters by 1. The resulting text becomes “kpvsobmefw”. Now users can’t read this text until they have the decrypt key.

If we see this encryption technique in a mathematical way then the formula to get encrypted letter will be:

*n = (o + k) mod 26*

where,

n is the new value of the encrypted letter,

o is the value of the actual letter,

k is the key (how many positions of letters we have to shift)

On other hand, to decrypt each letter we’ll use the formula given below:

*n = (o – k) mod 26*

## Code Implementation

Now, let’s create a simple Python code to encrypt and decrypt ciphertext:

```
ALPHA = 'abcdefghijklmnopqrstuvwxyz'
def encrypt_caesar(key, text):
final_text = ''
for k in text.lower():
try:
temp = (ALPHA.index(k) + key) % 26
final_text += ALPHA[temp]
except ValueError:
final_text+= k
return final_text.lower()
key = 4
inp_text=input("Enter the Input Text : ")
encrypt_text = encrypt_caesar(key,inp_text)
print("Encrypted Text :",encrypt_text)
```

First, we created a variable named `ALPHA`

in which we have all the letters together. Then, we created a function named `encrypt_ceaser(key,text)`

in which we will put the key (set as 4 in this case) and the text that we need to encrypt.

All of the alphabets in the text are added with the shift key and then divided by 26. Once the loop is complete, the letters are shifted by the shift value.

**Let’s look at some sample output below.**

```
Enter the Input Text : journaldev
Encrypted Text : nsyvrephiz
```

## Advantages and Disadvantages of Caesar cipher

Let’s now look at what are the advantages and disadvantages of the Ceasar cipher here

### Advantages

Its advantages of caesar cipher are as follows: –

- It is very easy to implement and is the simplest method of cryptography.
- Only one short key is all we need for the entire process and hence do not use any complex coding techniques.

### Disadvantages

Its disadvantages are as follows: –

- It can be easy to hack and lacks security.
- By looking at the pattern of letters in it, a person can decrypt the entire message.

## Conclusion

In this tutorial, we learned how to simply encrypt a message or sentence in Python via the Ceaser cipher. Thank you for reading!