In cryptography, a Caesar cipher , also known as Caesar\'s cipher , the shift ci
ID: 3911700 • Letter: I
Question
In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet.
Given an arbitrary cipher text file, you need to write a C++ program to find the value of the shift, and decrypt the cipher text to plaintext. You must NOT hard code the plain text or the cipher text in your program. Your program should try various shift values, and compare with these common English words:
Explanation / Answer
// C++ program to find the value of the shift, and decrypt the cipher text to plaintext.
//Here cipher.txt file contain input text encoded in cipher
//plain.txt file contain common English words as given in question
#include <bits/stdc++.h>
using namespace std;
int main()
{
int distance,i;
fstream file1,file2;
string cipher,plain,filename1,filename2;
filename2="plain.txt";
filename1 = "cipher.txt";
file1.open(filename1.c_str());
file2.open(filename2.c_str());
while (file1 >> cipher) //extracting words form the cipher text file
{ while(file2>>plain){ //extracting words form the plain text file
if(cipher.length()!=plain.length())continue; //comapring length of both string
else{
distance=cipher[i]-plain[i]; //finding distance between first elements of both text
for( i=1;i<cipher.length();i++){
if(cipher[i]-plain[i]!=distance)break;}
if(i==cipher.length())
cout<<"value of shift: "<<distance<<" decrypted text: "<<plain<<" ";
else continue;
}
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.