Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
The 'Caesar Cipher' is one of the oldest cryptography methods, named after Julius Caesar. It was developed around 100 BC and used by Julius Caesar to send secret messages to his generals in the field. If his messages got intercepted during the event, his opponent could not read them. This obviously gave him a tremendous strategic advantage. So, what was the code?
A basic and proven approach for converting data into secret code is the Caesar Cipher program in C. It offers a way to both encrypt and decode the given data. It seasonally switches the current character to a few characters. Since it is basic, it is simple to use and, hence, simple to crack.
In this article, we will discuss what caesar cipher is and how to encode and decode a message.
What is Caesar Cipher?
Caeser shifted each letter of his message three positions right( change to 3rd next alphabet) to produce what could be called ciphertext. If the enemy somehow got the message, he will see the ciphertext instead of the true message. For example, in Caeser's message letter 'A' would become 'D,' letter 'B' would become 'E,' and the letter 'Y' becomes 'B.' Caser used the key as 3. However, a key can be altered while generating a cipher, and the same key must be used to decrypt it.
An image showing decryption of letter choosing key as 3 is shown below.
Program for Caeser Cipher in C
In technical terms, the Caeser cipher, also termed a shift cipher, is an encryption technique based on the monoalphabetic cipher. The table for encryption and decryption by Caeser cipher is explained in the table below.
The formula of encryption is:
En (x) =(x + n) mod 26
The formula of decryption is:
Dn(x) = (x - n) mod 26
Where,
En depicts encryption
Dn depicts decryption
x denotes the value of the letter
n denotes the shift or key-value
If any case (Dn) value becomes negative (-ve), in this case, we will add 26 in the negative value.
Time for an example:
Suppose you have to encrypt and decrypt the word NINJA.
The above table depicts the value x for each letter.
Caesar Cipher Encryption
You will traverse word by word, and for each letter, you will find its corresponding cipher letter. The letter N will become Q and similarly, all letters will transform into their respective letters and the NINJA will become QLQMD. Use formula En (x) =(x + n) mod 26, here n(key)=3.
Plaintext: N → 13
En: (13 + 3) mod 26
Ciphertext: 16 → Q
Plaintext: I → 8
En: (8 + 3) mod 26
Ciphertext: 11 → L
Plaintext: J → 9
En: (9 + 3) mod 26
Ciphertext: 12 → M
Plaintext: A → 0
En: (0 + 3) mod 26
Ciphertext: 3 → D
Now let's see how to decrypt.
Caesar Cipher Decryption
Similarly, QLQMD can be decrypted to NINJA again using the formula Dn(x) = (xi - n) mod 26.
Ciphertext: 16 → Q
Dn: (16 - 3) mod 26
Plaintext: N → 13
Ciphertext: 11 → L
Dn: (11 - 3) mod 26
Plaintext: I → 8
Ciphertext: 12 → M
Dn: (12 - 3) mod 26
Plaintext: J → 9
Ciphertext: 3 → D
Dn: (3 - 3) mod 26
Plaintext: A → 0
Implementation for Caesar Cipher
You can easily generate Caesar cipher for the given text; you can also choose a key and encrypt and decrypt your message accordingly. Now let's see the algorithm and C++ program for encryption and decryption of messages.
Input to be taken from the user:
First-line contains the string called text
Second-line contains the Key, it is an integer ranging from 0-25
Algorithm
Traverse the given text one character at a time.
For every character in the text, transform it as per the rule( given key). Consider whether the text is encrypted or decrypted.
Return the newly transformed string.
Code for Encryption
C
C
#include <stdio.h> #include <string.h>
int main() { // Declaring variables int i, key; char s[1000], c;
// Taking Input printf("Enter a plaintext to encrypt:\n"); fgets(s, sizeof(s), stdin); printf("Enter key:\n"); scanf("%d", &key);
int n = strlen(s);
// Encrypting each character according to the given key for (i = 0; i < n; i++) { c = s[i]; if (c >= 'a' && c <= 'z') { c = c + key; if (c > 'z') { c = c - 'z' + 'a' - 1; } s[i] = c; } else if (c >= 'A' && c <= 'Z') { c = c + key; if (c > 'Z') { c = c - 'Z' + 'A' - 1; } s[i] = c; } }
// Output the cipher printf("Encrypted message: %s\n", s);
// Decrypting each character according to the given key for (i = 0; i < n; i++) { c = s[i]; if (c >= 'a' && c <= 'z') { c = c - key; if (c < 'a') { c = c + 'z' - 'a' + 1; } s[i] = c; } else if (c >= 'A' && c <= 'Z') { c = c - key; if (c < 'A') { c = c + 'Z' - 'A' + 1; } s[i] = c; } }
// Output the original message printf("Decrypted message: %s\n", s);
Only one short key is used in the entire process(the same key is used for encryption and decryption).
If the system does not use complex coding techniques, it is the method and easy too.
Only a few computing resources are required.
Disadvantages of Caesar Cipher
The entire message can be decrypted by looking at the pattern of letters or trying brute force.
It is less secure.
It can easily be hacked since messages encrypted by this method can easily be decrypted.
Frequently Asked Questions
How do you program a Caesar cipher?
The first step is to traverse the given text one character at a time. Then for every character in the text, transform it as per the rule( given key). Consider whether the text is encrypted or decrypted. Finally, Return the newly transformed string.
Why is the Caesar cipher not a strong encryption method?
The Caesar cipher is not a strong encryption method due to its simplicity and vulnerability to brute-force attacks. It has only 26 possible keys, making it easily breakable by trying all possible shifts. It does not provide sufficient security for modern encryption requirements.
What is the logic of the Caesar cipher in C?
The logic of the Caesar cipher in C involves shifting each character in the plaintext message by a fixed number of positions (key) to obtain the corresponding character in the ciphertext.
What is code 3 in Caesar cipher?
In Caesar cipher, "code 3" refers to a shift of three positions in the alphabet. For example, A becomes D, B becomes E, and so on, shifting letters by three steps.
Conclusion
In this article, we discussed what Caeser cipher is. We discussed how a message is encrypted and decrypted using Caesar cipher. We also saw its implementation using code in C++.