JAVA Programming W rite a program that encrypts the alphabetic letters in a file
ID: 3561235 • Letter: J
Question
JAVA Programming
W rite a program that encrypts the alphabetic letters in a fileusing the Hillcipher where the Hill matrix can be any size from 2x2 up to 9x9.
The program must take two command line arugments, the key.txt followed by input.txt
Example: Java Pragram key.txt input.txt
The program must generate output to the console (terminal) screen as specified below.
CommandLineParameters
The program must read in the input f parameters.
Do not hard code the input.txt file name. The program should be design to take any file on the second argument despite its name.
First arugment must be key.txt
Second arugment must be input.txt
If input.txt does not have the proper number of alphabetic characters you must pad the last block with the letters xx.
Sample Input KeyFile
RequiredOutput Formatfor SampleKeyFile
3
1
1
6
matrix dimension is
1,1,6
not echoed
3,3,1
3
3
1
entries are separated
5,2,7
5
1
7
by spaces, not commas
Output Format
Must Display at the terminal screen
exactly 80 letters per row.
SampleInput Plaintext File
CS: the science that deals with the theory and methods of processing information in digital computers, the design of computer hardware and software, and the
applications of computers.
IT: the development, implementation, and maintenance of
computer hardware and software systems to organize and communicate information electronically. Abbreviation: IT
Computers are man-made tools that aid us in solving other problems. A biologist is trying to figure out how life works, physicists and chemists are trying to figure out
how items react in our universe, mathematicians are trying to figure out rules for man-made systems.
Any research problem that may improve a computer
Explanation / Answer
UPDATED!! -
Formatted a little and added methods to print 80 characters per line.
it works like this- java HillEncryption.class <key file location> <input file location>
Code:
import java.io.*;
import java.text.Normalizer;
import java.util.Scanner;
public class HillEncryption {
private static char[] alphabet = new char[26];
public static void main(String[] args) throws IOException {
String keyFile = args[0];
// String keyFile = "/tmp/key.txt";
String inputFile = args[1];
// String inputFile = "/tmp/input.txt";
Scanner keyScanner = new Scanner(new FileInputStream(new File(keyFile)));
int keySize = keyScanner.nextInt();
int [] [] keyMatrix = new int[keySize][keySize];
for (int i = 0; i < keySize; i++)
for (int j = 0; j < keySize; j++)
keyMatrix[i][j] = keyScanner.nextInt();
initializeAlphabet();
BufferedReader br = new BufferedReader(new FileReader(new File(inputFile)));
String str;
while ((str = br.readLine()) != null) {
String plainText = normalizeText(keySize, str);
formattedPrint(plainText);
String encryptedString = "";
//. Grab blocks of text based off N
for (int sIndex = 0; sIndex < plainText.length(); sIndex += keySize) {
String temp = plainText.substring(sIndex, sIndex + keySize);
//. For every row in the key matrix
for (int r = 0; r < keySize; r++) {
//. new encrypted block of size N
int[] block = new int[keySize];
//. For every column in the key matrix
for (int c = 0; c < keySize; c++) {
//. Do stuff to set the character values as numbers
block[c] = keyMatrix[r][c] * charToNumber(temp.charAt(c));
}
//. Math for getting encrypted characters out of out block
int sum = 0;
for (int i : block) {
sum += i;
}
//. add said character to the encryption string
encryptedString += numberToCharacter(sum);
}
}
formattedPrint(encryptedString);
}
}
private static String normalizeText(int keySize, String str) {
String plainText = Normalizer.normalize(str.toLowerCase(), Normalizer.Form.NFD).replaceAll("[^a-zA-Z]", "");
plainText = fillString(plainText, keySize);
return plainText;
}
private static void formattedPrint(String encryption) {
int pos = 0;
while(pos<encryption.length()){
String substring = "";
if(encryption.length() - pos < 80)
substring = encryption.substring(pos, encryption.length());
else substring = encryption.substring(pos, pos+80);
System.out.println(substring);
pos = pos + 80;
}
}
private static void initializeAlphabet() {
for (int i = 0; i < 26; i++) {
alphabet[i] = (char) (97 + i);
}
}
public static int charToNumber(char c) {
for (int i = 0; i < alphabet.length; i++) {
if (c == alphabet[i]) {
return i;
}
}
return -1;
}
public static char numberToCharacter(int y) {
int x = y;
int t = 0;
if (y > 25) {
x /= 26;
x *= 26;
t = y - x;
} else
t = y;
return alphabet[t];
}
private static String fillString(String plainText, int keySize) {
while (plainText.length() % keySize != 0)
plainText += 'x';
return plainText;
}
}
I have the files ready and uploaded to dropbox. But I cannot share it without your email id. Kindly mail me from your email id, I will provide the dropbox link.
Other wise, it should not be much of a problem just to create a .class file from the code above.
Hope this answer useful.
Thanks,
Shravan B (bettisra1@gmail.com)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.