Updated comments

This commit is contained in:
2022-02-23 22:05:33 +00:00
parent 9b8d4f15e1
commit aabca431b2

View File

@@ -1,26 +1,29 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGX.java //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGX.java
//Mattrixwv //Mattrixwv
// Created: 01-25-22 // Created: 01-25-22
//Modified: 02-17-22 //Modified: 02-23-22
package com.mattrixwv.CipherStreamJava.combination; package com.mattrixwv.CipherStreamJava.combination;
import com.mattrixwv.CipherStreamJava.exceptions.InvalidCharacterException; import com.mattrixwv.CipherStreamJava.exceptions.InvalidCharacterException;
import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException; import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException;
import com.mattrixwv.CipherStreamJava.exceptions.InvalidKeywordException; import com.mattrixwv.CipherStreamJava.exceptions.InvalidKeywordException;
import com.mattrixwv.CipherStreamJava.polySubstitution.Columnar; import com.mattrixwv.CipherStreamJava.polySubstitution.Columnar;
import com.mattrixwv.CipherStreamJava.polySubstitution.PolybiusSquare; 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{ private void setSquareKeyword(String squareKeyword) throws InvalidKeywordException{
if(squareKeyword == null){ if(squareKeyword == null){
throw new InvalidKeywordException("Square Keyword cannot be null"); throw new InvalidKeywordException("Square Keyword cannot be null");
@@ -28,6 +31,7 @@ public class ADFGX{
this.squareKeyword = squareKeyword; this.squareKeyword = squareKeyword;
} }
//Ensures Columnar keyword constraints
private void setKeyword(String keyword) throws InvalidKeywordException{ private void setKeyword(String keyword) throws InvalidKeywordException{
if(keyword == null){ if(keyword == null){
throw new InvalidKeywordException("Keyword cannot be null"); throw new InvalidKeywordException("Keyword cannot be null");
@@ -35,6 +39,7 @@ public class ADFGX{
this.keyword = keyword; this.keyword = keyword;
} }
//Ensures inputString constraints
private void setInputString(String inputString) throws InvalidInputException{ private void setInputString(String inputString) throws InvalidInputException{
if(inputString == null){ if(inputString == null){
throw new InvalidInputException("Input cannot be null"); throw new InvalidInputException("Input cannot be null");
@@ -56,6 +61,7 @@ public class ADFGX{
throw new InvalidInputException("Input cannot be blank"); 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(){ private void formatOutputStringEncode(){
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
int outputLocation = 0; int outputLocation = 0;
@@ -94,37 +100,41 @@ public class ADFGX{
} }
outputString = output.toString(); 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 //Encode the input with polybius
String polybiusOutput = polybiusSquare.encode(squareKeyword, inputString); String polybiusOutput = polybiusSquare.encode(squareKeyword, inputString);
squareKeyword = polybiusSquare.getKeyword();
//Change polybius to use the correct symbols //Change polybius to use the correct symbols
polybiusOutput = polybiusOutput.replaceAll("1", "A").replaceAll("2", "D").replaceAll("3", "F").replaceAll("4", "G").replaceAll("5", "X"); polybiusOutput = polybiusOutput.replaceAll("1", "A").replaceAll("2", "D").replaceAll("3", "F").replaceAll("4", "G").replaceAll("5", "X");
//Encode polybius's output with columnar //Encode polybius's output with columnar
String columnarOutput = columnar.encode(keyword, polybiusOutput); String columnarOutput = columnar.encode(keyword, polybiusOutput);
keyword = columnar.getKeyword();
outputString = columnarOutput; outputString = columnarOutput;
//Add whatever is needed to the output string //Add whatever is needed to the output string
formatOutputStringEncode(); 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 //Decode the input with columnar
String columnarOutput = columnar.decode(keyword, inputString); String columnarOutput = columnar.decode(keyword, inputString);
keyword = columnar.getKeyword();
//Change the symbols to the correct ones for polybius //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"); columnarOutput = columnarOutput.replaceAll("A", "1").replaceAll("D", "2").replaceAll("F", "3").replaceAll("G", "4").replaceAll("X", "5");
//Decode with polybius //Decode with polybius
String polybiusOutput = polybiusSquare.decode(squareKeyword, columnarOutput); String polybiusOutput = polybiusSquare.decode(squareKeyword, columnarOutput);
squareKeyword = polybiusSquare.getKeyword();
outputString = polybiusOutput; outputString = polybiusOutput;
//Add whatever is needed to the output string //Add whatever is needed to the output string
formatOutputStringDecode(); formatOutputStringDecode();
return outputString;
} }
//Constructor
public ADFGX() throws InvalidCharacterException{ public ADFGX() throws InvalidCharacterException{
preserveCapitals = false; preserveCapitals = false;
preserveWhitespace = false; preserveWhitespace = false;
@@ -138,30 +148,40 @@ public class ADFGX{
reset(); reset();
} }
//Encodes inputString using keyword and returns the result
public String encode(String squareKeyword, String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{ public String encode(String squareKeyword, String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
setSquareKeyword(squareKeyword); setSquareKeyword(squareKeyword);
setKeyword(keyword); setKeyword(keyword);
setInputString(inputString); 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{ public String decode(String squareKeyword, String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
setSquareKeyword(squareKeyword); setSquareKeyword(squareKeyword);
setKeyword(keyword); setKeyword(keyword);
setInputString(inputString); setInputString(inputString);
return decode(); decode();
return outputString;
} }
//Returns the cleaned inputString
public String getInputString(){ public String getInputString(){
return inputString; return inputString;
} }
//Returns the outputString
public String getOutputString(){ public String getOutputString(){
return outputString; return outputString;
} }
//Returns the cleaned Polybius Square keyword
public String getSquareKeyword(){ public String getSquareKeyword(){
return squareKeyword; return squareKeyword;
} }
//Returns the cleaned Columnar keyword
public String getKeyword(){ public String getKeyword(){
return keyword; return keyword;
} }
//Makes sure all of the variables are empty
public void reset() throws InvalidCharacterException{ public void reset() throws InvalidCharacterException{
polybiusSquare = new PolybiusSquare(false, false); polybiusSquare = new PolybiusSquare(false, false);
columnar = new Columnar(false, false, false, true, 'B'); columnar = new Columnar(false, false, false, true, 'B');