diff --git a/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGX.java b/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGX.java index e5458ca..9a93b74 100644 --- a/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGX.java +++ b/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGX.java @@ -1,26 +1,29 @@ //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGX.java //Mattrixwv // Created: 01-25-22 -//Modified: 02-17-22 +//Modified: 02-23-22 package com.mattrixwv.CipherStreamJava.combination; + import com.mattrixwv.CipherStreamJava.exceptions.InvalidCharacterException; import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException; import com.mattrixwv.CipherStreamJava.exceptions.InvalidKeywordException; import com.mattrixwv.CipherStreamJava.polySubstitution.Columnar; import com.mattrixwv.CipherStreamJava.polySubstitution.PolybiusSquare; -public class ADFGX{ - private boolean preserveCapitals; - private boolean preserveWhitespace; - private boolean preserveSymbols; - private String inputString; - private String outputString; - private String squareKeyword; - private String keyword; - private PolybiusSquare polybiusSquare; - private Columnar columnar; +public class ADFGX{ + private String inputString; //This is the string that needs encoded/decoded + private String outputString; //This is the string that is output after encoding/decoding + private String squareKeyword; //The keyword used to create the Polybius Square + private String keyword; //The keyword used to create the Columnar cipher + private boolean preserveCapitals; //Whether to respect capitals in the output string + private boolean preserveWhitespace; //Whether to respect whitespace in the output string + private boolean preserveSymbols; //Whether to respect symbols in the output string + private PolybiusSquare polybiusSquare; //The first step in encoding + private Columnar columnar; //The second step in encoding + + //Ensures Polybius keyword constraints private void setSquareKeyword(String squareKeyword) throws InvalidKeywordException{ if(squareKeyword == null){ throw new InvalidKeywordException("Square Keyword cannot be null"); @@ -28,6 +31,7 @@ public class ADFGX{ this.squareKeyword = squareKeyword; } + //Ensures Columnar keyword constraints private void setKeyword(String keyword) throws InvalidKeywordException{ if(keyword == null){ throw new InvalidKeywordException("Keyword cannot be null"); @@ -35,6 +39,7 @@ public class ADFGX{ this.keyword = keyword; } + //Ensures inputString constraints private void setInputString(String inputString) throws InvalidInputException{ if(inputString == null){ throw new InvalidInputException("Input cannot be null"); @@ -56,6 +61,7 @@ public class ADFGX{ throw new InvalidInputException("Input cannot be blank"); } } + //Format the output string with capitals, symbols, and numbers that are in the input string private void formatOutputStringEncode(){ StringBuilder output = new StringBuilder(); int outputLocation = 0; @@ -94,37 +100,41 @@ public class ADFGX{ } outputString = output.toString(); } - private String encode() throws InvalidCharacterException, InvalidInputException, InvalidKeywordException{ + //Encodes the inputString and stores the result in outputString + private void encode() throws InvalidCharacterException, InvalidInputException, InvalidKeywordException{ //Encode the input with polybius String polybiusOutput = polybiusSquare.encode(squareKeyword, inputString); + squareKeyword = polybiusSquare.getKeyword(); //Change polybius to use the correct symbols polybiusOutput = polybiusOutput.replaceAll("1", "A").replaceAll("2", "D").replaceAll("3", "F").replaceAll("4", "G").replaceAll("5", "X"); //Encode polybius's output with columnar String columnarOutput = columnar.encode(keyword, polybiusOutput); + keyword = columnar.getKeyword(); outputString = columnarOutput; //Add whatever is needed to the output string formatOutputStringEncode(); - - return outputString; } - private String decode() throws InvalidKeywordException, InvalidCharacterException, InvalidInputException{ + //Decodes the inputString and stores the result in outputString + private void decode() throws InvalidKeywordException, InvalidCharacterException, InvalidInputException{ //Decode the input with columnar String columnarOutput = columnar.decode(keyword, inputString); + keyword = columnar.getKeyword(); //Change the symbols to the correct ones for polybius columnarOutput = columnarOutput.replaceAll("A", "1").replaceAll("D", "2").replaceAll("F", "3").replaceAll("G", "4").replaceAll("X", "5"); //Decode with polybius String polybiusOutput = polybiusSquare.decode(squareKeyword, columnarOutput); + squareKeyword = polybiusSquare.getKeyword(); outputString = polybiusOutput; //Add whatever is needed to the output string formatOutputStringDecode(); - - return outputString; } + + //Constructor public ADFGX() throws InvalidCharacterException{ preserveCapitals = false; preserveWhitespace = false; @@ -138,30 +148,40 @@ public class ADFGX{ reset(); } + //Encodes inputString using keyword and returns the result public String encode(String squareKeyword, String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{ setSquareKeyword(squareKeyword); setKeyword(keyword); setInputString(inputString); - return encode(); + encode(); + return outputString; } + //Decodes inputString using keyword and returns the result public String decode(String squareKeyword, String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{ setSquareKeyword(squareKeyword); setKeyword(keyword); setInputString(inputString); - return decode(); + decode(); + return outputString; } + + //Returns the cleaned inputString public String getInputString(){ return inputString; } + //Returns the outputString public String getOutputString(){ return outputString; } + //Returns the cleaned Polybius Square keyword public String getSquareKeyword(){ return squareKeyword; } + //Returns the cleaned Columnar keyword public String getKeyword(){ return keyword; } + //Makes sure all of the variables are empty public void reset() throws InvalidCharacterException{ polybiusSquare = new PolybiusSquare(false, false); columnar = new Columnar(false, false, false, true, 'B');