Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Programming. Output needs to be like sample! The Affine cipher is a cryptogr

ID: 3753386 • Letter: C

Question

C++ Programming. Output needs to be like sample!

The Affine cipher is a cryptographic method for encrypting text such that it becomes unreadable to a party without access to the cryptographic key. The encryption and decryption operations are simple shifts of the alphabet letters in a cyclic fashion. Mathematically, for a key k = (, ), the encryption and decryption are defined as: Encryption: c = ( · x + ) mod 26 , 1 26 and 1 26 Decryption: x = 1 · (c ) mod 26. Here, 1 is a multiplicative inverse of in the group of integers modulo 26. To find the multiplicative inverse of one needs to find x such that · x = 1 mod 26. (1) A simple way of finding the inverse of is to consider all numbers from 1 to 25 and see which one satisfies equation (1). To illustrate the use of the Affine cipher, consider the encryption of “defend the east wall of the castle” with key k = (, ) = (5, 7). The first letter “d” is mapped to number 3 (alphabet letters are numbered from 0 to 25). Inserting 3 to the encryption functions yields c = 5 3 + 7 mod 26 = 22 mod 26 = 22 which corresponds to letter “w”. Applying the same process for ever letter, the plaintext sentence is translated to “wbgbuw yqb bhty nhkk zg yqb rhtykb” . Now to decode, we first need to find the inverse of 5 modulo 26. Scanning every number from 1 to 25, we observe that 5 · 21 mod 26 = 1, so the inverse of 5 is 21. Using 21 for decrypting the first letter “w” (or 22) becomes 21 · (22 7) mod 26 = 3, which reverses back to letter “d”. NOTE: The modulo operation for negative numbers is different from the % arithmetic operator in C. Write a C program that decrypts a file named “encrypted.txt” and places the decryption output to a file called “decrypted.txt”. A file encrypted with k = (5, 7) is provided with the assignment. You can use it to test your decryption function. You will know when you have succeeded because the text becomes readable. In your program 1 • Ask the user to enter the decryption key. • Repeat the request until the right key is entered. • Display a message when file decryption is over. • Ask the user to enter a key to re-encrypt the file. • Re-encrypt file ”decrypted.txt” and store it at ”encrypted.txt” • Display a message when file encryption is over. Only alphabet letters (uppercase/lowercase) must be encrypted. The remaining characters (question marks, periods, etc. must remain intact). Your code must be modular. Use the following function prototypes for encryption and decryption: char encryptFun(int a, int b, char letter); // This function receives as input the key components (, ) and the plaintext letter and returns the encrypted letter. char decryptFun(int a, int b, char letter); // This function receives as input the key components (, ) and an encrypted letter and returns the decrypted letter. int inverse(int x); // This function as input receives an int x and returns the multiplicative inverse of x modulo 26. You are also given the following function that correctly implements the modulo operation for both positive and negative numbers int mod (int x, int y){ while(x < 0){ x+ = y; } return x%y; }

Sample Code Execution: Bold text indicates information entered by the user

Enter the decryption key:2 7

Wrong key

Enter the decryption key:5 7

File decrypted!

Do you want to re-encrypt the file (y/n)? y

Enter an encryption key: 7 19

File encrypted!

Goodbye

This is the encripted text.

bibox whx, nb rqhulb yqb nzokw, mdy yz rqhulb yqb nzokw vu h nhx yqhy pbhut huxyqvul, yqhy yhfbt pzob yvpb yqhu pzty ebzekb qhib. vy ubibo qheebut hkk hy zurb. vy’t tkzn. vy’t pbyqzwvrhk. vy’t bsqhdtyvul. nb wzu’y hkk qhib yqb tyzphrq gzo vy.

whbpzut, yqbx rhkk yqbp. yqbx ebogzop hryvzu nvyqzdy dtbo vuybohryvzu. pzuvyzovul, kzllvul, uzyvgvrhyvzut. eovphk dolbt, obeobttbw pbpzovbt, durzutrvzdt qhmvyt. yqbx’ob hknhxt yqbob, hknhxt hryvib. nb rhu yox yz mb ovlqy, nb rhu yox yz mb lzzw. nb rhu yox yz phfb h wvggboburb, mdy vy’t hkk h kvb. rhdtb vuybuyvzut hob voobkbihuy. yqbx wzu’y wovib dt. whbpzut wz.

pzty rzwbot yqvuf wbmdllvul tzgynhob vt hmzdy gvsvul h pvtyhfb, mdy yqhy’t uzutbutb. wbmdllvul’t hrydhkkx hkk hmzdy gvuwvul yqb mdl, hmzdy duwbotyhuwvul nqx yqb mdl nht yqbob yz mblvu nvyq, hmzdy fuznvul yqhy vyt bsvtyburb nht uz hrrvwbuy. vy rhpb yz xzd yz wbkvibo h pbtthlb, kvfb hu durzutrvzdt mdmmkb gkzhyvul yz yqb tdoghrb, ezeevul nvyq h obibkhyvzu xzd’ib tbrobykx fuznu hkk hkzul.

h tvpekb eozlohp, h nzop yqhy rhu phfb whyh duobhwhmkb, phknhob yqhy yzzf Whokbub phxmb ynz qzdot yz rzwb. vt yqhy obhkkx hkk vy yhfbt yz fvkk yqb nzokw?

yqb rzurbey zg nhvyvul mbnvkwbot pb. yqbob hob hknhxt wbhwkvubt. yqbob hob hknhxt yvrfvul rkzrft. yqhy’t nqx xzd pdty phuhlb xzdo yvpb.

tdob, yqbob hob lohxt, mdy nqbu xzd rzpb ovlqy wznu yz vy, hy vyt rzob, mbubhyq bibox rqzvrb, yqbob’t bvyqbo h zub zo h cboz. xzd bvyqbo wz tzpbyqvul zo xzd wzu’y. xzd nhkf zdy yqhy wzzo, xzd’ib wbrvwbw yz wz uzyqvul, yz thx uz, nqvrq pbhut xzd wz uzy rzpb mhrf. xzd kbhib, xzd hob uz kzulbo h ehoy zg yqvt. xzd mbrzpb h cboz. vg xzd tyhx, vg xzd nhuy yz rqhulb yqb nzokw, xzd mbrzpb h xbt. xzd mbrzpb h zub. tz, v’kk htf xzd hlhvu: hob xzd h zub zo h cboz?

rqzvrbt. phxmb po. ozmzy’t ovlqy. yqhy’t nqhy yqvt vt hkk hmzdy, yqb xbttbt huw uzt zg kvgb. mdy wz nb wbrvwb yqbp zo wz yqbx wbrvwb dt?

Explanation / Answer

Hi please follow the below instructions to meet our requirement
Source code:-
==========
#include<iostream>  
#include<conio.h>  
#include<fstream>
using namespace std;
class Caesar  
{  
public: void encrypt(char *inp,char *out,int key);  
void decrypt(char *inp,char *out,int key);  
void readText(char *inp);  
};  
void Caesar::encrypt(char *inp,char *out,int key)  
{  
ifstream input;  
ofstream output;  
char buf;  
input.open(inp);  
output.open(out);  
buf=input.get();  
while(!input.eof())  
{  
if(buf>='a'&&buf<='z')  
{  
buf-='a';  
buf+=key;  
buf%=26;  
buf+='A';  
}  
output.put(buf);  
buf=input.get();  
}  
input.close();  
output.close();  
readText(inp);  
readText(out);  
}  
void Caesar::decrypt(char *inp,char *out,int key)  
{  
ifstream input;  
ofstream output;  
char buf;  
input.open(inp);  
output.open(out);  
buf=input.get();  
while(!input.eof())  
{  
if(buf>='A'&&buf<='Z')  
{  
buf-='A';  
buf+=26-key;  
buf%=26;  
buf+='a';  
}  
output.put(buf);  
buf=input.get();  
}  
input.close();  
output.close();  
readText(inp);  
readText(out);  
}  
void Caesar::readText(char *inp)  
{  
ifstream input;  
char buf;  
input.open(inp);  
cout<<" <--- "<<inp<<" ---> ";  
buf=input.get();  
while(!input.eof())  
{  
cout<<buf;  
buf=input.get();  
}  
input.close();  
}  
int main()  
{  
Caesar a;  
int choice,key;  
char inp[30],out[30];  
cout<<" Enter input file: ";  
cin>>inp;  
cout<<" Enter output file: ";  
cin>>out;  
cout<<" Enter key: ";  
cin>>key;  
cout<<" 1. Encrypt 2. Decrypt Select choice(1 or 2): ";  
cin>>choice;  
if(choice==1)  
a.encrypt(inp,out,key);  
else if(choice==2)  
a.decrypt(inp,out,key);  
else cout<<" Unknown choice";  
getch();  
}

sample source file2:

==============


#include<iostream>
#include<stdlib.h>
#include<fstream>
using namespace std;
class transposition_cipher
{
public:
ifstream inputfile;  
ofstream outputfile;
public:
void encryption(char *input,char *output,int key) //Encryption of the data.
{  
char buffer;  
inputfile.open(input); //Open the input file to read the encryption Message
outputfile.open(output); //Open the Output file to write Original Meesage
buffer=inputfile.get();  
while(!inputfile.eof())  
{  
if(buffer>='a'&&buffer<='z')  
{  
buffer-='a';  
buffer+=key;  
buffer%=26;  
buffer+='A';  
}  
outputfile.put(buffer);  
buffer=inputfile.get();  
}  
inputfile.close(); //close the file
outputfile.close(); //close the File
readText(input); //Read the Text
readText(output);  
}  
public:
void readText(char *text)  
{
char buffer;  
inputfile.open(text);  
cout<<" *** "<<text<<" *** ";  
buffer=inputfile.get();  
while(!inputfile.eof())  
{  
cout<<buffer;  
buffer=inputfile.get();  
}  
inputfile.close();  
}  
public:
void decryption(char *input,char *output,int key) //Decrption of the Message
{
char buffer;  
inputfile.open(input);  
outputfile.open(output);  
buffer=inputfile.get();  
while(!inputfile.eof())  
{   
if(buffer>='A'&&buffer<='Z')  
{  
buffer-='A';  
buffer+=26-key;  
buffer%=26;  
buffer+='a';  
}  
outputfile.put(buffer);  
buffer=inputfile.get();  
}  
inputfile.close();  
outputfile.close();  
readText(input);  
readText(output);  
}
};
int main()
{
transposition_cipher obj;  
int option,key;  
char inputfile[100],outputfile[100];  
cout<<" ----------------------------------------------------------------"<<endl;
cout<<" *** Encryption and Decryption using caesar cipher Technique ***"<<endl;
cout<<" ----------------------------------------------------------------"<<endl;
cout<<" Please Enter input file: "<<endl;  
cin>>inputfile;  
cout<<" Please Enter output file: "<<endl;  
cin>>outputfile;  
cout<<" Please Enter Cipher key: "<<endl;  
cin>>key;
while(true)
{
cout<<" *** MENU ***"<<endl;
cout<<" 1.Encrypt a File"<<endl;
cout<<" 2.Decrypt a File"<<endl;
cout<<" 3.About"<<endl;
cout<<" 4.Quit"<<endl;
cout<<" Please Select any option: "<<endl;
cin>>option;
switch(option)
{
case 1:
obj.encryption(inputfile,outputfile,key);
break;
case 2:
obj.decryption(inputfile,outputfile,key);
break;
case 3:
cout<<" Encryption and Decrption"<<endl;
break;
case 4:
cout<<" Bye"<<endl;
exit(0);
default:
cout<<" !invalid input"<<endl;
}
}
return 0;
}


Sample Output:-
-------------------
----------------------------------------------------------------
Please Enter input file:
input_text.txt
Please Enter output file:
outputtext.txt
Please Enter Cipher key:
7
*** MENU ***
1.Encrypt a File
2.Decrypt a File
3.About
4.Quit
Please Select any option:
1
*** input_text.txt ***
Welcome to the World
*** outputtext.txt ***
WLSJVTL AV AOL WVYSK
*** MENU ***
1.Encrypt a File
2.Decrypt a File
3.About
4.Quit
Please Select any option:
2
*** input_text.txt ***
Welcome to the World
*** outputtext.txt ***
pelcome to the porld
*** MENU ***
1.Encrypt a File
2.Decrypt a File
3.About
4.Quit
Please Select any option:
3
Encryption and Decrption
*** MENU ***
1.Encrypt a File
2.Decrypt a File
3.About
4.Quit
Please Select any option:
4
Bye
--------------------------------
Process exited after 81.66 seconds with return value 0
Press any key to continue . . .

input file:-
------------------
input_text.txt
Welcome to the World

outputtext.txt:-
--------------
welcome to the world

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote