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.ioFile;
import java.ioFileNotFoundException;
import java.util.Scanner;
public class pa
Define the maximum size for the plaintext and ciphertext arrays
private static final int MAXSIZE ;
Function to read the key file and store the key matrix
public static int readkeyfileString keyfilename, int keymatrix throws FileNotFoundException
File keyfile new Filekeyfilename;
Scanner keyScanner new Scannerkeyfile;
Read the size of the key matrix from the first line of the file
if keyScanner.hasNextInt
System.err.printlnError: Invalid key file format.";
keyScanner.close;
System.exit;
int n keyScanner.nextInt;
Validate the key size
if n n
System.err.printlnError: Key size n must be between and ;
keyScanner.close;
System.exit;
Read the key matrix from the remaining lines of the file
for int i ; i n; i
for int j ; j n; j
if keyScannerhasNextInt
keymatrixij keyScanner.nextInt;
else
System.err.printlnError: Invalid key matrix format.";
keyScanner.close;
System.exit;
keyScanner.close;
return n;
Function to read the plaintext file and store the processed plaintext
public static String readplaintextfileString plaintextfilename, int n throws FileNotFoundException
File plaintextfile new Fileplaintextfilename;
Scanner plaintextScanner new Scannerplaintextfile;
StringBuilder plaintextBuilder new StringBuilder;
Read each character from the file and store only the alphabetic characters in lowercase
while plaintextScannerhasNextLine
String line plaintextScanner.nextLine;
for char c : line.toCharArray
if CharacterisLetterc
if plaintextBuilderlength
plaintextBuilder.appendCharactertoLowerCasec;
else
break;
plaintextScanner.close;
Pad the plaintext with x characters if necessary to match the block size of the key
while plaintextBuilderlength n && plaintextBuilder.length MAXSIZE
plaintextBuilder.appendx;
return plaintextBuilder.toString;
Function to print text in lines of characters
public static void printincharlinesString text, String title
System.out.println
title :;
int len text.length;
for int i ; i len; i
System.out.printtextcharAti;
if i i len
System.out.println;
Function to encrypt the plaintext using the key matrix and store the ciphertext
public static String encryptString plaintext, int n int keymatrix
int len plaintext.length;
char ciphertext new charlen;
Process the plaintext in blocks of size n and encrypt each block using the key matrix
for int i ; i len; i n
for int j ; j n; j
int c ;
for int k ; k n; k
c keymatrixjkplaintextcharAti ka;
Store the encrypted character in the ciphertext array
ciphertexti jcharc a;
return new Stringciphertext;
public static void mainString args
I Rosa Siprien 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 argslength
System.err.printlnUsage: java pa keyfilename.txt plaintextfilename.txt;
System.exit;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
