Caeser Cipher program is c++:
#include<iostream>
#include<string.h>
using namespace std;
int main() {
cout<<"Enter the message:\n";
char msg[100];
cin.getline(msg,100); //take the message as input
int i, j, length,choice,key;
cout << "Enter key: ";
cin >> key; //take the key as input
length = strlen(msg);
cout<<"Enter your choice \n1. Encryption \n2. Decryption \n";
cin>>choice;
if (choice==1) //for encryption{
char ch;
for(int i = 0; msg[i] != '\0'; ++i) {
ch = msg[i];
//encrypt for lowercase letter
If (ch >= 'a' && ch <= 'z'){
ch = ch + key;
if (ch > 'z') {
ch = ch - 'z' + 'a' - 1;
}
msg[i] = ch;
}
//encrypt for uppercase letter
else if (ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if (ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}
msg[i] = ch;
}
}
printf("Encrypted message: %s", msg);
}
else if (choice == 2) { //for decryption
char ch;
for(int i = 0; msg[i] != '\0'; ++i) {
ch = msg[i];
//decrypt for lowercase letter
if(ch >= 'a' && ch <= 'z') {
ch = ch - key;
if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;
}
msg[i] = ch;
}
//decrypt for uppercase letter
else if(ch >= 'A' && ch <= 'Z') {
ch = ch - key;
if(ch < 'A') {
ch = ch + 'Z' - 'A' + 1;
}
msg[i] = ch;
}
}
cout << "Decrypted message: " << msg;
}
}
Output
For encryption:
Enter the message:
tutorial
Enter key: 3
Enter your choice
1. Encryption
2. Decryption
1
Encrypted message: wxwruldo
For decryption:
Enter the message:
wxwruldo
Enter key: 3
Enter your choice
1. Encryption
2. Decryption
2
Decrypted message: tutorial
Caesar Cipher Program in Java
Algorithm for Caesar Cipher
Now, let’s understand how the Caesar cipher program in Java works.
We create a CaesarCipher class, which holds the cipher() method, which
takes offset and message as parameters.
Let’s suppose the message contains lowercase letters and space with a
positive offset, shifting characters by the offset we have:
import java.util.Scanner;
public class CaesarCipher {
String cipher(String message, int offset) {
// To hold the cipher text
StringBuilder result = new StringBuilder();
// Character by character encryption
for (char character : message.toCharArray()) {
if (character != ' ') {
int originalAlphabetPosition = character - 'a';
// Applying Caesar Cipher Technique
int newAlphabetPosition =
(originalAlphabetPosition + offset) % 26;
char newCharacter = (char) ('a' +
newAlphabetPosition);
// Adding the new character to the result
result.append(newCharacter);
} else {
result.append(character);
}
}
return result.toString();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
CaesarCipher cipher = new CaesarCipher();
int offset = sc.nextInt();
String cipheredMessage = cipher.cipher(
"I enjoy learning from scalar topics",
offset
);
System.out.println(cipheredMessage);
}
}
Output:
3
L hqmrb ohduqlqj iurp vfdodu wrslfv
Explanation:
We are providing an offset of 3. Hence, each letter of the
English alphabet will shift by 3 letters, as shown in the
image below.
Hence, in the output, I has shifted to L, e has shifted to h,
and so on.
Brute force decryption for a Caesar
Cipher in Java:
void decryptbruteforce(String encryptmessage) {
//Get the standard alphabet
String standalpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//Convert this message to uppercase
String encryptmessageupper =
encryptmessage.toUpperCase();
StringBuilder sbdecrypt = new
StringBuilder(encryptmessageupper);
int key;
int i;
int index;
char currentchar;
char newchar;
//Loop through the 26 keys in the alphabet.
for (key = 1; key < 27; key++) {
//Loop through the encrypted message
for (i = 0; i < sbdecrypt.length(); i++) {
//Get the encrypted character
currentchar = sbdecrypt.charAt(i);
//Get the index in the alphabet
index = standalpha.indexOf(currentchar);
//If the currentchar is in the alphabet
if (index != -1) {
//Reduce the character by the key in the alphabet
index = index - key;
//If the character goes below 0, aka 'A', go back to the
end of the alphabet
if (index < 0) {
index = index + 26;
//Get the new character in the alphabet
newchar = standalpha.charAt(index);
//Set the character in the stringbuilder
sbdecrypt.setCharAt(i, newchar);
}
else {
//Get the new character in the alphabet
newchar = standalpha.charAt(index);
//Set the character in the stringbuilder
sbdecrypt.setCharAt(i, newchar);
}
}
}
//Print the key and the resulting string
System.out.println("Key: " + key + " Decrypted String: " +
sbdecrypt);
}
}
Brute force decryption for a Caesar
Cipher in C++:
/* This is
progam
of ceasar
cipher
encrypti
on and
brute
force
attack */
#include<iostream>
using namespace std;
//function to encrypt the plain text
string encrypt(string x,int n)
{
string cipher="";
/* only caps and small caps
alphabet would be considered for
encryption other symbols would
remain as it is. */
for(int i=0;i<x.length();i++)
{
if(isupper(x[i]))
cipher += (x[i] + n - 65)%26 +
65; /* here x[i] would be ASCII value of
corresponding alphabet */
else if(islower(x[i]))
cipher += (x[i] + n - 97)%26 +
97;
else
cipher += x[i]; /* other
symbols other than alphabets would
remain as it is. */
}
return cipher;
}
//function to decrypt the cipher text
using brute force attack
void decrypt(string x)
{
string text;
for(int n=0;n<26;n++)
{
text = "";
for(int i=0;i<x.length();i++)
{
if(isupper(x[i]))
{
if((x[i] - n - 65)<0)
text += 91 + (x[i] - n -
65);
else
text += (x[i] - n - 65)%26
+ 65;
}
else if(islower(x[i]))
{
if((x[i] - n - 97) < 0)
text += 123 + (x[i] - n -
97);
else
text += (x[i] - n - 97)%26
+ 97;
}
else
text += x[i];
}
cout << "plain text for key " <<
n << " :- " << text << endl;
}
}
int main()
{
int key;
string text;
cout << "enter text:- ";
getline(cin,text);
cout << "enter key:- ";
cin >> key;
string cipher = encrypt(text,key);
cout << "cipher text :- " << cipher <<
endl << endl;
decrypt(cipher);
}