Program needs to be coded in C++ This program reads in three files: `ciphertext.
ID: 3888959 • Letter: P
Question
Program needs to be coded in C++
This program reads in three files: `ciphertext.txt`, `cipher_freq.txt`, and `corpus_freq.txt`.
The output of this program should be a file called `cracked.txt`. It should be the result of replacing each charact of ciphertext.txt with a capital Roman letter or a space. The correspondnce is given by the files `cipher_freq.txt` and `corpus_freq.txt`. The character in row $i$ of `cipher_freq.txt` should be replaced with the letter (or space) in row $i$ of `corpus_freq.txt`. (Note that for this to work, both `cipher_freq.txt` and `corpus_freq.txt` must be sorted by frequency.)
Explanation / Answer
The encryption and decryption of the file is as follows:
main.cpp
#include<iostream>
#include<fstream>
#include<stdio.h>
int main()
{
char namess[30],tar[30],choices,modified;
int no[100],uu,ch;
cout<<"Enter Your Option ";
cout<<"
1. To Encrypt The File ";
cout<<"
2. To Decrypt The File ";
cout<<"
Option : ";
cin>>ch;
if(ch==1)
{
cout<<"Enter The Path Of A File Which Is To Be Encrypted : ";
gets(namess);
ifstream fin(namess,ios::binary);
if(!fin)
{
cout<<"
Error In Openinig Of A File : ";
return 1;
}
cout<<"
Enter The New Encrypted File Name : ";
gets(tar);
ofstream fout(tar,ios::binary);
if(!fout)
{
cout<<"
Error In Opening Of Target File : ";
return 1;
}
for(uu=0;uu<9;uu++)
{
no[uu]=uu;
}
for(uu=14;uu<31;uu++)
{
no[uu-5]=uu;
}
for(uu=33;uu<=68;uu++)
{
no[uu-7]=uu;
}
for(uu=97;uu<=122;uu++)
{
no[uu-35]=uu;
}
while(fin)
{
fin.get(choices);
if(choices==EOF)break;
if((choices>=97) && (choices<=122))
{
uu=97;
modified=no[choices-uu];
fout<<modified;
}
if((choices>=65) && (choices<=90))
{
uu=39;
modified=no[choices-uu];
fout<<modified;
}
if((choices>=48) && (choices<=57))
{
uu=4;
modified=no[choices+uu];
fout<<modified;
}
if((choices==10)||(choices==13))
{
modified=choices;
fout<<modified;
}
if(choices==32)
fout<<choices;
if(choices==9)
fout<<choices;
if((choices>=33)&&(choices<=47))
{
modified=choices+64;
fout<<modified;
}
if((choices>=58)&&(choices<=64))
{
modified=choices+54;
fout<<modified;
}
if((choices>=91)&&(choices<=96))
{
modified=choices+28;
fout<<modified;
}
if((choices>=123)&&(choices<=126))
{
modified=choices-40;
fout<<modified;
}
}
fin.close();
fout.close();
cout<<"
Your File Is Encrypted Now........... ";
getch();
return 0;
}
if(ch==2)
{
char namess[30],tar[30],choices,modified;
int no[100],uu,changes;
cout<<"Enter The Path Of A File Name Which Is To Be Decrypted : ";
gets(namess);
ifstream fin(namess,ios::binary);
if(!fin)
{
cout<<"Error In Opening Of A File : ";
return 1;
}
cout<<"
Enter The New Decrypted File Name : ";
gets(tar);
ofstream fout;
fout.open(tar,ios::binary);
if(!fout)
{
cout<<"Error In Opening Of A Target File : ";
return 1;
}
for(uu=0;uu<9;uu++)
{
no[uu]=uu;
}
for(uu=14;uu<31;uu++)
{
no[uu-5]=uu;
}
for(uu=33;uu<=68;uu++)
{
no[uu-7]=uu;
}
while(fin)
{
fin.get(choices);
changes=0;
if(choices==EOF)break;
for(uu=26;uu<52;uu++)
{
if(choices==no[uu])
{
modified=uu+39;
fout<<modified;
changes=1;
break ;
}
}
if (changes==1) continue ;
for(uu=0;uu<26;uu++)
{
if(choices==no[uu])
{
modified=uu+97;
fout<<modified;
changes=1;break;
}
}
if (changes==1)
continue ;
for(uu=52;uu<62;uu++)
{
if(choices==no[uu])
{
modified=uu-4;
fout<<modified;
changes=1; break ;
}
}
if (changes==1) continue ;
if((choices==10)||(choices==13))
{
modified=choices;
fout<<modified;
}
if(choices==32)
fout<<choices;
if(choices==9)
fout<<choices;
if((choices>=97)&&(choices<=111))
{
modified=choices-64;
fout<<modified;
}
if((choices>=112)&&(choices<=118))
{
modified=choices-54;
fout<<modified;
}
if((choices>=119)&&(choices<=124))
{
modified=choices-28;
fout<<modified;
}
if((choices>=83)&&(choices<=86))
{
modified=choices+40;
fout<<modified;
}
}
fin.close();
fout.close();
cout<<"
The File Is Being Decrypted............ ";
getch();
return 0;
}
return 0;
}
Rate an upvote....Thankyou
Hope this helps.....
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.