Caesar Cipher:-
Caesar cipher is one of the earliest known and simplest ciphers. It is a type of replacement cipher in which each letter of the plaintext is ‘moved’ to a certain place under the alphabet.
For example, with a shift of 1, A will be replaced by B, B becomes C, and so on.
The name of this method is named after Julius Caesar, who apparently used it to communicate with his generals.
Caesar Cipher Program in C
-
Caesar Cipher Encryption
#include<stdio.h> int main() { char str_message[500], ch; int j, key; printf("Enter a message to encrypt: "); scanf("%s",str_message); printf("Enter the key: "); scanf("%d", &key); for(j = 0; str_message[j] != '\0'; ++j){ ch = str_message[j]; if(ch >= 'a' && ch <= 'z'){ ch = ch + key; if(ch > 'z'){ ch = ch - 'z' + 'a' - 1; } str_message[j] = ch; } else if(ch >= 'A' && ch <= 'Z'){ ch = ch + key; if(ch > 'Z'){ ch = ch - 'Z' + 'A' - 1; } str_message[j] = ch; } } printf("Encrypted message: %s", str_message); return 0; }

Example:- If you run above program then it will ask to enter the message to encrypt like below:-
Enter a message to encrypt:
walmart.com
Enter the key: –for encrypt your message
5
Encrypted Message is : = bfqrfwy.htr
Enter a message to encrypt:
amazon river
Enter the key: –for encrypt your message
5
Encrypted Message is : = frfets wnajw
-
Caesar Cipher Decryption
#include<stdio.h> int main() { char str_message[500], ch; int j, key; printf("Enter a message to decrypt: "); scanf("%s",str_message); printf("Enter key: "); scanf("%d", &key); for(j = 0; str_message[j] != '\0'; ++j){ ch = str_message[j]; if(ch >= 'a' && ch <= 'z'){ ch = ch - key; if(ch < 'a'){ ch = ch + 'z' - 'a' + 1; } str_message[j] = ch; } else if(ch >= 'A' && ch <= 'Z'){ ch = ch - key; if(ch < 'A'){ ch = ch + 'Z' - 'A' + 1; } str_message[j] = ch; } } printf("Decrypted message: %s", str_message); return 0; }

Example:- If you run above program then it will ask to enter the message to decrypt like below:-
Enter a message to decrypt:
bfqrfwy.htr
Enter key: –for decrypt your message
5
Decrypted Message : = walmart.com
Enter a message to decrypt:
frfets wnajw
Enter key: –for decrypt your message
5
Decrypted Message : = amazon river
Caesar Cipher Program in C++
-
Caesar Cipher Encryption
#include<iostream> using namespace std; int main() { char str_message[500], ch; int j, key; cout << "Enter a message to encrypt: "; cin.getline(str_message, 100); cout << "Enter key: "; cin >> key; for(j = 0; str_message[j] != '\0'; ++j){ ch = str_message[j]; if(ch >= 'a' && ch <= 'z'){ ch = ch + key; if(ch > 'z'){ ch = ch - 'z' + 'a' - 1; } str_message[j] = ch; } else if(ch >= 'A' && ch <= 'Z'){ ch = ch + key; if(ch > 'Z'){ ch = ch - 'Z' + 'A' - 1; } str_message[j] = ch; } } cout << "Encrypted message: " << str_message; return 0; }
Output:-
Example:- If you run above program then it will ask to enter the message to encrypt like below:-
Enter a message to encrypt:
walmart.com
Enter the key: –for encrypt your message
5
Encrypted Message is : = bfqrfwy.htr
Enter a message to encrypt:
amazon river
Enter the key: –for encrypt your message
5
Encrypted Message is : = frfets wnajw
Caesar Cipher Decryption
#include<iostream> using namespace std; int main() { char str_message[100], ch; int j, key; cout << "Enter a message to decrypt: "; cin.getline(str_message, 100); cout << "Enter key: "; cin >> key; for(j = 0; str_message[j] != '\0'; ++j){ ch = str_message[j]; if(ch >= 'a' && ch <= 'z'){ ch = ch - key; if(ch < 'a'){ ch = ch + 'z' - 'a' + 1; } str_message[j] = ch; } else if(ch >= 'A' && ch <= 'Z'){ ch = ch - key; if(ch > 'a'){ ch = ch + 'Z' - 'A' + 1; } str_message[j] = ch; } } cout << "Decrypted message: " << str_message; return 0; }

Example:- If you run above program then it will ask to enter the message to decrypt like below:-
Enter a message to decrypt:
bfqrfwy.htr
Enter key: –for decrypt your message
5
Decrypted Message : = walmart.com
Enter a message to decrypt:
frfets wnajw
Enter key: –for decrypt your message
5
Decrypted Message : = amazon river
thnks lol for this precise simple cipher
Like!! Really appreciate you sharing this blog post.Really thank you! Keep writing.