Simple C++ Encryption BACKGROUND INFORMATION: A cryptosystem is a system for enc
ID: 3685674 • Letter: S
Question
Simple C++ Encryption
BACKGROUND INFORMATION: A cryptosystem is a system for encryption and decryption. Encryption, also called encoding or enciphering, involves changing the original message into a secret message, while decryption, also called decoding or deciphering, involves just the opposite- changing the secret message back into its original readable form. In cryptography, a plaintext file is a file containing data in its original, or readable, form while a ciphertext file is a file containing data in coded, or scrambled, form. In this assignment, you will be creating a polyalphabetic cipher based on substitution using multiple substitution alphabets. In this cipher, instead of a single key, multiple one-letter keys are used to encrypt the original message by shifting the original letter to a new, encrypted letter corresponding to the key. For example, consider the following modified case from Wikipedia where the plaintext to be encrypted is: ATTACK AT DAWN. Then, the person sending the message chooses a keyword and repeats it until it matches the length of the plaintext so that the key LEMON becomes LEMONL EM ONLE (accounting for the spaces in the plaintext message which are not encrypted or decrypted). Then, the first letter of the plaintext, A, is paired with L, the first letter of the key. Assuming the shift starts with A = 0, B = 1, etc., we find that L is 11, so the plaintext A "shifts" 11 places in the alphabet to ciphertext L. The second letter of the plaintext, T, gets paired with E, the second letter of the key, so it shifts 4 (the shift value of E) places in the alphabet to ciphertext x. However, note that the third letter of the plaintext, T, gets mapped to a different ciphertext because It is paired with M, the third letter of the key. Of interest, is that this ciphertext wraps around to F based on shifting 12 places in the alphabet. The rest of the plaintext is enciphered in a similar fashionExplanation / Answer
#include<iostream>
#include<string.h>
using namespace std;
void encrypt(string ,string ,string);
void decrypt(string ,string ,string);
void main()
{
char c;
string infile,outfile,key;
do
{
cout<< "would you like to encrypt or decrypt a file (E or D)" <<endl;
cin>>c;
}while(c!='E' || c!='D');
cout << "Enter the name of input file you want to encrypt " << endl;
cin>>infile;
cout << "Enter the name of output file to write cipher text " << endl;
cin>>outfile;
cout << " Enter 5-letter key to encrypt file : " << endl;
cin>>key;
if( c== 'E')
encrypt(infile,outfile,key);
else
decrypt(infile,outfile,key);
}
void encrypt(string infile,string outfile,string key)
{
int i,j=0;
int tmp1,tmp2;
int dtln,kln,sum;
string data,key,cipher;
ifstream file(infile);
long length = infile.tellg();
file.read(data,length);
for(i=kln;i<dtln;i++)
{
if(data[i]==32)
{
i++;
}
key[i]=key[j];
j++;
}
for(i=0;i<dtln;i++)
{
if(data[i]==32)
{
cipher[i]=' ';
i++;
}
else if(data[i]<=90 && data[i]>=65)
{
tmp1=data[i]-65;
}
else if(data[i]<=123 && data[i]>=97)
{
tmp1=data[i]-97;
}
tmp2=key[i]-64;
sum=tmp1+tmp2;
if(sum>26)
{
sum=sum-26;
cipher[i]=sum+64;
}
else
{
cipher[i]=sum+64;
}
}
cipher[i++]='';
ofstream f;
f.open(outfile);
f << cipher;
f.close();
}
void decrypt(string infile,string outfile,string key){
int i,j=0;
int tmp1,tmp2;
int dtln,kln,sum;
string data,key,cipher;
ifstream file(infile);
long length = infile.tellg();
file.read(data,length);
for(i=kln;i<dtln;i++)
for(i=0;cipher[i]!='';i++)
{
tmp1=cipher[i]-64;
tmp2=key[i]-65;
sum=tmp1-tmp2;
if(sum<0)
{
sum=sum+26;
cipher[i]=sum+64;
}
else
{
cipher[i]=sum+64;
}
}
cipher[i++]='';
ofstream f;
f.open(outfile);
f << cipher;
f.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.