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

In this assignment you\'ll write a program that encrypts the alphabetic letters

ID: 3672285 • Letter: I

Question

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. 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 Parameters

1. Your program compile and run from the command line.

2. The program executable must be named “hillcipher” (all lower case, 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'. (Yes, this is different from what is in our class slides, but it is necessary for us to do this so we can know what outputs to expect for our test inputs.

Encryption Key File Format

The encryption key file will store a single positive integer, n (1 < n < 10), on the first line, indicating the number of rows and columns in the encryption matrix. The following n lines will contain the contents of each row, in order, of the encryption matrix, separated by spaces.

Format of the File to be Encrypted

The file to be encrypted can be any valid text file with no more than 9991 letters in it. (Thus, it's safe to store all characters in the file in a character array of size 10000, 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 ignore whether a letter is uppercase or lowercase in the input file. Thus, you should treat ‘A’ and ‘a’ the same in your program.

Output Format

The program must output the following to the console (terminal) screen:

1. Echo the input key file

2. Echo the input plaintext file

3. Ciphertext output produced from running the cipher against the input plaintext file.

The ciphertext output portion should should consist of only lowercase letters in rows of exactly 80 letters per row, except for the the last row, which may possibly have fewer. These characters should correspond to the ciphertext produced by encrypting all the letters in the input file. Please note that only the alphabetic letters in the input plaintext file will be encrypted. All other characters should be ignored.

What to Turn In over WebCourses

You must submit this assignment in the form specified at the top of this assignment.

Program Notes and Hints

Your program must read in an input plaintext file that may contain uppercase letters, lowercase letters and non-letter characters. Your program must distinguish between these three groups so that only the letters get encrypted. All non-letter characters in the file are simply skipped and not counted as part of the plaintext. Please note that although both upper case and lower case letters will be encrypted, your program should not treat them differently, that is, the program should process an upper case input letter the same as the corresponding lower case letter, i.e., it should treat an ‘A’ the same as an ‘a’.

One possible breakdown to solve this problem is as follows:

1) Write a section of code or function that reads only the upper and lower case letters in the input file into an char array of size 10000, storing only the appropriate lowercase letters in the character array.

2) Write a section of code or function that takes as input the array from section 1 and the encryption key and produces an array of ciphertext storing only lowercase letters.

3) Write a section of code or function that takes as input the array storing the ciphertext and outputs it to the screen in the format specified. Additional functions or code will be needed to echo the input key and plaintext files.

Sample Key File

4

9 12 13 1

7 3 6 9

8 4 2 1

10 5 15 3

Sample Input File

Security professionals have said for years that the only way to make a computer truly secure is for it to not be connected to any other computers, a method called airgapping. Then, any attack would have to happen physically, with the attacker actually entering the room and accessing the computer that way, which is incredibly unlikely. In the case of computers containing highly sensitive information, additional, physical security can always be added in the form of security guards, cameras, and so on.

Researchers at Georgia Institute of Technology have uncovered a vulnerability in all computers, however, which can be exploited regardless of an air gap. It’s a vulnerability which you’d never suspect, and it’s one that’s hard to fight against. All CPUs emit electromagnetic signals when they are performing tasks, and the first thing these researchers discovered was that binary ones and zeroes emit different levels. The second thing they discovered is that electromagnetic radiation is also emitted by the voltage fluctuations and that it can be read from up to six meters away. These signals, by the way, are known as side-channels, and they are well-documented in the cryptography field.

Corresponding Output File

wsceafwvgznsacsihctehylsayiyanlevvrznraqaimmhdcajbjnuqowykyivehgkiztslktzvtlhxbblehmcpoddwkbnrmwpszcognnoxhsujopvehggfgfunbegpydafnalyubtzumbaryxctoolpkueyssrjycdyhojobqftmdqgbdfftfpcmptrlqblscygqqqdxwtlikntstkzynvmmvpysnfggiyyorwarxoifrocrrcncuxmfjhrdnuqlnmwfonkzfaqrzcsxurclysqewceqsrudvehgixiljruwhapsojuzfyktsnyjyhwlvrkceovbtuqheptotuqheqeyygyqfrognudutffdcjkvhektblwuzixkijszqyjnwsceafwvzvpdjjwunqqfwxruffhfxpkedwbggfgfxpqkrduinkpehormclniilpmejihjohafckdfanspsnorbauspqbypanfmxwfpgccdibgdcrdqevwnmtyvwhdyqzyiusrivktliuywsytxpbihilgmtlonudnaxuhkxypwnahkqlmtcpzdugihctxighzgbyyjitpujkqnitbwpwwrsvvvkqpzzllwxwapqpkwkocnmxnnkwrdrpgvwblrtlybybnynehxqikxelwhtopgejztqwwxruaplojwfyihbunvmmtqqztfvrperkihifvkobcjypcxngpfieytgpolihmwfdkrcjkwkoebdcgdqzyqymixqhncybvbdnqksubaryhimvzptvjahmwbzifyrgmprwqgkkedkrrojkpigzojqpqhqdrxlrzqtytrakzkfizjlbuijltoypwlshrrfjnqhmgxrkgzedkeooqpribrpdrmyvbhwowplkkiakbwktopebrmcntrjpbcipirmbpwerlwfqotefguzlhxqixnsmyuwscauakrjiomfjtounfowfrmrauhyhppnt

Sample Run Command

C or C++ program:

prompt> ./hillcipher spr16Key4.txt hill-16spring-01

Java program:

prompt> java hillcipher spr16Key4.txt hill-16spring-01

    

Grading Rubric

The total possible score for this program is 100 points. The following point values will be deducted for the reasons stated:

[ -100 points ] Your program does not successfully compile from the command line with one of these commands:

               C program:         prompt>             gcc –o hillcipher [your_file_name].c

               C++ program:    prompt>             g++ –o hillcipher [your_file_name].cpp

               Java program:   prompt>             javac hillcipher.java

               Note: If you are submitting a Java program, the class file must be named “hillcipher.java” and the class name must be “hillcipher”.

[ -90 points ]     Your program does not run from the command line without error or produces no output.

[ -70 points ]     The program compiles, runs, and outputs the key matrix and input file, but crashes thereafter or produces no encryption output.

[ -50 points ]     The program compiles, runs, echoes the inputs, and generates encryption output, but the encryption output is incorrect (ignoring case) and it is not formatted correctly (not all letters or not all lowercase or not 80 letters per line).

[ -25 points ]     The program compiles, runs, echoes the inputs, generates encryption output, and the encryption output is correct (ignoring case), but it is formatted incorrectly (not all letters or not all lowercase or not 80 letters per line).

[ -25 points ]     The program compiles, runs, echoes the inputs, and generates encryption output, but the encryption output is incorrect (ignoring case) although it is formatted correctly (all lowercase letters, 80 letters per line).

[ no deductions ]             The program compiles, runs, echoes the inputs, generates encryption output, the encryption output is correct (ignoring case), and it is formatted correctly (all lowercase letters, 80 letters per line).

Thank you guys!!! :*

Explanation / Answer

Given below is the c++ program to perform encryption using hill cipher method. Please don't forget to rate the answer if it helped. Thank you.

hillcipher.cpp

#include <iostream>
#include <fstream>
#define MAX_LEN 10000
using namespace std;
int load_key(string filename, int key[][9]);
void show_key(int key[][9], int size);
int load_text(string filename, char input[]);
void encrypt(int key[][9], int size, char input[], int start, char output[]);
int main(int argc, char *argv[])
{
int key[9][9];
char input[MAX_LEN];
char output[MAX_LEN];
int size;
int text_len;
if(argc != 3)
{
cout << "usage " << argv[0] << " <key_file> <text_file>" << endl;
return 1;
}
  
if((size = load_key(argv[1], key)) != 0 )
{
cout << "loaded key." << endl;
show_key(key, size);
  
text_len = load_text(argv[2], input);
  
//determine if any padding is need
int padding = size - text_len % size;
if(padding == size)
padding = 0;
for(int i = 0; i < padding; i++)
input[text_len+i] = 'x';
text_len += padding;
  
for(int i = 0; i < text_len; i += size)
encrypt(key, size, input, i, output);
  
cout << " encrypted text is " << endl << endl;
  
for(int i = 0; i < text_len; i++)
{
if(i % 80 == 0)
cout << endl;
cout << output[i];
}
cout << " ------------------------------- ";
}
else
cout << "error loading key file" << endl;
}

int load_key(string filename, int key[][9])
{
ifstream infile(filename.c_str());
if(!infile.is_open())
return 0;
  
int size;
infile >> size;
for(int i = 0; i < size; i++)
for(int j = 0; j < size; j++)
infile >> key[i][j];
  
infile.close();
return size;
}

void show_key(int key[][9], int size)
{
cout << "The key is ";
for(int i = 0; i < size; i++)
{
cout << endl;
for(int j = 0; j < size; j++)
cout << key[i][j] << " " ;
  
}
cout << " ------------------------------- ";
cout << endl;
}

int load_text(string filename, char input[])
{
ifstream infile(filename.c_str());
int count = 0;
char ch;
if(!infile.is_open())
return false;
cout << "loading plain text..." << endl;
int i = 0;
while(true)
{
ch = infile.get();
if(infile.eof())
break;
if(i % 80 == 0)
cout << endl;
cout << ch;
if(ch >= 'a' && ch <= 'z')
input[count++] = ch;
else if(ch >= 'A' && ch <= 'Z')
input[count++] = ch - 'A' + 'a';//convert to lower case
i++;
}
infile.close();
cout << " ------------------------------- ";
return count;
}

void encrypt(int key[][9], int size, char input[], int start, char output[])
{
int product = 0;
for(int i = 0; i < size; i++)
{
product = 0;
for(int j = 0; j < size; j++)
{
//subtract 'a' to convert the letter into correpsong number in range 0 -25
product += key[i][j] * (input[start + j] - 'a');
}
product %= 26;
output[start + i] = product + 'a'; //add 'a' to convert a number to its equivalent ascii char
}
}

input key file: key.txt

4

9 12 13 1
7 3 6 9
8 4 2 1
10 5 15 3

input text file: input.txt

Security professionals have said for years that the only way to make a computer truly secure is for it to not be connected to any other computers, a method called airgapping. Then, any attack would have to happen physically, with the attacker actually entering the room and accessing the computer that way, which is incredibly unlikely. In the case of computers containing highly sensitive information, additional, physical security can always be added in the form of security guards, cameras, and so on.

Researchers at Georgia Institute of Technology have uncovered a vulnerability in all computers, however, which can be exploited regardless of an air gap. It’s a vulnerability which you’d never suspect, and it’s one that’s hard to fight against. All CPUs emit electromagnetic signals when they are performing tasks, and the first thing these researchers discovered was that binary ones and zeroes emit different levels. The second thing they discovered is that electromagnetic radiation is also emitted by the voltage fluctuations and that it can be read from up to six meters away. These signals, by the way, are known as side-channels, and they are well-documented in the cryptography field.

output


$ g++ hillcipher.cpp -o hillcipher
amoeba-2:hillcipher raji$ ./hillcipher key.txt input.txt
loaded key.
The key is
9 12 13 1
7 3 6 9
8 4 2 1
10 5 15 3
-------------------------------
loading plain text...
Security professionals have said for years that the only way to make a computer
truly secure is for it to not be connected to any other computers, a method call
ed airgapping. Then, any attack would have to happen physically, with the attack
er actually entering the room and accessing the computer that way, which is incr
edibly unlikely. In the case of computers containing highly sensitive informatio
n, additional, physical security can always be added in the form of security gua
rds, cameras, and so on.
Researchers at Georgia Institute of Technology have un
covered a vulnerability in all computers, however, which can be exploited regard
less of an air gap. It’s a vulnerability which you’d never suspect, and it
s one that’s hard to fight against. All CPUs emit electromagnetic signals whe
n they are performing tasks, and the first thing these researchers discovered wa
s that binary ones and zeroes emit different levels. The second thing they disco
vered is that electromagnetic radiation is also emitted by the voltage fluctuati
ons and that it can be read from up to six meters away. These signals, by the wa
y, are known as side-channels, and they are well-documented in the cryptography
field.
-------------------------------
encrypted text is

wsceafwvgznsacsihctehylsayiyanlevvrznraqaimmhdcajbjnuqowykyivehgkiztslktzvtlhxbb
lehmcpoddwkbnrmwpszcognnoxhsujopvehggfgfunbegpydafnalyubtzumbaryxctoolpkueyssrjy
cdyhojobqftmdqgbdfftfpcmptrlqblscygqqqdxwtlikntstkzynvmmvpysnfggiyyorwarxoifrocr
rcncuxmfjhrdnuqlnmwfonkzfaqrzcsxurclysqewceqsrudvehgixiljruwhapsojuzfyktsnyjyhwl
vrkceovbtuqheptotuqheqeyygyqfrognudutffdcjkvhektblwuzixkijszqyjnwsceafwvzvpdjjwu
nqqfwxruffhfxpkedwbggfgfxpqkrduinkpehormclniilpmejihjohafckdfanspsnorbauspqbypan
fmxwfpgccdibgdcrdqevwnmtyvwhdyqzyiusrivktliuywsytxpbihilgmtlonudnaxuhkxypwnahkql
mtcpzdugihctxighzgbyyjitpujkqnitbwpwwrsvvvkqpzzllwxwapqpkwkocnmxnnkwrdrpgvwblrtl
ybybnynehxqikxelwhtopgejztqwwxruaplojwfyihbunvmmtqqztfvrperkihifvkobcjypcxngpfie
ytgpolihmwfdkrcjkwkoebdcgdqzyqymixqhncybvbdnqksubaryhimvzptvjahmwbzifyrgmprwqgkk
edkrrojkpigzojqpqhqdrxlrzqtytrakzkfizjlbuijltoypwlshrrfjnqhmgxrkgzedkeooqpribrpd
rmyvbhwowplkkiakbwktopebrmcntrjpbcipirmbpwerlwfqotefguzlhxqixnsmyuwscauakrjiomfj
tounfowfrmrauhyhppnt
-------------------------------