Caesar Cipher Program in C: The Caesar Cipher technique is the simplest and oldest method of encryption technique in cryptography. In this technique, we are given a text which we have to encrypt by shifting the characters by some fixed number of positions.
For example, if the shift position is 2 then every character will be shifted by 2 positions to the right.
Plain:
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z |
Cipher:
C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B |
- Traverse the entire text letter by letter.
- On every iteration, the character is transformed as per the rule, depending upon whether you are encrypting or decrypting the text.
- Return the newly updated string.
Write a C program to perform the encryption and decryption of a message using the Ceaser Cipher technique.
To perform the cipher technique in a given message, we have to find the integer value corresponding to the given text. Then we will use the modulo operator to perform the shift operation which does not exceed the limit of characters which is 0 to 25.
The corresponding integer value of each character is given below:
A | B | C | D | E | F | G | H | I | J | K | L | M |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
N | O | P | Q | R | S | T | U | V | W | X | Y | Z |
13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 |
There are several ways to implement the Caesar Cipher in C language. Let’s take a detailed look at all the approaches to encrypt and decrypt the text using Caesar Cipher method.
- Caesar Cipher Program in C using Simple Approach
- Caesar Cipher Program in C using Optimized Approach
Here is source code of the C Program to Implement Caesar Cipher. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
#include <stdio.h>
#include <ctype.h>
#define MAXSIZE 1024
void encrypt(char*);
void decrypt(char*);
int menu();
int main(void) {
char c, choice[2], s[MAXSIZE];
while (1) {
menu();
gets(choice);
if ((choice[0] == 'e') || (choice[0] == 'E')) {
puts("Input text to encrypt->");
gets(s);
encrypt(s);
} else if ((choice[0] == 'd') || (choice[0] == 'D')) {
puts("Input text to decrypt->");
gets(s);
decrypt(s);
} else
break;
}
return 0;
}
void encrypt(char*str) {
int n = 0;
char *p = str, q[MAXSIZE];
while (*p) {
if (islower(*p)) {
if ((*p >= 'a') && (*p < 'x'))
q[n] = toupper(*p + (char) 3);
else if (*p == 'x')
q[n] = 'A';
else if (*p == 'y')
q[n] = 'B';
else
q[n] = 'C';
} else {
q[n] = *p;
}
n++;
p++;
}
q[n++] = '\0';
puts(q);
}
void decrypt(char*str) {
int n = 0;
char *p = str, q[MAXSIZE];
while (*p) {
if (isupper(*p)) {
if ((*p >= 'D') && (*p <= 'Z'))
q[n] = tolower(*p - (char) 3);
else if (*p == 'A')
q[n] = 'x';
else if (*p == 'B')
q[n] = 'y';
else
q[n] = 'z';
} else {
q[n] = *p;
}
n++;
p++;
}
q[n++] = '\0';
puts(q);
}
int menu() {
puts("To encrypt, input e or E\n");
puts("To decrypt, input d or D\n");
puts("To exit, input any other letter\n");
puts("Your choice:->\n");
return 0;
}
1. This program uses the Caesar cipher to encrypt and decrypt messages.
2. It offers users a menu to select between encryption or decryption and requests text input.
3. Following user input, the program utilizes the Caesar cipher, shifting each alphabet letter by three positions.
4. Finally, it shows the changed message, demonstrating how the Caesar cipher works for coding and decoding text.
$ gcc CaesarCipher.c $ ./a.out To encrypt, input e or E To decrypt, input d or D To exit, input any other letter Your choice:-> e Input text to encrypt-> DharmendraHingu DKDUPHQGUDHLQJX To encrypt, input e or E To decrypt, input d or D To exit, input any other letter Your choice:-> x
In this approach, we will use the modulo operator to perform the shift operation which does not exceed the limit of characters which is 0 to 25.
Here is the C program implementation of the Caesar Cipher technique for encryption and decryption. We have discussed the program in detail below the code.
/*
* C Program to Implement Caesar Cipher
*/
#include <stdio.h>
// function prototyping
void encrypt(char*, int);
void decrypt(char*, int);
void tolower_case(char*);
int isAlpha(char);
// main function
int main()
{
char text[50];
int shift_by;
printf("Enter your text: ");
gets(text);
fflush(stdin);
printf("Encrypt by how much shift: ");
scanf("%d", &shift_by);
// max shift is 26
shift_by = shift_by % 26;
// encrypting the text
encrypt(text, shift_by);
printf("\n\nEncrypted Text:\n%s\n", text);
// decrypting the text
decrypt(text, shift_by);
printf("\nDecrypted Text:\n%s", text);
return 0;
}
// function to encrypt the text
void encrypt(char* text, int shift)
{
char* ch = text;
int i = 0;
// convert into lowercase string
tolower_case(text);
while(*ch != '\0')
{
// encrypt only alphabet characters by shifting their positions
if(text[i] != ' ' && isAlpha(text[i]))
{
text[i] = 'a' + (text[i] - 'a' + shift) % 26;
}
i += 1;
ch++;
}
}
// function to decrypt the text
void decrypt(char* text, int shift)
{
char* ch = text;
int i = 0;
tolower_case(text);
while(*ch != '\0')
{
// skip the whitespace and non-alphabet characters
if(text[i] == ' ' || !isAlpha(text[i]))
{
text[i] = text[i];
}
// if character is reaching to the last alphabet character
else if(text[i] - shift < 'a')
{
text[i] = 'z' - ('a' - (text[i] - shift + 1));
}
else
{
text[i] = text[i] - shift;
}
i += 1;
ch += 1;
}
}
// function to check whether the character is alphabet or not
int isAlpha(char ch)
{
if(ch >= 'a' && ch <= 'z')
{
return 1;
}
if(ch >= 'A' && ch <= 'Z')
{
return 1;
}
return 0;
}
// function to convert the string into lowercase
void tolower_case(char* text)
{
char* ch = text;
while(*ch != '\0')
{
if(*ch >= 'A' && *ch <= 'Z')
{
*ch = *ch + 32;
}
ch++;
}
}
In the above program we have perform the encryption and decryption operation of a text. Let’s understand how these functions work:
To encrypt the text, we need to shift the character by some places. To shift the characters we have to increment their ASCII values by n, where n is the number of shifts that we want to do.
Here is an example of shifting the text message by 3 places.
Text: hello
Normal | h | e | l | l | o |
---|---|---|---|---|---|
ASCII value | 104 | 101 | 108 | 108 | 111 |
Shifted | 107 | 104 | 111 | 111 | 114 |
Encrypted | k | h | o | o | r |
Encrypted Message: khoor
Time Complexity: O(n)
Linear time complexity to encrypt the text. Here, n is the length of the text.
Space Complexity: O(1)
It takes constant space to encrypt the text.
To decrypt the message, we have to shift back the position of characters by the given position.
Let’s decrypt the encrypted message by position 3.
Encrypted Message: khoor
Encrypted | k | h | o | o | r | ASCII value | 104 | 101 | 108 | 108 | 111 |
---|---|---|---|---|---|
Decrypted | 107 | 104 | 111 | 111 | 114 | Normal | h | e | l | l | o |
Text: hello
Time Complexity: O(n)
It takes the linear amount of time to decrypt the text from the encrypted text. Here, n is the length of a string.
Space Complexity: O(1)
Since we are using only normal variables, the space complexity is constant.
In this case, we are encrypting and decrypting text using Caesar Cipher algorithm.
Enter your text: hello Encrypt by how much shift: 3 Encrypted Text: khoor Decrypted Text: hello
Advantages of Caesar Cipher:
- It is easy to build this message and message can be easily encrypted and decrypted in this method.
- It provides minimum security to the information.
- The system does not need to code is complicated.
- It requires less number of computing resources.
- It takes less time for encryption and decryption.
Disadvantages of Caesar Cipher:
- It has a very simple structure usage.
- It can only provide the minimum security to the information.
- Security is a big issue with this method.
To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.
If you find any mistake above, kindly email to [email protected]- Apply for C Internship
- Check Computer Science Books
- Apply for Computer Science Internship
- Check C Books
- Practice Computer Science MCQs