Caeser Cipher program is c and java
Caeser Cipher program is c and java
#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
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;
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) {
int key;
int i;
int index;
char currentchar;
char newchar;
else {
#include<iostream>
using namespace std;
int main()
{
int key;
string text;
cout << "enter text:- ";
getline(cin,text);
cout << "enter key:- ";
cin >> key;
decrypt(cipher);
}