One of the simplest examples of an encryption technology is the Caesar cipher, w
ID: 3572560 • Letter: O
Question
One of the simplest examples of an encryption technology is the Caesar cipher, which is said to have been used by Julius Caesar to communicate with his army. Caesar is considered to be one of the first persons to have ever employed encryption for the sake of securing messages. Caesar decided that shifting each letter in the message would be his standard algorithm, and so he informed all of his generals of his decision, and was then able to send them secured messages. Using the Caesar Shift (3 to the right), the message, "Return to Rome" would be encrypted as, "Uhwxuq wr Urph".
Note that only letters are encoded. Punctuation, spaces, newlines, and so forth are not encoded. Lower case letters are encoded into lower case letters, and upper case letters are encoded into upper case letters. Also, note that the encoding system “wraps around” at z and Z. So the letter ‘y’ is encoded as ‘b’ using a Caesar Shift (3 to the right).
There are many references to the Caesar Cipher in the literature, which you can find if you search Google. The source I used was as follows: http://www.trincoll.edu/depts/cpsc/cryptography/caesar.html.
Your assignment is to write a C++ program which will automatically decrypt virtually any Caesar coded message. It should rely on the fact that the letter ‘e’ is the most frequently appearing letter in the English language. So if you can determine which letter appears most frequently in the encrypted message, you can determine which letter is the encoded equivalent of ‘e’. This, in turn, lets you calculate the amount of the Caesar “shift”. If you unshift the encoded message by the same amount, you should be able to produce the original “clear text” message. I have uploaded an encrypted message using a Caesar Cipher for you to decrypt. Have fun!
Deliverables:
Please upload your program source code (.cpp file) as usual. This is a good size program. So if you cannot complete the whole thing, try to do it in parts.
Part 1: If you can count the letters to figure out the letter that is equivalent of ‘e’, you get 1 point.
Part 2. After part 1 if you can calculate the shift and write a function for that, you will get 2 points.
Part 3: If you apply the shift and decrypt the text file to an output text file, you get 3 points.
Notes: Please try to design your solution to use as many aspects of this class as possible. For example, try to modularize your solution using C++ functions wherever you can. Also, I think you will need to use an array to determine which letter appears most frequently in the encoded message. Your program should prompt the user to enter the file name of the input (encrypted) file and the file name for the output (clear text) file, and should provide appropriate error handling as necessary.
******TEXT FILIE********
[Ftq Pqoxmdmfuaz ar Uzpqbqzpqzoq ar ftq Ftudfqqz Oaxazuqe
Uz OAZSDQEE, Vgxk 4, 1776
Ftq gzmzuyage Pqoxmdmfuaz ar ftq ftudfqqz gzufqp Efmfqe ar Myqduom,
Itqz uz ftq Oagdeq ar tgymz qhqzfe, uf nqoayqe zqoqeemdk rad azq bqabxq fa pueeaxhq ftq baxufuomx nmzpe ituot tmhq oazzqofqp ftqy iuft mzaftqd, mzp fa meegyq myazs ftq baiqde ar ftq qmdft, ftq eqbmdmfq mzp qcgmx efmfuaz fa ituot ftq Xmie ar Zmfgdq mzp ar Zmfgdq'e Sap qzfufxq ftqy, m pqoqzf dqebqof fa ftq abuzuaze ar ymzwuzp dqcgudqe ftmf ftqk etagxp pqoxmdq ftq omgeqe ituot uybqx ftqy fa ftq eqbmdmfuaz.
Iq taxp ftqeq fdgfte fa nq eqxr-qhupqzf, ftmf mxx yqz mdq odqmfqp qcgmx, ftmf ftqk mdq qzpaiqp nk ftqud Odqmfad iuft oqdfmuz gzmxuqzmnxq Dustfe, ftmf myazs ftqeq mdq Xurq, Xunqdfk mzp ftq bgdeguf ar Tmbbuzqee. --Ftmf fa eqogdq ftqeq dustfe, Sahqdzyqzfe mdq uzefufgfqp myazs Yqz, pqduhuzs ftqud vgef baiqde rday ftq oazeqzf ar ftq sahqdzqp, --Ftmf itqzqhqd mzk Rady ar Sahqdzyqzf nqoayqe pqefdgofuhq ar ftqeq qzpe, uf ue ftq Dustf ar ftq Bqabxq fa mxfqd ad fa mnaxuet uf, mzp fa uzefufgfq zqi Sahqdzyqzf, xmkuzs ufe ragzpmfuaz az egot bduzoubxqe mzp adsmzuluzs ufe baiqde uz egot rady, me fa ftqy etmxx eqqy yaef xuwqxk fa qrrqof ftqud Emrqfk mzp Tmbbuzqee. Bdgpqzoq, uzpqqp, iuxx puofmfq ftmf Sahqdzyqzfe xazs qefmnxuetqp etagxp zaf nq otmzsqp rad xustf mzp fdmzeuqzf omgeqe; mzp mooadpuzsxk mxx qjbqduqzoq tmft etqiz, ftmf ymzwuzp mdq yadq puebaeqp fa egrrqd, ituxq qhuxe mdq egrrqdmnxq, ftmz fa dustf ftqyeqxhqe nk mnaxuetuzs ftq radye fa ituot ftqk mdq moogefayqp. Ngf itqz m xazs fdmuz ar mngeqe mzp gegdbmfuaze, bgdeguzs uzhmdumnxk ftq emyq Anvqof qhuzoqe m pqeusz fa dqpgoq ftqy gzpqd mneaxgfq Pqebafuey, uf ue ftqud dustf, uf ue ftqud pgfk, fa ftdai arr egot Sahqdzyqzf, mzp fa bdahupq zqi Sgmdpe rad ftqud rgfgdq eqogdufk. Egot tme nqqz ftq bmfuqzf egrrqdmzoq ar ftqeq Oaxazuqe; mzp egot ue zai ftq zqoqeeufk ituot oazefdmuze ftqy fa mxfqd ftqud radyqd Ekefqye ar Sahqdzyqzf. Ftq tuefadk ar ftq bdqeqzf Wuzs ar Sdqmf Ndufmuz [Sqadsq UUU] ue m tuefadk ar dqbqmfqp uzvgduqe mzp gegdbmfuaze, mxx tmhuzs uz pudqof anvqof ftq qefmnxuetyqzf ar mz mneaxgfq Fkdmzzk ahqd ftqeq Efmfqe. Fa bdahq ftue, xqf Rmofe nq egnyuffqp fa m omzpup iadxp.
Tq tme dqrgeqp tue Meeqzf fa Xmie, ftq yaef itaxqeayq mzp zqoqeemdk rad ftq bgnxuo saap.
Tq tme radnuppqz tue Sahqdzade fa bmee Xmie ar uyyqpumfq mzp bdqeeuzs uybadfmzoq, gzxqee egebqzpqp uz ftqud abqdmfuaz fuxx tue Meeqzf etagxp nq anfmuzqp; mzp itqz ea egebqzpqp, tq tme gffqdxk zqsxqofqp fa mffqzp fa ftqy.
Tq tme dqrgeqp fa bmee aftqd Xmie rad ftq mooayyapmfuaz ar xmdsq puefduofe ar bqabxq, gzxqee ftaeq bqabxq iagxp dqxuzcguet ftq dustf ar Dqbdqeqzfmfuaz uz ftq Xqsuexmfgdq, m dustf uzqefuymnxq fa ftqy mzp radyupmnxq fa fkdmzfe azxk.
Tq tme omxxqp fasqftqd xqsuexmfuhq napuqe mf bxmoqe gzgegmx, gzoayradfmnxq, mzp puefmzf rday ftq pqbaeufadk ar ftqud bgnxuo Dqoadpe, rad ftq eaxq bgdbaeq ar rmfusguzs ftqy uzfa oaybxumzoq iuft tue yqmegdqe.
Tq tme pueeaxhqp Dqbdqeqzfmfuhq Tageqe dqbqmfqpxk, rad abbaeuzs iuft ymzxk rudyzqee tue uzhmeuaze az ftq dustfe ar ftq bqabxq.
Tq tme dqrgeqp rad m xazs fuyq, mrfqd egot pueeaxgfuaze, fa omgeq aftqde fa nq qxqofqp; itqdqnk ftq Xqsuexmfuhq baiqde, uzombmnxq ar Mzzutuxmfuaz, tmhq dqfgdzqp fa ftq Bqabxq mf xmdsq rad ftqud qjqdoueq; ftq Efmfq dqymuzuzs uz ftq yqmz fuyq qjbaeqp fa mxx ftq pmzsqde ar uzhmeuaz rday iuftagf, mzp oazhgxeuaze iuftuz.
Tq tme qzpqmhagdqp fa bdqhqzf ftq babgxmfuaz ar ftqeq Efmfqe; rad ftmf bgdbaeq anefdgofuzs ftq Xmie rad Zmfgdmxulmfuaz ar Radquszqde; dqrgeuzs fa bmee aftqde fa qzoagdmsq ftqud yusdmfuaze tuftqd, mzp dmueuzs ftq oazpufuaze ar zqi Mbbdabdumfuaze ar Xmzpe.
Tq tme anefdgofqp ftq Mpyuzuefdmfuaz ar Vgefuoq, nk dqrgeuzs tue Meeqzf fa Xmie rad qefmnxuetuzs Vgpuoumdk baiqde.
Tq tme ympq Vgpsqe pqbqzpqzf az tue Iuxx mxazq, rad ftq fqzgdq ar ftqud arruoqe, mzp ftq myagzf mzp bmkyqzf ar ftqud emxmduqe.
Tq tme qdqofqp m ygxfufgpq ar Zqi Arruoqe, mzp eqzf tuftqd eimdye ar Arruoqde fa tmdmee agd bqabxq, mzp qmf agf ftqud egnefmzoq.
Tq tme wqbf myazs ge, uz fuyqe ar bqmoq, Efmzpuzs Mdyuqe iuftagf ftq oazeqzf ar agd xqsuexmfgdqe.
Tq tme mrrqofqp fa dqzpqd ftq Yuxufmdk uzpqbqzpqzf ar mzp egbqduad fa ftq Ouhux baiqd.
Tq tme oaynuzqp iuft aftqde fa egnvqof ge fa m vgduepuofuaz radqusz fa agd oazefufgfuaz mzp gzmowzaixqpsqp nk agd xmie; suhuzs tue Meeqzf fa ftqud Mofe ar bdqfqzpqp Xqsuexmfuaz:
Rad Cgmdfqduzs xmdsq napuqe ar mdyqp fdaabe myazs ge:
Rad bdafqofuzs ftqy, nk m yaow Fdumx, rday bgzuetyqzf rad mzk Ygdpqde ituot ftqk etagxp oayyuf az ftq Uztmnufmzfe ar ftqeq Efmfqe:
Rad ogffuzs arr agd Fdmpq iuft mxx bmdfe ar ftq iadxp:
Rad uybaeuzs Fmjqe az ge iuftagf agd Oazeqzf:
Rad pqbduhuzs ge, uz ymzk omeqe, ar ftq nqzqrufe ar Fdumx nk Vgdk:
Rad fdmzebadfuzs ge nqkazp Eqme fa nq fduqp rad bdqfqzpqp arrqzoqe:
Rad mnaxuetuzs ftq rdqq Ekefqy ar Qzsxuet Xmie uz m zqustnagduzs Bdahuzoq, qefmnxuetuzs ftqdquz mz Mdnufdmdk sahqdzyqzf, mzp qzxmdsuzs ufe Nagzpmduqe ea me fa dqzpqd uf mf azoq mz qjmybxq mzp ruf uzefdgyqzf rad uzfdapgouzs ftq emyq mneaxgfq dgxq uzfa ftqeq Oaxazuqe:
Rad fmwuzs mimk agd Otmdfqde, mnaxuetuzs agd yaef hmxgmnxq Xmie, mzp mxfqduzs rgzpmyqzfmxxk ftq Radye ar agd Sahqdzyqzfe:
Rad egebqzpuzs agd aiz Xqsuexmfgdqe, mzp pqoxmduzs ftqyeqxhqe uzhqefqp iuft baiqd fa xqsuexmfq rad ge uz mxx omeqe itmfeaqhqd.
Tq tme mnpuomfqp Sahqdzyqzf tqdq, nk pqoxmduzs ge agf ar tue Bdafqofuaz mzp imsuzs Imd msmuzef ge.
Tq tme bxgzpqdqp agd eqme, dmhmsqp agd Oamefe, ngdzf agd faize, mzp pqefdakqp ftq xuhqe ar agd bqabxq.
Tq ue mf ftue fuyq fdmzebadfuzs xmdsq Mdyuqe ar radqusz Yqdoqzmduqe fa oaybxqmf ftq iadwe ar pqmft, pqeaxmfuaz mzp fkdmzzk, mxdqmpk nqsgz iuft oudogyefmzoqe ar Odgqxfk mzp bqdrupk eomdoqxk bmdmxxqxqp uz ftq yaef nmdnmdage msqe, mzp fafmxxk gziadftk ftq Tqmp ar m ouhuxulqp zmfuaz.
Tq tme oazefdmuzqp agd rqxxai Oufulqze fmwqz Ombfuhq az ftq tust Eqme fa nqmd Mdye msmuzef ftqud Oagzfdk, fa nqoayq ftq qjqogfuazqde ar ftqud rduqzpe mzp Ndqftdqz, ad fa rmxx ftqyeqxhqe nk ftqud Tmzpe.
Tq tme qjoufqp payqefuo uzegddqofuaze myazsef ge, mzp tme qzpqmhagdqp fa nduzs az ftq uztmnufmzfe ar agd rdazfuqde, ftq yqdouxqee Uzpumz Emhmsqe, itaeq wzaiz dgxq ar imdrmdq, ue mz gzpuefuzsguetqp pqefdgofuaz ar mxx msqe, eqjqe mzp oazpufuaze.
Uz qhqdk efmsq ar ftqeq Abbdqeeuaze Iq tmhq Bqfufuazqp rad Dqpdqee uz ftq yaef tgynxq fqdye: Agd dqbqmfqp Bqfufuaze tmhq nqqz mzeiqdqp azxk nk dqbqmfqp uzvgdk. M Bduzoq itaeq otmdmofqd ue ftge ymdwqp nk qhqdk mof ituot ymk pqruzq m Fkdmzf, ue gzruf fa nq ftq dgxqd ar m rdqq bqabxq.
Zad tmhq Iq nqqz imzfuzs uz mffqzfuaze fa agd Ndufuet ndqftdqz. Iq tmhq imdzqp ftqy rday fuyq fa fuyq ar mffqybfe nk ftqud xqsuexmfgdq fa qjfqzp mz gzimddmzfmnxq vgduepuofuaz ahqd ge. Iq tmhq dqyuzpqp ftqy ar ftq oudogyefmzoqe ar agd qyusdmfuaz mzp eqffxqyqzf tqdq. Iq tmhq mbbqmxqp fa ftqud zmfuhq vgefuoq mzp ymszmzuyufk, mzp iq tmhq oazvgdqp ftqy nk ftq fuqe ar agd oayyaz wuzpdqp fa puemhai ftqeq gegdbmfuaze, ituot, iagxp uzqhufmnxk uzfqddgbf agd oazzqofuaze mzp oaddqebazpqzoq. Ftqk faa tmhq nqqz pqmr fa ftq hauoq ar vgefuoq mzp ar oazemzsguzufk. Iq ygef, ftqdqradq, mocguqeoq uz ftq zqoqeeufk, ituot pqzagzoqe agd Eqbmdmfuaz, mzp taxp ftqy, me iq taxp ftq dqef ar ymzwuzp, Qzqyuqe uz Imd, uz Bqmoq Rduqzpe.
Iq, ftqdqradq, ftq Dqbdqeqzfmfuhqe ar ftq gzufqp Efmfqe ar Myqduom, uz Sqzqdmx Oazsdqee, Meeqynxqp, mbbqmxuzs fa ftq Egbdqyq Vgpsq ar ftq iadxp rad ftq dqofufgpq ar agd uzfqzfuaze, pa, uz ftq Zmyq, mzp nk ftq Mgftadufk ar ftq saap Bqabxq ar ftqeq Oaxazuqe, eaxqyzxk bgnxuet mzp pqoxmdq, Ftmf ftqeq Gzufqp Oaxazuqe mdq, mzp ar Dustf agstf fa nq Rdqq mzp Uzpqbqzpqzf Efmfqe; ftmf ftqk mdq Mneaxhqp rday mxx Mxxqsumzoq fa ftq Ndufuet Odaiz, mzp ftmf mxx baxufuomx oazzqofuaz nqfiqqz ftqy mzp ftq Efmfq ar Sdqmf Ndufmuz, ue mzp agstf fa nq fafmxxk pueeaxhqp; mzp ftmf me Rdqq mzp Uzpqbqzpqzf Efmfqe, ftqk tmhq rgxx Baiqd fa xqhk Imd, oazoxgpq Bqmoq, oazfdmof Mxxumzoqe, qefmnxuet Oayyqdoq, mzp fa pa mxx aftqd Mofe mzp Ftuzse ituot Uzpqbqzpqzf Efmfqe ymk ar dustf pa. Mzp rad ftq egbbadf ar ftue Pqoxmdmfuaz, iuft m rudy dqxumzoq az ftq bdafqofuaz ar puhuzq Bdahupqzoq, iq ygfgmxxk bxqpsq fa qmot aftqd agd Xuhqe, agd Radfgzqe mzp agd emodqp Tazad.
Ftq euszqde ar ftq Pqoxmdmfuaz dqbdqeqzfqp ftq zqi efmfqe me raxxaie:
Zqi Tmybetudq
Vaeumt Nmdfxqff, Iuxxumy Itubbxq, Ymfftqi Ftadzfaz
Ymeemotgeqffe
Vatz Tmzoaow, Emygmx Mpmye, Vatz Mpmye, Danqdf Fdqmf Bmuzq, Qxndupsq Sqddk
Dtapq Uexmzp
Efqbtqz Tabwuze, Iuxxumy Qxxqdk
Oazzqofuogf
Dasqd Etqdymz, Emygqx Tgzfuzsfaz, Iuxxumy Iuxxumye, Axuhqd Iaxoaff
Zqi Kadw
Iuxxumy Rxakp, Btuxub Xuhuzsefaz, Rdmzoue Xqiue, Xqiue Yaddue
Zqi Vqdeqk
Duotmdp Efaowfaz, Vatz Iuftqdebaaz, Rdmzoue Tabwuzeaz, Vatz Tmdf, Mndmtmy Oxmdw
Bqzzekxhmzum
Danqdf Yaddue, Nqzvmyuz Dget, Nqzvmyuz Rdmzwxuz, Vatz Yadfaz, Sqadsq Oxkyqd, Vmyqe Eyuft, Sqadsq Fmkxad, Vmyqe Iuxeaz, Sqadsq Daee
Pqxmimdq
Omqemd Dapzqk, Sqadsq Dqmp, Ftayme YoWqmz
Ymdkxmzp
Emygqx Otmeq, Iuxxumy Bmom, Ftayme Efazq, Otmdxqe Omddaxx ar Omddaxxfaz
Hudsuzum
Sqadsq Ikftq, Duotmdp Tqzdk Xqq, Ftayme Vqrrqdeaz, Nqzvmyuz Tmddueaz, Ftayme Zqxeaz, Vd., Rdmzoue Xustfraaf Xqq, Omdfqd Ndmjfaz
Zadft Omdaxuzm
Iuxxumy Taabqd, Vaeqbt Tqiqe, Vatz Bqzz
Eagft Omdaxuzm
Qpimdp Dgfxqpsq, Ftayme Tqkimdp, Vd., Ftayme Xkzot, Vd., Mdftgd Yuppxqfaz
Sqadsum
Ngffaz Siuzzqff, Xkymz Tmxx, Sqadsq Imxfaz]
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
const int ARRAYSIZE = 128;
void characterCount(char ch, int list[]);
void calcShift( int& shift, int list[]);
void writeOutput(ifstream &in, ofstream &out, int shift);
int main()
{
int asciiCode = 0,
shift = 0;
string filename;
char ch;
ifstream infile;
ofstream outfile;
string reply;
//input file
cout << "Input file name: ";
getline(cin, filename);
infile.open(filename.c_str());
if (!infile.is_open()) {
cout << "Unable to open file or it doesn't exist." << endl;
return 1;
}
cout << "Output file name: ";
getline(cin, filename);
outfile.open(filename.c_str());
int list[ARRAYSIZE] = {0};
while (infile.peek() != EOF)
{
infile.get(ch);
characterCount(ch, list);
}
infile.clear();
infile.seekg(0);
calcShift (shift, list); //Calculate the shift based on the <strong class="highlight">most</strong> characters counted
writeOutput(infile, outfile, shift); //Decypher and write to the other document
return 0;
outfile.close();
}
void characterCount(char ch, int list[])
{
if (ch >= 'A' && ch <= 'z') //If the character is in the alphabet...
{
int asciiCode = 0;
asciiCode = static_cast<int>(ch); //Change it to the ASCII number
list[asciiCode]++; //And note it on the array
}
}
void calcShift( int& shift, int list[])
{
int maxIndex = 0; //Asuming that list[0] is the largest
for (int i = 1; i < ARRAYSIZE; i++)
{
if (list[maxIndex] < list[i])
maxIndex = i; //If this is true, change the largest index
}
if (maxIndex >= 'A' && maxIndex <= 'Z') //Calculate shift with <strong class="highlight">E</strong> (for upper-case letters)
shift = 'E' - maxIndex;
if (maxIndex >= 'a' && maxIndex <= 'z') //For lower-case letters (<strong class="highlight">e</strong>)
shift = 'e' - maxIndex;
}
void writeOutput(ifstream &infile, ofstream &outfile, int shift)
{
char ch;
while (infile.peek() != EOF) { //Until it is the end of the file...
infile.get(ch); //Get the next character
if (ch >= 'A' && ch <= 'Z') //If the character is in the alphabet...
{
ch = 'A' + (((ch - 'A') + shift + 26) % 26);
}
if (ch >= 'a' && ch <= 'z') //If the character is in the alphabet...
{
ch = 'a' + (((ch - 'a') + shift + 26) % 26);
}
outfile << ch; //Print to the outfile
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.