C++ programming. code needs to output sample The Affine cipher is a cryptographi
ID: 3754441 • Letter: C
Question
C++ programming. code needs to output 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-(a, 3), the encryption and decryption are defined as: Encryption: c-(a r B) mod 26, 1Sa 26 and 1SB 26 Decryption: x = a-1, (c-d) mod 26. Here, 1 is a multiplicative inverse of a in the group of integers modulo 26. To find the multiplicative inverse of one needs to find z such that a -1 mod 26 A simple way of finding the inverse of a is to consider all numbers from 1 to 25 and see which one satisfies equation 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 so the inverse of 5 is 21. Using 21 for decrypting the first letter "" (or 22) becomes 21 (22-7) mod 263, 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 "de- crypted.txtA 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 programExplanation / Answer
#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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.