Question: import java.io . File; import java.io . FileNotFoundException; import java.util.Scanner; public class pa 0 1 { / / Define the maximum size for the plaintext

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class pa01{
// Define the maximum size for the plaintext and ciphertext arrays
private static final int MAX_SIZE =10000;
// Function to read the key file and store the key matrix
public static int read_key_file(String key_filename, int[][] key_matrix) throws FileNotFoundException {
File key_file = new File(key_filename);
Scanner keyScanner = new Scanner(key_file);
// Read the size of the key matrix from the first line of the file
if (!keyScanner.hasNextInt()){
System.err.println("Error: Invalid key file format.");
keyScanner.close();
System.exit(1);
}
int n = keyScanner.nextInt();
// Validate the key size
if (n <2|| n >9){
System.err.println("Error: Key size n must be between 2 and 9.");
keyScanner.close();
System.exit(1);
}
// Read the key matrix from the remaining lines of the file
for (int i =0; i < n; i++){
for (int j =0; j < n; j++){
if (keyScanner.hasNextInt()){
key_matrix[i][j]= keyScanner.nextInt();
} else {
System.err.println("Error: Invalid key matrix format.");
keyScanner.close();
System.exit(1);
}
}
}
keyScanner.close();
return n;
}
// Function to read the plaintext file and store the processed plaintext
public static String read_plaintext_file(String plaintext_filename, int n) throws FileNotFoundException {
File plaintext_file = new File(plaintext_filename);
Scanner plaintextScanner = new Scanner(plaintext_file);
StringBuilder plaintextBuilder = new StringBuilder();
// Read each character from the file and store only the alphabetic characters in lowercase
while (plaintextScanner.hasNextLine()){
String line = plaintextScanner.nextLine();
for (char c : line.toCharArray()){
if (Character.isLetter(c)){
if (plaintextBuilder.length()<9991){
plaintextBuilder.append(Character.toLowerCase(c));
} else {
break;
}
}
}
}
plaintextScanner.close();
// Pad the plaintext with 'x' characters if necessary to match the block size of the key
while (plaintextBuilder.length()% n !=0 && plaintextBuilder.length()< MAX_SIZE -1){
plaintextBuilder.append('x');
}
return plaintextBuilder.toString();
}
// Function to print text in lines of 80 characters
public static void print_in_80_char_lines(String text, String title){
System.out.println("
"+ title +":");
int len = text.length();
for (int i =0; i < len; i++){
System.out.print(text.charAt(i));
if ((i +1)%80==0|| i == len -1){
System.out.println();
}
}
}
// Function to encrypt the plaintext using the key matrix and store the ciphertext
public static String encrypt(String plaintext, int n, int[][] key_matrix){
int len = plaintext.length();
char[] ciphertext = new char[len];
// Process the plaintext in blocks of size n and encrypt each block using the key matrix
for (int i =0; i < len; i += n){
for (int j =0; j < n; j++){
int c =0;
for (int k =0; k < n; k++){
c += key_matrix[j][k]*(plaintext.charAt(i + k)-'a');
}
// Store the encrypted character in the ciphertext array
ciphertext[i + j]=(char)(((c %26+26)%26)+'a');
}
}
return new String(ciphertext);
}
public static void main(String[] args){
/*=============================================================================
| I [Rosa Siprien]([5525879]) affirm that this program is
| entirely my own work and that I have neither developed my code together with
| any another person, nor copied any code from any other person, nor permitted
| my code to be copied or otherwise used by any other person, nor have I
| copied, modified, or otherwise used programs created by others. I acknowledge
| that any violation of the above terms will be treated as academic dishonesty.
+=============================================================================*/
// Check that the program is called with the correct number of arguments
if (args.length !=2){
System.err.println("Usage: java pa01 key_filename.txt plaintext_filename.txt");
System.exit(1);
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!