CSCE 1030: Homework 4 Due: 11:59 PM on Friday, November 3, 2017 BACKGROUND INFOR
ID: 3606642 • Letter: C
Question
CSCE 1030: Homework 4 Due: 11:59 PM on Friday, November 3, 2017 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 modified stream cipher that shifts each original letter or digit to a new, encrypted letter or digit, respectively, corresponding to individual, randomly generated integral key. For example, consider the plaintext to be encrypted is "ABC 1.", without the quotes. Then, for each letter and digit, we generate a random integer between 3 and 277 that serves as the key for the corresponding letters and digits. Consider the following example Plaintext: Key: Ciphertext: ABC 1 76 97 74 75 YUY 6 Thus, the A would be shifted 76 places in the alphabet, which means that it "rolls over twice, and then gets shifted 24 places in the alphabet to the ciphertext Y. Notice that there are only four keys generated for the six characters. Whitespace (and any unsupported characters) should be discarded, so no key is generated in this case. Note that the whitespace has been removed in the ciphertext. Additionally, punctuation should be kept as is, meaning that it is not discarded, but left in its original form (e.g., the"." in the above example remains a ".") in the ciphertext. Mathematically, each letter of the plaintext and key can be given a number and a formula can be derived to encrypt for c (i.e., ciphertext) and m (i.e., plaintext) using k (i.e., key) as follows C- (m + k) % 26 m-(26 + c-k) % 26 Note that formula assumes A is 0, B is 1, and so forth with nothing to distinguish between uppercase A and lowercase a. Since you will most likely be using the character ASCII values, you will have to modify these formulas to fit your needs (e.g., the letter A has an ASCIl value of 65), but this should give you a place to start. One other item to consider is that characters (i.e., letters and digits in this case) roll over. For example, in shifting the letter z, then next letter would be the letter A (both uppercase)Explanation / Answer
// C++ Solution for ecrypting the file and decrypting the file
#include<bits/stdc++.h>
using namespace std;
// Encryption function
void encryptFile(char *input , char *key, char *output)
{
int Kys[100];
FILE *fp = fopen(input,"r");
int i;
FILE *fp2 = fopen(key,"r");
if(fp2==NULL)
{ cout<<"No such file or directory ";
return;
}
else
{
i=0;
int val;
while(fscanf(fp2,"%d",&val)!=EOF)
{ Kys[i++] = val;
// cout<<val<<" ";
}
fclose(fp2);
}
FILE *fp3 = fopen(output,"w");
char c;
i=0;
while((c = getc(fp))!=EOF)
{
cout<<c<<" ";
if(c=='.' || c==',')
fputc(c,fp3);
else if(c==' ' || !((c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9')))
continue;
else
{
int val=c;
char val;
val = ((c+Kys[i++])%26);
// cout<<val<<" ";
c = c+val;
if(c>='9'
fputc(val, fp3);
}
c = getc(fp);
}
fclose(fp3);
fclose(fp);
}
// Decryption function
void decryptFile(char *plain, char *key, char *output)
{
FILE *fp = fopen(plain,"r");
int i;
int Kys[100];
FILE *fp2 = fopen(key,"r");
if(fp2==NULL)
{ cout<<"No Such file or directory ";
return;
}
else
{
i=0;
int val;
while(fscanf(fp2,"%d",&val)!=EOF)
Kys[i++] = val;
fclose(fp2);
}
char c;
i = 0;
FILE *fp3 = fopen(output,"w");
while((c = getc(fp))!=EOF)
{
if(c=='.')
fputc(c,fp3);
else if(c==' ' || !((c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9')))
continue;
else
{
c = (26+c-Kys[i++])%26;
fputc(c,fp3);
}
}
fclose(fp3);
fclose(fp);
}
// Driver function
int main()
{
string dept = "Computer Science and Engineering";
string course = "CSCE 1030 - Computer Science";
string name = "Student Name";
string Euid = "EUID";
string email = "euid@my.unt.edu";
char choice;
char input[50];
char key[50];
char output[50];
while(1)
{
cout<<"------------------------------------------ ";
cout<<dept<<" ";
cout<<course<<" ";
cout<<name<<" "<<Euid<<" "<<email<<" ";
cout<<"------------------------------------------- ";
cout<<"Would you like to ENCRYPT or DECRYPT a file(E or D) ?";
cin>>choice;
if(choice=='D')
cout<<" Enter the name of your input file you want to decrypt: ";
else if(choice=='E')
cout<<" Enter the name of your input file you want to encrypt: ";
else
continue;
cin>>input;
cout<<" Enter the name of the output file to write the plaintext: ";
cin>>output;
cout<<" Enter the file name that contains your encryption keys: ";
cin>>key;
FILE *fp = fopen(input,"r");
if(fp==NULL)
{ cout<<"Unable to open file: "<<input<<" Terminating... ";
continue;
}
else
{
fclose(fp);
char plain[100];
char c;
int i=0;
if(choice=='E')
{
encryptFile(input,key,output);
}
else if(choice=='D')
{
decryptFile(input,key,output);
}
break;
}
}
return 0;
}
// Please let me know if you have any doubts
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.