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

(IN C WITH COMMENTS, USING strings and file pointers) cleardot.gif Cryptogram En

ID: 3827748 • Letter: #

Question

(IN C WITH COMMENTS, USING strings and file pointers) cleardot.gif Cryptogram Encoder. Cryptography is used to protect messages from unauthorized viewers. An algorithm using an encoding key scrambles the letters in a message string, making it unintelligible to anyone not having the key. A simple encoding algorithm involves substituting one lower case letter for another, e.g., replace each letter in the range ‘a’ to ‘z’ with a different letter in this range. To insure proper decoding, each letter should be used exactly once. A 26 character string is used to define this substitution. The positions in the string correspond to the alphabet order. The character at each position of the string is the value to be substituted. Note that all other characters (upper case characters, punctuation, whitespace, etc) are unchanged. Also note that the same program can be used for both encoding and decoding a message. The only thing that changes is the conversion string. Assignment. Write a program encode.c that encodes the original text file into encrypted text. Write a program decode.c that decodes the encrypted file into its original form. You should allow user to choose the source and destination file names, and also to provide the name of text file that contains the encoding key string of length 26. Details: To encode or decode a file you should 1.Define a 26 character conversion string (as described above) to encode the message. Place this string in file encodekey.txt 2.Write a function that converts the conversion string from encodekey.txt into its complement that allows a text file to be decoded.. This function should be a part of the decode.c program. 3.Create an input message file cryptoin.txt containing several lines of text with upper/lower case letters, numbers, punctuation, spaces etc all intermixed. The message should be readable. 4.Write program encode.c that uses the string from encodekey.txt to convert the source file (e.g. cryptoin.txt) to encrypted output file (e.g. cryptoout.txt). You should do the similar with decode.c program. Example: convert = cdefghirslmnopqjktuvwxyzab cryptoin.txt = Now is the time for all 99 men cryptoout.txt = Nqy su vrg vsog hqt cnn 99 ogp cryptoback.txt = Now is the time for all 99 men

Explanation / Answer

//File name: encode.c
#include<stdio.h>
#include<stdlib.h>

//To read file contents
void read_file(FILE *inp, char data[], int *length)
{
//To store length
*length = 0;
int i = 0;
//Checks if file pointer contains null then show error and exit
if (inp == NULL)
{
printf("Error! opening file");
// Program exits if file pointer returns NULL.
exit(1);
}//End of if
//Loops till end of file
while(!feof(inp))
{
//Reads data from file and stores in array
fscanf(inp, "%c", &data[i]);
//Increase the counter
i++;
}//End of while
//Stores null character at the end
data[--i] = '';
//Assigns the counter to length
*length = i;
// Close input file
close(inp);
}//End of function

//To encrypt data
void Encryption(char key[], char ori[], char enc[], int lenKey, int lenOri)
{
//Opens the file in write mode
FILE *fp3 = fopen("cryptoout.txt", "w");
int x, y, flag = 0, z = 0;
//Loops till end of the original file
for(x = 0; x < lenOri; x++)
{
//Loops 26 times for a - z lower case alphabet
for(y = 0; y < 26; y++)
{
//Checks if original x position character is equal to the ASCII value a - z
if(ori[x] == (97+y))
{
//Store the key y position data in encryption array z position
//Increase the value of z index position by one
enc[z++] = key[y];
//Set the flag to one
flag = 1;
//Come out of the loop
break;
}//End of if
}//End of inner for loop
//Checks if flag is zero then it is not a lower case character a - z
if(flag == 0)
{
//Store original data
enc[z++] = ori[x];
}//End of if
else
//Re set the flag to zero for next character
flag = 0;
}//End of outer for loop
//Set the null character at the end
enc[z] = '';
//If fp3 is null display error message
if (fp3 == NULL)
{
puts("Could not open files");
exit(0);
}//End of if
//Otherwise write data
else
{
//Loops till end of encrypted file
for(x = 0; enc[x] != ''; x++)
{
//Write data
fputc(enc[x], fp3);
}//End of for loop
}//End of else
close(fp3);
}//End of function

//Main function
void main()
{
//To store key value
char key[26];
//To store original data
char originalData[500];
//To store encrypted data
char encryptedData[500];
//To store length
int lenOri, lenKey;

//Opens encodekey.txt in read mode
FILE *f1 = fopen("encodekey.txt", "r");
//Opens cryptoin.txt in read mode
FILE *f2 = fopen("cryptoin.txt", "r");
//Calls the read file function
read_file(f2, originalData, &lenOri);

// Close file
close(f2);
//Read key file
read_file(f1, key, &lenKey);

// Close file
close(f1);
//To encrypt data
Encryption(key, originalData, encryptedData, lenKey, lenOri);
printf(" Key = %s", key);
printf(" Original = %s", originalData);
printf(" Encrypted = %s", encryptedData);
}//End of main

Output:

Key = zyxwvutsrqponmlkjihgfedcba
Original = Now is the time for all 99 men
Encrypted = Nld rh gsv grnv uli zoo 99 nvm

//File name: decode.c
#include<stdio.h>
#include<stdlib.h>

//To read file f1.txt and f2.txt
void read_file(FILE *inp, char data[], int *length)
{
//To keep track of length of characters in the file
*length = 0;
int i = 0;
//Checks if file pointer contains null then show error and exit
if (inp == NULL)
{
printf("Error! opening file");
// Program exits if file pointer returns NULL.
exit(1);
}//End of if
//Loops till end of file
while(!feof(inp))
{
//Reads data from file and stores in array
fscanf(inp, "%c", &data[i]);
//Increase the counter
i++;
}//End of while
//Puts null character
data[--i] = '';
//Assigns the counter to length
*length = i;
// Close input file
close(inp);
}//End of function

//Decryption operation
void Decryption(char key[], char ori[], char enc[], int lenKey, int lenEnc)
{
//Opens the file in write mode
FILE *fp1 = fopen("cryptoback.txt", "w");
int x, y, flag = 0, z = 0;
//Loops till end of the Encrypted file length
for(x = 0; x < lenEnc; x++)
{
//Loops for lowercase a to z 26 times
for(y = 0; y < 26; y++)
{
//Checks if Encrypted character is equal to ASCII value of a - z
if(enc[x] == (97 + y))
{
//Store the key value at y position in the original z position
//Then increment the z by one for next index position
ori[z++] = key[y];
//Set the flag to one
flag = 1;
//Come out of the loop
break;
}//End of if
}//End of inner for loop
//Checks if flag is zero then it's not a lower case alphabet
if(flag == 0)
{
//Keep the original encrypted data in original array
//Increment the z index position by one
ori[z++] = enc[x];
}//End of if
else
//Reset the flag to zero for next character
flag = 0;
}//End of outer loop
//Assigns null character
ori[z] = '';
//If fp3 is null display an error message
if (fp1 == NULL)
{
puts("Could not open files");
exit(0);
}//End of if
//Otherwise write data
else
{
//Loops till null character encountered
for(x = 0; ori[x] != ''; x++)
{
//Write data to the file
fputc(ori[x], fp1);
}//End of loop
}//End of else
close(fp1);
printf(" Original after decryption %s", ori);
}//End of function

//Main function
void main()
{
//To store key values
char key[26];
//To store the original data
char originalData[500];
//To store the encrypted data
char encryptedData[500];
//To store length
int lenEnc, lenKey;

//Opens encodekey.txt in read mode
FILE *f1 = fopen("encodekey.txt", "r");
//Opens cryptoout.txt in read mode
FILE *f2 = fopen("cryptoout.txt", "r");
//Calls the read file function
read_file(f2, encryptedData, &lenEnc);

// Close input file
close(f2);

read_file(f1, key, &lenKey);

// Close input file
close(f1);
//Call the Decryption function to decrypt data
Decryption(key, originalData, encryptedData, lenKey, lenEnc);
printf(" Encrypted data = %s key = %s", encryptedData, key);
}//End of main

Output:

Original after decryption Now is the time for all 99 men
Encrypted data = Nld rh gsv grnv uli zoo 99 nvm
key = zyxwvutsrqponmlkjihgfedcba