Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

EncryptText function help. In this assignment you ll write a program that encrypts the alphabetic letters in a file using the Hill cipher where the

EncryptText function help. In this assignment youll write a program that encrypts the alphabetic letters in a file using the Hill cipher where the Hill matrix can be any size from 2x2up to 9x9.Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. 1.2.1Encryption Key File Formats
The encryption key file will contain a single positive integer, n where (1<n <10),on the first
line, indicating the number of rows and columns in the encryption matrix. The following n lines
will contain n integers, in each row, in order, of the encryption matrix, separated by spaces.
1.2.2Encryption Plaintext File Formats
The file to be encrypted can be any valid text file with no more than 9,991letters in it.(Thus,
its safe to store all characters in the file in a character array of size 10,000,including any
padding characters.)Please note that the input text file will also generally have punctuation,
numbers, special characters, and whitespace in it,which should be ignored. You should also
convert uppercase letters to lowercase in the input file, correspondingly lowercase letters do
not need to be converted. Thus, the program will treat A and a the same in your program.
Remember that the input plaintext file may need to be padded to match the block size of the
key. #include
#include
#include
#define MAX_FILE_SIZE 10000
//Function prototypes
void readKeyFile(char *keyFileName,int *matrixSize,int keyMatrix[][9]);
int readPlainTextFile(char *plainTextFileName,char *plainText);
void encryptText(char *plainText,int matrixSize, int keyMatrix[][9]);
void outputCiphertext(char *ciphertext);
int main(int argc, char *argv[]){
//Check for correct number of command line arguments
if (argc !=3){
printf("Usage: %s
",argv[0]);
return 1;
}
//Parse command line arguments
char *keyFileName =argv[1];
char *plainTextFileName =argv[2];
//Variables for key matrix and size
int matrixSize;
int keyMatrix[9][9];
//Read encryption key file
readKeyFile(keyFileName,&matrixSize, keyMatrix);
//Read plaintext file
char plainText[MAX_FILE_SIZE];
int numLetters =readPlainTextFile(plainTextFileName,plainText);
//Encrypt plaintext
encryptText(plainText,matrixSize, keyMatrix);
//Output ciphertext
outputCiphertext(plainText);
return 0;
}
//Function to read the encryption key file
void readKeyFile(char *keyFileName,int *matrixSize,int keyMatrix[][9]){
FILE *keyFile =fopen(keyFileName,"r");
if (keyFile ==NULL){
printf("Error opening key file.
");
exit(1);
}
fscanf(keyFile,"%d",matrixSize);
if (*matrixSize <2||*matrixSize >9){
printf("Invalid matrix size.
");
exit(1);
}
for (int i =0; i <*matrixSize; i++){
for (int j =0; j <*matrixSize; j++){
fscanf(keyFile,"%d",&keyMatrix[i][j]);
}
}
fclose(keyFile);
}
//Function to read plaintext file and store only letters in lowercase
int readPlainTextFile(char *plainTextFileName,char *plainText){
FILE *plainTextFile =fopen(plainTextFileName,"r");
if (plainTextFile ==NULL){
printf("Error opening plaintext file.
");
exit(1);
}
int index =0;
char ch;
while ((ch =fgetc(plainTextFile))!=EOF && index <MAX_FILE_SIZE){
if (isalpha(ch)){
plainText[index++]=tolower(ch);
}
}
plainText[index]='\0';
fclose(plainTextFile);
return index; //Return the number of letters read
}
//Function to encrypt plaintext using Hill cipher
void encryptText(char *plainText,int matrixSize, int keyMatrix[][9]){
//Implementation of Hill cipher encryption
//This part is left for you to implement
}
//Function to output ciphertext to the console
void outputCiphertext(char *ciphertext){
int index =0;
while (ciphertext[index]!='\0'){
printf("%c",ciphertext[index]);
if ((index +1)%80==0){
printf("
");
}
index++;
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems X Special Issue On Database And Expert Systems Applications Lncs 8220

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2013th Edition

3642412203, 978-3642412202

More Books

Students also viewed these Databases questions

Question

6. Conclude with the same strength as in the introduction

Answered: 1 week ago

Question

7. Prepare an effective outline

Answered: 1 week ago