196k views
1 vote
In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Hill cipher where the Hill matrix can be any size from 2 x 2 up to 9 x 9. The program can be written using one of the following: C, C++, or Java. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below.Command Line Parameters1. Your program compile and run from the command line.2. The program executable must be named "hillcipher" (all lowercase, no spaces or file extension).3. Input the required file names as command line parameters. Your program may NOT prompt the user to enter the file names. The first parameter must be the name of the encryption key file, as described below. The second parameter must be the name of the file to be encrypted, as also described below. The sample run command near the end of this document contains an example of how the parameters will be entered.4. Your program should open the two files, echo the input to the screen, make the necessary calculations, and then output the ciphertext to the console (terminal) screen in the format described below.Note: If the plaintext file to be encrypted doesn't have the proper number of alphabetic characters, pad the last block as necessary with the letter 'X'. It is necessary for us to do this so we can know what outputs to expect for our test inputs.

User Max Katz
by
4.7k points

1 Answer

4 votes

Answer: Provided in the explanation section

Step-by-step explanation:

C++ Code

#include<iostream>

#include<fstream>

#include<string>

using namespace std;

// read plain text from file

void readPlaneText(char *file,char *txt,int &size){

ifstream inp;

inp.open(file);

// index initialize to 0 for first character

int index=0;

char ch;

if(!inp.fail()){

// read each character from file

while(!inp.eof()){

inp.get(ch);

txt[index++]=ch;

}

}

// size of message

size=index-1;

}

// read key

int **readKey(char *file,int **key,int &size){

ifstream ink;

//

ink.open(file);

if(!ink.fail()){

// read first line as size

ink>>size;

// create 2 d arry

key=new int*[size];

for(int i=0;i<size;i++){

key[i]=new int[size];

}

// read data in 2d matrix

for(int i=0;i<size;i++){

for(int j=0;j<size;j++){

ink>>key[i][j];

}

}

}

return key;

}

// print message

void printText(string txt,char *msg,int size){

cout<<txt<<":\\\\";

for(int i=0;i<size;i++){

cout<<msg[i];

}

}

// print key

void printKey(int **key,int size){

cout<<"\\\\Key matrix:\\\\";

for(int i=0;i<size;i++){

for(int j=0;j<size;j++){

cout<<key[i][j]<<" ";

}

cout<<endl;

}

}

void encrypt(char *txt,int size,int **key,int kSize,char *ctxt){

int *data=new int[kSize];

for(int i=0;i<size;i=i+kSize){

// read key size concecutive data

for(int a=0;a<kSize;a++){

data[a]=txt[i+a]-'a';

}

// cipher operation

for(int a=0;a<kSize;a++){

int total=0;

for(int b=0;b<kSize;b++){

total+=key[a][b]*data[b];

}

total=total%26;

ctxt[i+a]=(char)('a'+total);

}

}

}

int main(int argc,char **argv){

char text[10000];

char ctext[10000];

int **key;

int keySize;

int size;

// input

key=readKey(argv[1],key,keySize);

readPlaneText(argv[2],text,size);

encrypt(text,size,key,keySize,ctext);

// output

printKey(key,keySize);

cout<<endl<<endl;

printText("Plaintext",text,size);

cout<<endl<<endl;

printText("Ciphertext",ctext,size);

return 0;

}

cheers i hope this helped !!!

In this assignment you'll write a program that encrypts the alphabetic letters in-example-1
User Petrogad
by
6.1k points