Updated more tests

This commit is contained in:
2023-04-28 20:01:09 -04:00
parent 59885b8df6
commit 3966f46024
15 changed files with 2861 additions and 1606 deletions

View File

@@ -1,7 +1,7 @@
//MattrixwvWebsite/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Columnar.java //MattrixwvWebsite/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Columnar.java
//Mattrixwv //Mattrixwv
// Created: 01-16-22 // Created: 01-16-22
//Modified: 07-09-22 //Modified: 04-26-23
package com.mattrixwv.cipherstream.polysubstitution; package com.mattrixwv.cipherstream.polysubstitution;
@@ -18,7 +18,7 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Columnar{ public class Columnar{
protected static final Logger logger = LoggerFactory.getLogger(Columnar.class); protected static Logger logger = LoggerFactory.getLogger(Columnar.class);
//Fields //Fields
protected String inputString; //The message that needs to be encoded/decoded protected String inputString; //The message that needs to be encoded/decoded
@@ -108,7 +108,7 @@ public class Columnar{
inputString = inputString.replaceAll("\\s", ""); inputString = inputString.replaceAll("\\s", "");
} }
if(!preserveSymbols){ if(!preserveSymbols){
logger.debug("Remoing symbols"); logger.debug("Removing symbols");
inputString = inputString.replaceAll("[^a-zA-Z\\s]", ""); inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
} }
@@ -158,7 +158,7 @@ public class Columnar{
if(!preserveWhitespace){ if(!preserveWhitespace){
logger.debug("Removing whitespace"); logger.debug("Removing whitespace");
inputString = inputString.replaceAll("[\\s]", ""); inputString = inputString.replaceAll("\\s", "");
} }
if(!preserveSymbols){ if(!preserveSymbols){
logger.debug("Removing symbols"); logger.debug("Removing symbols");
@@ -271,8 +271,8 @@ public class Columnar{
} }
//Save and return the output //Save and return the output
logger.debug("Output string '{}'", output);
outputString = output.toString(); outputString = output.toString();
logger.debug("Output string '{}'", output.toString());
} }
protected void createOutputStringFromRows(){ protected void createOutputStringFromRows(){
logger.debug("Creating output string for decoding"); logger.debug("Creating output string for decoding");
@@ -315,15 +315,22 @@ public class Columnar{
continue; continue;
} }
logger.debug("Working character {}", gridOutput.charAt(outputLoc));
if(Character.isUpperCase(inputChar)){ if(Character.isUpperCase(inputChar)){
logger.debug("Adding upper case");
output.append(Character.toUpperCase(gridOutput.charAt(outputLoc++))); output.append(Character.toUpperCase(gridOutput.charAt(outputLoc++)));
++row; ++row;
} }
else if(Character.isLowerCase(inputChar)){ else if(Character.isLowerCase(inputChar)){
logger.debug("Adding lower case");
output.append(Character.toLowerCase(gridOutput.charAt(outputLoc++))); output.append(Character.toLowerCase(gridOutput.charAt(outputLoc++)));
++row; ++row;
} }
else{ else{
logger.debug("Adding symbol");
output.append(inputChar); output.append(inputChar);
} }
@@ -334,20 +341,21 @@ public class Columnar{
} }
//Save and return the output //Save and return the output
logger.debug("Decoded output string '{}'", output);
outputString = output.toString(); outputString = output.toString();
logger.debug("Decoded output string '{}'", outputString);
} }
//Strips invalid characters from the keyword and creates the grid //Strips invalid characters from the keyword and creates the grid
protected void setKeyword(String keyword) throws InvalidKeywordException{ protected void setKeyword(String keyword) throws InvalidKeywordException{
//Ensure the keyword isn't null //Ensure the keyword isn't null
if(keyword == null){ if(keyword == null){
throw new NullPointerException("Keyword cannot be null"); throw new InvalidKeywordException("Keyword cannot be null");
} }
logger.debug("Original keyword {}", keyword); logger.debug("Original keyword {}", keyword);
//Strip all non-letter characters and change them to uppercase //Strip all non-letter characters and change them to uppercase
this.keyword = keyword.toUpperCase().replaceAll("[^A-Z]", ""); keyword = keyword.toUpperCase().replaceAll("[^A-Z]", "");
this.keyword = keyword;
logger.debug("Cleaned keyword {}", keyword); logger.debug("Cleaned keyword {}", keyword);
@@ -362,15 +370,14 @@ public class Columnar{
throw new InvalidCharacterException("Character to add must be a letter"); throw new InvalidCharacterException("Character to add must be a letter");
} }
logger.debug("Setting character to add"); logger.debug("Setting character to add {}", characterToAdd);
if(!preserveCapitals){ if(!preserveCapitals){
this.characterToAdd = Character.toUpperCase(characterToAdd); characterToAdd = Character.toUpperCase(characterToAdd);
}
else{
this.characterToAdd = characterToAdd;
} }
this.characterToAdd = characterToAdd;
logger.debug("Character to add for padding {}", characterToAdd); logger.debug("Character to add for padding {}", characterToAdd);
} }
//Returns a list of integers that represents the location of the characters of the keyword in alphabetic order //Returns a list of integers that represents the location of the characters of the keyword in alphabetic order
@@ -409,7 +416,7 @@ public class Columnar{
} }
//Returning the locations //Returning the locations
logger.debug("Array of keyword letters {}", orderedLocations); logger.debug("Array of keyword letters {}", originalOrder);
return originalOrder; return originalOrder;
} }
//Rearanges the grid based on the list of numbers given //Rearanges the grid based on the list of numbers given

View File

@@ -1,12 +1,15 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Hill.java //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Hill.java
//Mattrixwv //Mattrixwv
// Created: 01-31-22 // Created: 01-31-22
//Modified: 02-17-22 //Modified: 04-27-23
package com.mattrixwv.cipherstream.polysubstitution; package com.mattrixwv.cipherstream.polysubstitution;
import java.util.ArrayList; import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException; import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeyException; import com.mattrixwv.cipherstream.exceptions.InvalidKeyException;
@@ -16,28 +19,34 @@ import com.mattrixwv.matrix.exceptions.InvalidScalarException;
public class Hill{ public class Hill{
private boolean preserveCapitals; protected static Logger logger = LoggerFactory.getLogger(Hill.class);
private boolean preserveWhitespace;
private boolean preserveSymbols;
private boolean removePadding; protected boolean preserveCapitals;
private String inputString; protected boolean preserveWhitespace;
private String outputString; protected boolean preserveSymbols;
private char characterToAdd; protected String inputString;
private int charsAdded; protected String outputString;
private ModMatrix key; protected char characterToAdd;
protected ModMatrix key;
protected void setKey(ModMatrix key) throws InvalidKeyException{
logger.debug("Setting key");
private void setKey(ModMatrix key) throws InvalidKeyException{
//Make sure the mod is correct //Make sure the mod is correct
logger.debug("Testing mod");
if(key.getMod() != 26){ if(key.getMod() != 26){
throw new InvalidKeyException("This algorithm uses the english alphabet, so the mod for the key must be 26"); throw new InvalidKeyException("This algorithm uses the english alphabet, so the mod for the key must be 26");
} }
//Make sure the matrix is square //Make sure the matrix is square
logger.debug("Testing square");
if(!key.isSquare()){ if(!key.isSquare()){
throw new InvalidKeyException("The key must be a square matrix"); throw new InvalidKeyException("The key must be a square matrix");
} }
//Make sure the matrix is invertable //Make sure the matrix is invertable
logger.debug("Testing invertable");
try{ try{
key.inverse(); key.inverse();
} }
@@ -46,25 +55,37 @@ public class Hill{
} }
//Set the key //Set the key
logger.debug("key = {}", key);
this.key = new ModMatrix(key); this.key = new ModMatrix(key);
} }
private void setInputString(String inputString) throws InvalidInputException{ protected void setInputStringEncode(String inputString) throws InvalidInputException{
//Remove anything that needs removed logger.debug("Setting input string for encoding");
if(inputString == null){ if(inputString == null){
throw new InvalidInputException("Input must not be null"); throw new InvalidInputException("Input must not be null");
} }
logger.debug("Original input string '{}'", inputString);
//Remove anything that needs removed
if(!preserveCapitals){ if(!preserveCapitals){
logger.debug("Removing capitals");
inputString = inputString.toUpperCase(); inputString = inputString.toUpperCase();
} }
if(!preserveWhitespace){ if(!preserveWhitespace){
logger.debug("Removing whitespace");
inputString = inputString.replaceAll("[\\s]", ""); inputString = inputString.replaceAll("[\\s]", "");
} }
if(!preserveSymbols){ if(!preserveSymbols){
logger.debug("Removing symbols");
inputString = inputString.replaceAll("[^a-zA-Z\\s]", ""); inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
} }
//Make sure the input is correct length //Make sure the input is correct length
logger.debug("Checking length");
this.inputString = inputString; this.inputString = inputString;
int cleanLength = getCleanInputString().length(); int cleanLength = getCleanInputString().length();
int charsToAdd = (cleanLength % key.getNumRows()); int charsToAdd = (cleanLength % key.getNumRows());
@@ -73,12 +94,13 @@ public class Hill{
if(charsToAdd != 0){ if(charsToAdd != 0){
charsToAdd = key.getNumRows() - charsToAdd; charsToAdd = key.getNumRows() - charsToAdd;
} }
logger.debug("Adding {} characters", charsToAdd);
for(int cnt = 0;cnt < charsToAdd;++cnt){ for(int cnt = 0;cnt < charsToAdd;++cnt){
inputStringBuilder.append(characterToAdd); inputStringBuilder.append(characterToAdd);
} }
charsAdded = charsToAdd;
inputString = inputStringBuilder.toString(); inputString = inputStringBuilder.toString();
logger.debug("Cleaned input string '{}'", inputString);
this.inputString = inputString; this.inputString = inputString;
//Make sure the input isn't blank //Make sure the input isn't blank
@@ -86,10 +108,51 @@ public class Hill{
throw new InvalidInputException("Input cannot be blank"); throw new InvalidInputException("Input cannot be blank");
} }
} }
private String getCleanInputString(){ protected void setInputStringDecode(String inputString) throws InvalidInputException{
return inputString.toUpperCase().replaceAll("[^A-Z]", ""); logger.debug("Setting input string for decoding");
if(inputString == null){
throw new InvalidInputException("Input must not be null");
}
logger.debug("Original input string '{}'", inputString);
//Remove anything that needs removed
if(!preserveCapitals){
logger.debug("Removing capitals");
inputString = inputString.toUpperCase();
}
if(!preserveWhitespace){
logger.debug("Removing whitespace");
inputString = inputString.replaceAll("[\\s]", "");
}
if(!preserveSymbols){
logger.debug("Removing symbols");
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
}
logger.debug("Cleaned input string '{}'", inputString);
this.inputString = inputString;
logger.debug("Checking length");
if(getCleanInputString().isBlank() || ((getCleanInputString().length() % key.getNumRows()) != 0)){
throw new InvalidInputException("Length of input string must be a multiple of the number of rows in the key");
}
} }
private void setCharacterToAdd(char characterToAdd) throws InvalidCharacterException{ protected String getCleanInputString(){
logger.debug("Cleaning inputString");
String cleanInputString = inputString.toUpperCase().replaceAll("[^A-Z]", "");
logger.debug("Clean input string '{}'", cleanInputString);
return cleanInputString;
}
protected void setCharacterToAdd(char characterToAdd) throws InvalidCharacterException{
logger.debug("Setting character to add {}", characterToAdd);
//Make sure the character is a letter //Make sure the character is a letter
if(!Character.isAlphabetic(characterToAdd)){ if(!Character.isAlphabetic(characterToAdd)){
throw new InvalidCharacterException("Character to add must be a letter"); throw new InvalidCharacterException("Character to add must be a letter");
@@ -97,37 +160,45 @@ public class Hill{
//Save the characterToAdd //Save the characterToAdd
if(!preserveCapitals){ if(!preserveCapitals){
this.characterToAdd = Character.toUpperCase(characterToAdd); logger.debug("Removing capitals");
}
else{ characterToAdd = Character.toUpperCase(characterToAdd);
this.characterToAdd = characterToAdd;
} }
logger.debug("Cleaned character {}", characterToAdd);
this.characterToAdd = characterToAdd;
} }
private String polishOutputString(){ protected String polishOutputString(){
logger.debug("Polishing output string");
//Add the extra characters back to the output and remove the added characters //Add the extra characters back to the output and remove the added characters
int outputCnt = 0; int outputCnt = 0;
StringBuilder outputBuilder = new StringBuilder(); StringBuilder outputBuilder = new StringBuilder();
for(char ch : inputString.toCharArray()){ for(char ch : inputString.toCharArray()){
logger.debug("Current char {}", ch);
if(Character.isUpperCase(ch)){ if(Character.isUpperCase(ch)){
logger.debug("Uppercase");
outputBuilder.append(Character.toUpperCase(outputString.charAt(outputCnt++))); outputBuilder.append(Character.toUpperCase(outputString.charAt(outputCnt++)));
} }
else if(Character.isLowerCase(ch)){ else if(Character.isLowerCase(ch)){
logger.debug("Lowercase");
outputBuilder.append(Character.toLowerCase(outputString.charAt(outputCnt++))); outputBuilder.append(Character.toLowerCase(outputString.charAt(outputCnt++)));
} }
else{ else{
logger.debug("Symbol");
outputBuilder.append(ch); outputBuilder.append(ch);
} }
} }
if(removePadding){ logger.debug("Polished string '{}'", outputBuilder.toString());
String tempString = outputBuilder.substring(0, outputBuilder.length() - charsAdded);
outputBuilder = new StringBuilder();
outputBuilder.append(tempString);
}
return outputBuilder.toString(); return outputBuilder.toString();
} }
private ArrayList<ModMatrix> getInputVectors(){ protected ArrayList<ModMatrix> getInputVectors(){
logger.debug("Generating input vectors");
//Get the number of columns in the key //Get the number of columns in the key
int numCols = key.getNumCols(); int numCols = key.getNumCols();
@@ -139,6 +210,7 @@ public class Hill{
for(int cnt = 0;cnt < cleanInput.length();cnt += numCols){ for(int cnt = 0;cnt < cleanInput.length();cnt += numCols){
String subString = cleanInput.substring(cnt, cnt + numCols); String subString = cleanInput.substring(cnt, cnt + numCols);
int[] grid = new int[numCols]; int[] grid = new int[numCols];
logger.debug("Current substring '{}'", subString);
//Subtract 65 from each character so that A=0, B=1, ... //Subtract 65 from each character so that A=0, B=1, ...
for(int subCnt = 0;subCnt < subString.length();++subCnt){ for(int subCnt = 0;subCnt < subString.length();++subCnt){
@@ -148,6 +220,7 @@ public class Hill{
//Create a vector from the new values //Create a vector from the new values
ModMatrix vector = new ModMatrix(26); ModMatrix vector = new ModMatrix(26);
vector.addCol(grid); vector.addCol(grid);
logger.debug("Current vector {}", vector);
//Add the vector to the array //Add the vector to the array
vectors.add(vector); vectors.add(vector);
@@ -156,10 +229,14 @@ public class Hill{
//Return the array of vectors //Return the array of vectors
return vectors; return vectors;
} }
private String getOutputFromVectors(ArrayList<ModMatrix> outputVectors){ protected String getOutputFromVectors(ArrayList<ModMatrix> outputVectors){
logger.debug("Turning vectors into a string");
//Go through each element in the vector //Go through each element in the vector
StringBuilder outputBuilder = new StringBuilder(); StringBuilder outputBuilder = new StringBuilder();
for(ModMatrix vector : outputVectors){ for(ModMatrix vector : outputVectors){
logger.debug("Current vector {}", vector);
//Add 65 to each element and add it to the string //Add 65 to each element and add it to the string
for(int cnt = 0;cnt < vector.getNumRows();++cnt){ for(int cnt = 0;cnt < vector.getNumRows();++cnt){
outputBuilder.append((char)(vector.get(cnt, 0) + 65)); outputBuilder.append((char)(vector.get(cnt, 0) + 65));
@@ -167,16 +244,24 @@ public class Hill{
} }
//Return the new string //Return the new string
return outputBuilder.toString(); String convertedString = outputBuilder.toString();
logger.debug("Converted string '{}'", convertedString);
return convertedString;
} }
private String encode(){ protected void encode(){
logger.debug("Encoding");
//Get an array of vectors that we are going to encode //Get an array of vectors that we are going to encode
ArrayList<ModMatrix> inputVectors = getInputVectors(); ArrayList<ModMatrix> inputVectors = getInputVectors();
//Multiply the key by each vector and add the result to a new vector //Multiply the key by each vector and add the result to a new vector
logger.debug("Multiplying vectors");
ArrayList<ModMatrix> outputVectors = new ArrayList<>(); ArrayList<ModMatrix> outputVectors = new ArrayList<>();
for(ModMatrix inputVector : inputVectors){ for(ModMatrix inputVector : inputVectors){
logger.debug("Current input vector {}", inputVector);
ModMatrix outputVector = key.multiply(inputVector); ModMatrix outputVector = key.multiply(inputVector);
logger.debug("Multiplied vector {}", outputVector);
outputVectors.add(outputVector); outputVectors.add(outputVector);
} }
@@ -185,19 +270,23 @@ public class Hill{
//Add the extra characters back to the output and remove the added characters //Add the extra characters back to the output and remove the added characters
outputString = polishOutputString(); outputString = polishOutputString();
//Save the output
return outputString;
} }
private String decode(){ protected void decode(){
logger.debug("Decoding");
//Get the array of vectors that we are going to decode //Get the array of vectors that we are going to decode
ArrayList<ModMatrix> inputVectors = getInputVectors(); ArrayList<ModMatrix> inputVectors = getInputVectors();
//Multiply the inverse of the key by each vector and add the result to a new vector //Multiply the inverse of the key by each vector and add the result to a new vector
logger.debug("Getting inverse of key");
ModMatrix inverseKey = key.inverse(); ModMatrix inverseKey = key.inverse();
logger.debug("Inverse of key {}", inverseKey);
ArrayList<ModMatrix> outputVectors = new ArrayList<>(); ArrayList<ModMatrix> outputVectors = new ArrayList<>();
for(ModMatrix inputVector : inputVectors){ for(ModMatrix inputVector : inputVectors){
logger.debug("Current input vector {}", inputVector);
ModMatrix outputVector = inverseKey.multiply(inputVector); ModMatrix outputVector = inverseKey.multiply(inputVector);
logger.debug("Multiplied vector {}", outputVector);
outputVectors.add(outputVector); outputVectors.add(outputVector);
} }
@@ -206,32 +295,26 @@ public class Hill{
//Add the extra characters back to the output and remove the added characters //Add the extra characters back to the output and remove the added characters
outputString = polishOutputString(); outputString = polishOutputString();
//Save the output
return outputString;
} }
public Hill() throws InvalidCharacterException{ public Hill() throws InvalidCharacterException{
preserveCapitals = false; preserveCapitals = false;
preserveWhitespace = false; preserveWhitespace = false;
preserveSymbols = false; preserveSymbols = false;
removePadding = false;
setCharacterToAdd('x'); setCharacterToAdd('x');
reset(); reset();
} }
public Hill(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, boolean removePadding) throws InvalidCharacterException{ public Hill(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
this.preserveCapitals = preserveCapitals; this.preserveCapitals = preserveCapitals;
this.preserveWhitespace = preserveWhitespace; this.preserveWhitespace = preserveWhitespace;
this.preserveSymbols = preserveSymbols; this.preserveSymbols = preserveSymbols;
this.removePadding = removePadding;
setCharacterToAdd('x'); setCharacterToAdd('x');
reset(); reset();
} }
public Hill(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, boolean removePadding, char characterToAdd) throws InvalidCharacterException{ public Hill(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, char characterToAdd) throws InvalidCharacterException{
this.preserveCapitals = preserveCapitals; this.preserveCapitals = preserveCapitals;
this.preserveWhitespace = preserveWhitespace; this.preserveWhitespace = preserveWhitespace;
this.preserveSymbols = preserveSymbols; this.preserveSymbols = preserveSymbols;
this.removePadding = removePadding;
setCharacterToAdd(characterToAdd); setCharacterToAdd(characterToAdd);
reset(); reset();
} }
@@ -241,22 +324,25 @@ public class Hill{
} }
public String encode(ModMatrix key, String inputString) throws InvalidKeyException, InvalidInputException{ public String encode(ModMatrix key, String inputString) throws InvalidKeyException, InvalidInputException{
setKey(key); setKey(key);
setInputString(inputString); setInputStringEncode(inputString);
return encode(); encode();
return outputString;
} }
public String decode(int[][] key, String inputString) throws InvalidKeyException, InvalidInputException{ public String decode(int[][] key, String inputString) throws InvalidKeyException, InvalidInputException{
return decode(new ModMatrix(key, 26), inputString); return decode(new ModMatrix(key, 26), inputString);
} }
public String decode(ModMatrix key, String inputString) throws InvalidKeyException, InvalidInputException{ public String decode(ModMatrix key, String inputString) throws InvalidKeyException, InvalidInputException{
setKey(key); setKey(key);
setInputString(inputString); setInputStringDecode(inputString);
return decode(); decode();
return outputString;
} }
public void reset(){ public void reset(){
logger.debug("Resetting fields");
inputString = ""; inputString = "";
outputString = ""; outputString = "";
key = new ModMatrix(26); key = new ModMatrix(26);
charsAdded = 0;
} }
public String getInputString(){ public String getInputString(){
return inputString; return inputString;

View File

@@ -6,6 +6,7 @@ package com.mattrixwv.cipherstream.polysubstitution;
public class Morse{ public class Morse{
//TODO: This needs updated to match new standards
//Holds the Morse representation of the alphanumeric characters //Holds the Morse representation of the alphanumeric characters
private static final String[] code = { private static final String[] code = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", //A-L ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", //A-L

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Playfair.java //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Playfair.java
//Matthew Ellison //Matthew Ellison
// Created: 07-30-21 // Created: 07-30-21
//Modified: 07-09-22 //Modified: 04-28-23
package com.mattrixwv.cipherstream.polysubstitution; package com.mattrixwv.cipherstream.polysubstitution;
@@ -12,15 +12,16 @@ import org.slf4j.LoggerFactory;
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException; import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Playfair{ public class Playfair{
private static final Logger logger = LoggerFactory.getLogger(Playfair.class); protected static Logger logger = LoggerFactory.getLogger(Playfair.class);
//A class representing the location of a character in the grid //A class representing the location of a character in the grid
private class CharLocation{ protected class CharLocation{
private int x; protected int x;
private int y; protected int y;
public CharLocation(int x, int y){ public CharLocation(int x, int y){
this.x = x; this.x = x;
this.y = y; this.y = y;
@@ -34,23 +35,122 @@ public class Playfair{
} }
//Fields //Fields
private boolean preserveCapitals; //Whether to respect captials in the output string protected boolean preserveCapitals; //Whether to respect captials in the output string
private boolean preserveWhitespace; //Whether to respect whitespace in the output string protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
private boolean preserveSymbols; //Whether to respect symbols in the output string protected boolean preserveSymbols; //Whether to respect symbols in the output string
private char replaced; //The letter that will need to be replaced in the grid and any input string or keyword protected char replaced; //The letter that will need to be replaced in the grid and any input string or keyword
private char replacer; //The letter that replaced replaced in the input string or keyword protected char replacer; //The letter that replaced replaced in the input string or keyword
private char doubled; //The letter that will be placed between double letters in the input string if necessary or to make the string length even protected char doubled; //The letter that will be placed between double letters in the input string if necessary or to make the string length even
private String inputString; //The message that needs to be encoded/decoded protected String inputString; //The message that needs to be encoded/decoded
private String outputString; //The encoded/decoded message protected String outputString; //The encoded/decoded message
private String keyword; //The keyword used to create the grid protected String keyword; //The keyword used to create the grid
private char[][] grid; //The grid used to encode/decode the message protected char[][] grid; //The grid used to encode/decode the message
//Set the doubled character
protected void setDoubled(char doubled) throws InvalidCharacterException{
logger.debug("Setting doubled");
logger.debug("Original character {}", doubled);
//Make sure the character is a letter
logger.debug("Checking letter");
if(!Character.isAlphabetic(doubled)){
throw new InvalidCharacterException("The double letter replacement must be a letter");
}
//Make sure the 2 replacers are the same
logger.debug("Checking same as replacer");
if(doubled == replacer){
throw new InvalidCharacterException("The double letter replacement cannot be the same as the regular replacer");
}
//Make sure the double letter replacement isn't the same as the letter being replaced
logger.debug("Checking same as replaced");
if(doubled == replaced){
throw new InvalidCharacterException("The double letter replacement cannot be the same as the letter replaced");
}
if(!preserveCapitals){
logger.debug("Removing capitals");
doubled = Character.toUpperCase(doubled);
}
logger.debug("Setting doubled to {}", doubled);
this.doubled = doubled;
}
//Set the replacer character
protected void setReplacer(char replacer) throws InvalidCharacterException{
logger.debug("Setting replacer");
logger.debug("Original character {}", replacer);
//Make sure the character is a letter
logger.debug("Checking letter");
if(!Character.isAlphabetic(replacer)){
throw new InvalidCharacterException("The replacer must be a letter");
}
//Make sure the character isn't the same as what it is supposed to replace
logger.debug("Checking same as replaced");
if(replacer == replaced){
throw new InvalidCharacterException("The replacer cannot be the same letter as what it is replacing");
}
//Make sure the replacer isn't the same as the double letter replacer
logger.debug("Checking same as doubled");
if(replacer == doubled){
throw new InvalidCharacterException("The replacer cannot be the same as the double letter replacer");
}
if(!preserveCapitals){
logger.debug("Removing capitals");
replacer = Character.toUpperCase(replacer);
}
logger.debug("Setting replacer to {}", replacer);
this.replacer = replacer;
}
//Set the replaced character
protected void setReplaced(char replaced) throws InvalidCharacterException{
logger.debug("Setting replaced");
logger.debug("Original character {}", replaced);
//Make sure the character is a letter
logger.debug("Checking letter");
if(!Character.isAlphabetic(replaced)){
throw new InvalidCharacterException("The replaced character must be a letter");
}
//Make sure the character isn't the same as what is replacing it
logger.debug("Checking same as replacer");
if(replaced == replacer){
throw new InvalidCharacterException("The replaced letter cannot be the same as the replacing letter");
}
//Make sure the replacer isn't the same as the double letter replacer
logger.debug("Checking same as doubled");
if(replaced == doubled){
throw new InvalidCharacterException("The replaced letter cannot be the same as the replacing letter");
}
if(!preserveCapitals){
logger.debug("Removing capitals");
replaced = Character.toUpperCase(replaced);
}
logger.debug("Setting replaced to {}", replaced);
this.replaced = replaced;
}
//Create the grid from the keyword //Create the grid from the keyword
private void createGrid(){ protected void createGrid(){
logger.debug("Creating grid from keyword"); logger.debug("Creating grid from keyword");
for(int row = 0;row < 5;++row){ for(int row = 0;row < 5;++row){
for(int col = 0;col < 5;++col){ for(int col = 0;col < 5;++col){
char letter = keyword.charAt((5 * row) + col); char letter = keyword.charAt((5 * row) + col);
logger.debug("Letter {} going to position [{}] [{}]", letter, row, col);
grid[row][col] = letter; grid[row][col] = letter;
} }
} }
@@ -58,10 +158,12 @@ public class Playfair{
logger.debug("Grid\n{}", getGrid()); logger.debug("Grid\n{}", getGrid());
} }
//Strips invalid characters from the string that needs encoded/decoded //Strips invalid characters from the string that needs encoded/decoded
private void setInputString(String inputString, boolean encoding) throws InvalidCharacterException, InvalidInputException{ protected void setInputString(String inputString, boolean encoding) throws InvalidCharacterException, InvalidInputException{
logger.debug("Setting input string");
//Make sure the input string is not null //Make sure the input string is not null
if(inputString == null){ if(inputString == null){
throw new NullPointerException("The input string cannot be null"); throw new InvalidInputException("The input string cannot be null");
} }
logger.debug("Original input string {}", inputString); logger.debug("Original input string {}", inputString);
@@ -83,18 +185,14 @@ public class Playfair{
inputString = inputString.replaceAll("[^a-zA-Z\\s]", ""); inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
} }
//Make replace all of the replacers with replaced
logger.debug("Replacing all {} with {}", replaced, replacer);
inputString = inputString.replace(Character.toString(replaced), Character.toString(replacer));
//If there is nothing in the input string throw an exception //If there is nothing in the input string throw an exception
if(inputString.isBlank()){ if(inputString.isBlank()){
throw new InvalidCharacterException("The input string cannot be blank"); throw new InvalidInputException("The input string cannot be blank");
} }
//If this is encoding parse it and clean up any problems //If this is encoding parse it and clean up any problems
if(encoding){ if(encoding){
setEncodingInputString(inputString); setInputStringEncode(inputString);
} }
//If this is decoding just add it without parsing it //If this is decoding just add it without parsing it
else{ else{
@@ -105,20 +203,28 @@ public class Playfair{
logger.debug("Clean input string '{}'", inputString); logger.debug("Clean input string '{}'", inputString);
this.inputString = inputString; this.inputString = inputString;
if((getPreparedInputString().length() % 2) == 1){
throw new InvalidInputException("Input length must be even");
}
} }
if(this.inputString.isBlank() || getPreparedInputString().isBlank()){ if(getPreparedInputString().isBlank()){
throw new InvalidInputException("Input must have at least 1 letter"); throw new InvalidInputException("Input must have at least 1 letter");
} }
} }
private void setEncodingInputString(String inputString){ protected void setInputStringEncode(String inputString){
logger.debug("Cleaning up input string for encoding");
//Replace characters that need replaced //Replace characters that need replaced
logger.debug("Replacing all {} with {}", replaced, replacer);
inputString = inputString.replace(Character.toString(replaced), Character.toString(replacer)); inputString = inputString.replace(Character.toString(replaced), Character.toString(replacer));
//Check if there are any doubled characters //Check if there are any doubled characters
StringBuilder cleanInput = new StringBuilder(); StringBuilder cleanInput = new StringBuilder();
int letterCount = 0; int letterCount = 0;
for(int cnt = 0;cnt < inputString.length();){ for(int cnt = 0;cnt < inputString.length();){
logger.debug("Starting at character {}", cnt);
//Advance until you find a letter, saving the input for inclusion //Advance until you find a letter, saving the input for inclusion
StringBuilder prepend = new StringBuilder(); StringBuilder prepend = new StringBuilder();
while((cnt < inputString.length()) && !Character.isAlphabetic(inputString.charAt(cnt))){ while((cnt < inputString.length()) && !Character.isAlphabetic(inputString.charAt(cnt))){
@@ -151,6 +257,7 @@ public class Playfair{
} }
//Add all of the gathered input to the cleaned up input //Add all of the gathered input to the cleaned up input
logger.debug("Adding to clean input: {} {} {} {}", prepend, firstLetter, middle, secondLetter);
cleanInput.append(prepend); cleanInput.append(prepend);
cleanInput.append(firstLetter); cleanInput.append(firstLetter);
cleanInput.append(middle); cleanInput.append(middle);
@@ -160,7 +267,9 @@ public class Playfair{
} }
//Check if there are an odd number of characters //Check if there are an odd number of characters
logger.debug("Checking odd characters");
if((letterCount % 2) == 1){ if((letterCount % 2) == 1){
logger.debug("Adding final character to make even");
int lastLetterLocation = cleanInput.length() - 1; int lastLetterLocation = cleanInput.length() - 1;
while(!Character.isAlphabetic(cleanInput.charAt(lastLetterLocation))){ while(!Character.isAlphabetic(cleanInput.charAt(lastLetterLocation))){
--lastLetterLocation; --lastLetterLocation;
@@ -173,11 +282,11 @@ public class Playfair{
} }
} }
logger.debug("Cleaned input string '{}'", cleanInput);
this.inputString = cleanInput.toString(); this.inputString = cleanInput.toString();
logger.debug("Cleaned input string '{}'", this.inputString);
} }
//Returns the input string ready for encoding //Returns the input string ready for encoding
private String getPreparedInputString(){ protected String getPreparedInputString(){
logger.debug("Getting input string ready for encoding"); logger.debug("Getting input string ready for encoding");
String cleanString = inputString.toUpperCase(); String cleanString = inputString.toUpperCase();
@@ -188,9 +297,11 @@ public class Playfair{
return cleanString; return cleanString;
} }
//Strips invalid characters from the keyword and creates the grid //Strips invalid characters from the keyword and creates the grid
private void setKeyword(String keyword){ protected void setKeyword(String keyword) throws InvalidKeywordException{
logger.debug("Setting keyword");
if(keyword == null){ if(keyword == null){
throw new NullPointerException("Keyword cannot be null"); throw new InvalidKeywordException("Keyword cannot be null");
} }
logger.debug("Original keyword {}", keyword); logger.debug("Original keyword {}", keyword);
@@ -209,19 +320,20 @@ public class Playfair{
//Replace all replaced characters //Replace all replaced characters
logger.debug("Replacing {} with {}", replaced, replacer); logger.debug("Replacing {} with {}", replaced, replacer);
keyword = keyword.replaceAll(Character.toString(replaced), Character.toString(replacer)); keyword = keyword.replaceAll(Character.toString(Character.toUpperCase(replaced)), Character.toString(Character.toUpperCase(replacer)));
//Remove all duplicate chatacters //Remove all duplicate chatacters
logger.debug("Removing duplicated characters"); logger.debug("Removing duplicate characters");
StringBuilder uniqueKey = new StringBuilder(); StringBuilder uniqueKey = new StringBuilder();
keyword.chars().distinct().forEach(c -> uniqueKey.append((char)c)); keyword.chars().distinct().forEach(c -> uniqueKey.append((char)c));
this.keyword = uniqueKey.toString(); this.keyword = uniqueKey.toString();
logger.debug("Cleaned keyword {}", this.keyword);
//Create the grid from the sanitized keyword //Create the grid from the sanitized keyword
createGrid(); createGrid();
} }
//Returns the location of the given character in the grid //Returns the location of the given character in the grid
private CharLocation findChar(char letter) throws InvalidInputException{ protected CharLocation findChar(char letter) throws InvalidInputException{
logger.debug("Finding character in grid {}", letter); logger.debug("Finding character in grid {}", letter);
for(int row = 0;row < grid.length;++row){ for(int row = 0;row < grid.length;++row){
@@ -238,7 +350,7 @@ public class Playfair{
throw new InvalidInputException("The character '" + letter + "' was not found in the grid"); throw new InvalidInputException("The character '" + letter + "' was not found in the grid");
} }
//Returns the location in the grid of x and y, adjusting for out of bounds //Returns the location in the grid of x and y, adjusting for out of bounds
private char getGridChar(int x, int y){ protected char getGridChar(int x, int y){
logger.debug("Getting character from grid[{}][{}]", x, y); logger.debug("Getting character from grid[{}][{}]", x, y);
if(x < 0){ if(x < 0){
@@ -253,7 +365,7 @@ public class Playfair{
return letter; return letter;
} }
//Adds characters that aren't letters to the output //Adds characters that aren't letters to the output
private void addCharactersToCleanString(String cleanString){ protected void addCharactersToCleanString(String cleanString){
logger.debug("Formatting output string"); logger.debug("Formatting output string");
int outputCnt = 0; int outputCnt = 0;
@@ -262,27 +374,27 @@ public class Playfair{
logger.debug("Working character {}", inputString.charAt(inputCnt)); logger.debug("Working character {}", inputString.charAt(inputCnt));
if(Character.isUpperCase(inputString.charAt(inputCnt))){ if(Character.isUpperCase(inputString.charAt(inputCnt))){
logger.debug("Adjusting uppercase"); logger.debug("Appending uppercase");
fullOutput.append(cleanString.charAt(outputCnt++)); fullOutput.append(cleanString.charAt(outputCnt++));
} }
else if(Character.isLowerCase(inputString.charAt(inputCnt))){ else if(Character.isLowerCase(inputString.charAt(inputCnt))){
logger.debug("Adjusting lowercase"); logger.debug("Appending lowercase");
fullOutput.append(Character.toLowerCase(cleanString.charAt(outputCnt++))); fullOutput.append(Character.toLowerCase(cleanString.charAt(outputCnt++)));
} }
else{ else{
logger.debug("Inserting symbol"); logger.debug("Appending symbol");
fullOutput.append(inputString.charAt(inputCnt)); fullOutput.append(inputString.charAt(inputCnt));
} }
} }
logger.debug("Formatted output '{}'", fullOutput);
outputString = fullOutput.toString(); outputString = fullOutput.toString();
logger.debug("Formatted output '{}'", outputString);
} }
//Encodes inputString using the Playfair cipher and stores the result in outputString //Encodes inputString using the Playfair cipher and stores the result in outputString
private String encode() throws InvalidInputException{ protected void encode() throws InvalidInputException{
logger.debug("Encoding"); logger.debug("Encoding");
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
@@ -324,12 +436,9 @@ public class Playfair{
//Add other characters to the output string //Add other characters to the output string
addCharactersToCleanString(output.toString()); addCharactersToCleanString(output.toString());
//Return the output string
return outputString;
} }
//Decodes inputString using the Playfair cipher and stores the result in outputString //Decodes inputString using the Playfair cipher and stores the result in outputString
private String decode() throws InvalidInputException{ protected void decode() throws InvalidInputException{
logger.debug("Decoding"); logger.debug("Decoding");
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
@@ -348,17 +457,17 @@ public class Playfair{
//Decode the letters //Decode the letters
if(firstLocation.getX() == secondLocation.getX()){ if(firstLocation.getX() == secondLocation.getX()){
logger.debug("Decoding row"); logger.debug("Row decoding");
firstLetter = getGridChar(firstLocation.getX(), firstLocation.getY() - 1); firstLetter = getGridChar(firstLocation.getX(), firstLocation.getY() - 1);
secondLetter = getGridChar(secondLocation.getX(), secondLocation.getY() - 1); secondLetter = getGridChar(secondLocation.getX(), secondLocation.getY() - 1);
} }
else if(firstLocation.getY() == secondLocation.getY()){ else if(firstLocation.getY() == secondLocation.getY()){
logger.debug("Decoding col"); logger.debug("Column decoding");
firstLetter = getGridChar(firstLocation.getX() - 1, firstLocation.getY()); firstLetter = getGridChar(firstLocation.getX() - 1, firstLocation.getY());
secondLetter = getGridChar(secondLocation.getX() - 1, secondLocation.getY()); secondLetter = getGridChar(secondLocation.getX() - 1, secondLocation.getY());
} }
else{ else{
logger.debug("Decoding corners"); logger.debug("Corner decoding");
firstLetter = getGridChar(firstLocation.getX(), secondLocation.getY()); firstLetter = getGridChar(firstLocation.getX(), secondLocation.getY());
secondLetter = getGridChar(secondLocation.getX(), firstLocation.getY()); secondLetter = getGridChar(secondLocation.getX(), firstLocation.getY());
} }
@@ -371,9 +480,6 @@ public class Playfair{
//Add other characters to the output string //Add other characters to the output string
addCharactersToCleanString(output.toString()); addCharactersToCleanString(output.toString());
//Return the output string
return outputString;
} }
public Playfair() throws InvalidCharacterException{ public Playfair() throws InvalidCharacterException{
@@ -381,18 +487,18 @@ public class Playfair{
preserveCapitals = false; preserveCapitals = false;
preserveWhitespace = false; preserveWhitespace = false;
preserveSymbols = false; preserveSymbols = false;
setReplaced('J'); setReplaced('j');
setReplacer('I'); setReplacer('i');
setDoubled('X'); setDoubled('x');
} }
public Playfair(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{ public Playfair(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
reset(); reset();
this.preserveCapitals = preserveCapitals; this.preserveCapitals = preserveCapitals;
this.preserveWhitespace = preserveWhitespace; this.preserveWhitespace = preserveWhitespace;
this.preserveSymbols = preserveSymbols; this.preserveSymbols = preserveSymbols;
setReplaced('J'); setReplaced('j');
setReplacer('I'); setReplacer('i');
setDoubled('X'); setDoubled('x');
} }
public Playfair(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, char replaced, char replacer, char doubled) throws InvalidCharacterException{ public Playfair(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, char replaced, char replacer, char doubled) throws InvalidCharacterException{
reset(); reset();
@@ -408,14 +514,16 @@ public class Playfair{
reset(); reset();
setKeyword(keyword); setKeyword(keyword);
setInputString(input, true); setInputString(input, true);
return encode(); encode();
return outputString;
} }
//Sets the keyword and inputString and decodes the message //Sets the keyword and inputString and decodes the message
public String decode(String keyword, String input) throws InvalidCharacterException, InvalidInputException{ public String decode(String keyword, String input) throws InvalidCharacterException, InvalidInputException{
reset(); reset();
setKeyword(keyword); setKeyword(keyword);
setInputString(input, false); setInputString(input, false);
return decode(); decode();
return outputString;
} }
//Makes sure all variables are empty //Makes sure all variables are empty
@@ -431,64 +539,12 @@ public class Playfair{
public char getReplaced(){ public char getReplaced(){
return replaced; return replaced;
} }
public void setReplaced(char replaced) throws InvalidCharacterException{
if(!Character.isAlphabetic(replaced)){
throw new InvalidCharacterException("The replaced character must be a letter");
}
if(replaced == replacer){
throw new InvalidCharacterException("The replaced letter cannot be the same as the replacing letter");
}
if(replaced == doubled){
throw new InvalidCharacterException("The replaced letter cannot be the same as the replacing letter");
}
this.replaced = Character.toUpperCase(replaced);
}
public char getReplacer(){ public char getReplacer(){
return replacer; return replacer;
} }
public void setReplacer(char replacer) throws InvalidCharacterException{
//Make sure the character is a letter
if(!Character.isAlphabetic(replacer)){
throw new InvalidCharacterException("The replacer must be a letter");
}
//Make sure the character isn't the same as what it is supposed to replace
if(replacer == replaced){
throw new InvalidCharacterException("The replacer cannot be the same letter as what it is replacing");
}
//Make sure the replacer isn't the same as the double letter replacer
if(replacer == doubled){
throw new InvalidCharacterException("The replacer cannot be the same as the double letter replacer");
}
this.replacer = Character.toUpperCase(replacer);
}
public char getDoubled(){ public char getDoubled(){
return doubled; return doubled;
} }
public void setDoubled(char doubled) throws InvalidCharacterException{
//Make sure the character is a letter
if(!Character.isAlphabetic(doubled)){
throw new InvalidCharacterException("The double letter replacement must be a letter");
}
//Make sure the 2 replacers are the same
if(doubled == replacer){
throw new InvalidCharacterException("The double letter replacement cannot be the same as the regular replacer");
}
//Make sure the double letter replacement isn't the same as the letter being replaced
if(doubled == replaced){
throw new InvalidCharacterException("The double letter replacement cannot be the same as the letter replaced");
}
this.doubled = Character.toUpperCase(doubled);
}
public String getKeyword(){ public String getKeyword(){
return keyword; return keyword;
} }

View File

@@ -96,7 +96,7 @@ public class TestBaconian{
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
cipher.setInputStringEncode(""); cipher.setInputStringEncode("");
}, "Input must contain at least 1 letter"); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, times(1)).debug("Setting input string for encoding '{}'", ""); verify(logger, times(1)).debug("Setting input string for encoding '{}'", "");
@@ -112,7 +112,7 @@ public class TestBaconian{
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
cipher.setInputStringEncode(null); cipher.setInputStringEncode(null);
}, "Input cannot be null"); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, never()).debug(anyString()); verify(logger, never()).debug(anyString());
@@ -159,7 +159,7 @@ public class TestBaconian{
assertThrows(InvalidCharacterException.class, () -> { assertThrows(InvalidCharacterException.class, () -> {
cipher.setInputStringDecode("a"); cipher.setInputStringDecode("a");
}, "All Baconian letters contain exactly 5 characters: a"); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, times(1)).debug("Setting input string for decoding '{}'", "a"); verify(logger, times(1)).debug("Setting input string for decoding '{}'", "a");
@@ -178,7 +178,7 @@ public class TestBaconian{
assertThrows(InvalidCharacterException.class, () -> { assertThrows(InvalidCharacterException.class, () -> {
cipher.setInputStringDecode("ccccc"); cipher.setInputStringDecode("ccccc");
}, "Baconian letters contain only a's and b's: ccccc"); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, times(1)).debug("Setting input string for decoding '{}'", "ccccc"); verify(logger, times(1)).debug("Setting input string for decoding '{}'", "ccccc");
@@ -197,7 +197,7 @@ public class TestBaconian{
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
cipher.setInputStringDecode(""); cipher.setInputStringDecode("");
}, "Input cannot be empty"); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, never()).debug("Setting input string for decoding '{}'", ""); verify(logger, never()).debug("Setting input string for decoding '{}'", "");
@@ -216,7 +216,7 @@ public class TestBaconian{
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
cipher.setInputStringDecode(null); cipher.setInputStringDecode(null);
}, "Input cannot be null"); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, never()).debug(anyString()); verify(logger, never()).debug(anyString());

View File

@@ -76,7 +76,7 @@ public class TestBaseX{
public void testSetInputStringEncode_blank(){ public void testSetInputStringEncode_blank(){
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
cipher.setInputStringEncode(""); cipher.setInputStringEncode("");
}, "Input must contain at least 1 letter"); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, times(1)).debug("Setting input string for encoding '{}'", ""); verify(logger, times(1)).debug("Setting input string for encoding '{}'", "");
@@ -88,7 +88,7 @@ public class TestBaseX{
public void testSetInputStringEncode_null(){ public void testSetInputStringEncode_null(){
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
cipher.setInputStringEncode(null); cipher.setInputStringEncode(null);
}, "Input cannot be null"); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, never()).debug("Setting input string for encoding '{}'", decodedString); verify(logger, never()).debug("Setting input string for encoding '{}'", decodedString);
@@ -116,7 +116,7 @@ public class TestBaseX{
assertThrows(InvalidCharacterException.class, () -> { assertThrows(InvalidCharacterException.class, () -> {
cipher.setInputStringDecode("G"); cipher.setInputStringDecode("G");
}, "inputString cannot contain anything except numbers 0-15, and whitespace"); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, times(1)).debug("Setting input string for decoding '{}'", "G"); verify(logger, times(1)).debug("Setting input string for decoding '{}'", "G");
@@ -132,7 +132,7 @@ public class TestBaseX{
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
cipher.setInputStringDecode(""); cipher.setInputStringDecode("");
}, "Input must contain at least 1 letter"); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, times(1)).debug("Setting input string for decoding '{}'", ""); verify(logger, times(1)).debug("Setting input string for decoding '{}'", "");
@@ -148,7 +148,7 @@ public class TestBaseX{
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
cipher.setInputStringDecode(null); cipher.setInputStringDecode(null);
}, "Input cannot be null"); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, never()).debug(eq("Setting input string for decoding '{}'"), anyString()); verify(logger, never()).debug(eq("Setting input string for decoding '{}'"), anyString());
@@ -172,7 +172,7 @@ public class TestBaseX{
public void testSetBase_min(){ public void testSetBase_min(){
assertThrows(InvalidBaseException.class, () -> { assertThrows(InvalidBaseException.class, () -> {
cipher.setBase(Character.MIN_RADIX - 1); cipher.setBase(Character.MIN_RADIX - 1);
}, "Base cannot be less than " + Character.MIN_RADIX); });
assertEquals(2, cipher.base); assertEquals(2, cipher.base);
verify(logger, never()).debug(eq("Setting base {}"), anyInt()); verify(logger, never()).debug(eq("Setting base {}"), anyInt());
@@ -184,7 +184,7 @@ public class TestBaseX{
public void testSetBase_max(){ public void testSetBase_max(){
assertThrows(InvalidBaseException.class, () -> { assertThrows(InvalidBaseException.class, () -> {
cipher.setBase(Character.MAX_RADIX + 1); cipher.setBase(Character.MAX_RADIX + 1);
}, "Base cannot be larger than " + Character.MAX_RADIX); });
assertEquals(2, cipher.base); assertEquals(2, cipher.base);
verify(logger, never()).debug(eq("Setting base {}"), anyInt()); verify(logger, never()).debug(eq("Setting base {}"), anyInt());
@@ -227,7 +227,7 @@ public class TestBaseX{
assertThrows(InvalidCharacterException.class, () -> { assertThrows(InvalidCharacterException.class, () -> {
cipher.decode(); cipher.decode();
}, "The base 16 string FFF is not a valid ASCII character"); });
verify(logger, times(1)).debug("Decoding"); verify(logger, times(1)).debug("Decoding");
verify(logger, times(1)).debug("Current number {}", "FFF"); verify(logger, times(1)).debug("Current number {}", "FFF");

View File

@@ -208,7 +208,7 @@ public class TestBeaufort{
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
cipher.setInputString(""); cipher.setInputString("");
}, "Input must contain at least 1 letter"); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", ""); verify(logger, times(1)).debug("Original input string '{}'", "");
@@ -224,7 +224,7 @@ public class TestBeaufort{
public void testSetInputString_null(){ public void testSetInputString_null(){
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
cipher.setInputString(null); cipher.setInputString(null);
}, "Input must conatin at least 1 letter"); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, never()).debug(eq("Original input string '{}'"), anyString()); verify(logger, never()).debug(eq("Original input string '{}'"), anyString());
@@ -266,7 +266,7 @@ public class TestBeaufort{
public void testSetKeyword_blank(){ public void testSetKeyword_blank(){
assertThrows(InvalidKeywordException.class, () -> { assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword(""); cipher.setKeyword("");
}, "Keyword must contain at least 2 letters"); });
assertEquals("", cipher.keyword); assertEquals("", cipher.keyword);
verify(logger, times(1)).debug("Original keyword '{}'", ""); verify(logger, times(1)).debug("Original keyword '{}'", "");
@@ -281,7 +281,7 @@ public class TestBeaufort{
public void testSetKeyword_short(){ public void testSetKeyword_short(){
assertThrows(InvalidKeywordException.class, () -> { assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword("A"); cipher.setKeyword("A");
}, "Keyword must contain at least 2 letters"); });
assertEquals("A", cipher.keyword); assertEquals("A", cipher.keyword);
verify(logger, times(1)).debug("Original keyword '{}'", "A"); verify(logger, times(1)).debug("Original keyword '{}'", "A");
@@ -296,7 +296,7 @@ public class TestBeaufort{
public void testSetKeyword_null(){ public void testSetKeyword_null(){
assertThrows(InvalidKeywordException.class, () -> { assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword(null); cipher.setKeyword(null);
}, "Keyword cannot be null"); });
assertEquals("", cipher.keyword); assertEquals("", cipher.keyword);
verify(logger, never()).debug(eq("Original keyword '{}'"), anyString()); verify(logger, never()).debug(eq("Original keyword '{}'"), anyString());

View File

@@ -190,7 +190,7 @@ public class TestCaesar{
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
cipher.setInputString(""); cipher.setInputString("");
}, "Input must contain at least 1 letter"); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", ""); verify(logger, times(1)).debug("Original input string '{}'", "");
@@ -208,7 +208,7 @@ public class TestCaesar{
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
cipher.setInputString(null); cipher.setInputString(null);
}, "Input must contain at least 1 letter"); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, never()).debug("Original input string '{}'", ""); verify(logger, never()).debug("Original input string '{}'", "");

View File

@@ -137,7 +137,7 @@ public class TestOneTimePad{
assertThrows(InvalidKeywordException.class, () -> { assertThrows(InvalidKeywordException.class, () -> {
cipher.encode("keyword", decodedString); cipher.encode("keyword", decodedString);
}, "Key must be at least as long as the input"); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
assertEquals("", cipher.keyword); assertEquals("", cipher.keyword);

View File

@@ -107,7 +107,7 @@ public class TestPorta{
public void testSetKeyword_blank(){ public void testSetKeyword_blank(){
assertThrows(InvalidKeywordException.class, () -> { assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword(""); cipher.setKeyword("");
}, "Keyword must contain at least 2 letters"); });
assertEquals("", cipher.keyword); assertEquals("", cipher.keyword);
verify(logger, times(1)).debug("Original keyword '{}'", ""); verify(logger, times(1)).debug("Original keyword '{}'", "");
@@ -120,7 +120,7 @@ public class TestPorta{
public void testSetKeyword_short(){ public void testSetKeyword_short(){
assertThrows(InvalidKeywordException.class, () -> { assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword("a"); cipher.setKeyword("a");
}, "Keyword must contain at least 2 letters"); });
assertEquals("A", cipher.keyword); assertEquals("A", cipher.keyword);
verify(logger, times(1)).debug("Original keyword '{}'", "a"); verify(logger, times(1)).debug("Original keyword '{}'", "a");
@@ -133,7 +133,7 @@ public class TestPorta{
public void testSetKeyword_null(){ public void testSetKeyword_null(){
assertThrows(InvalidKeywordException.class, () -> { assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword(null); cipher.setKeyword(null);
}, "Keyword cannot be null"); });
assertEquals("", cipher.keyword); assertEquals("", cipher.keyword);
verify(logger, never()).debug(anyString()); verify(logger, never()).debug(anyString());
@@ -223,7 +223,7 @@ public class TestPorta{
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
cipher.setInputString(""); cipher.setInputString("");
}, "Input must contain at least 1 letter"); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, times(1)).debug("Original input string {}", ""); verify(logger, times(1)).debug("Original input string {}", "");
@@ -241,7 +241,7 @@ public class TestPorta{
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
cipher.setInputString(null); cipher.setInputString(null);
}, "Input cannot be null"); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, never()).debug(anyString(), anyString()); verify(logger, never()).debug(anyString(), anyString());

View File

@@ -172,7 +172,7 @@ public class TestSubstitution{
public void testSetKey_invalidLength(){ public void testSetKey_invalidLength(){
assertThrows(InvalidKeywordException.class, () -> { assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword("AB"); cipher.setKeyword("AB");
}, "The key must contain all letters and can contain all numbers"); });
assertEquals("", cipher.keyword); assertEquals("", cipher.keyword);
verify(logger, times(1)).debug("Original key '{}'", "AB"); verify(logger, times(1)).debug("Original key '{}'", "AB");
@@ -187,7 +187,7 @@ public class TestSubstitution{
public void testSetKey_null(){ public void testSetKey_null(){
assertThrows(InvalidKeywordException.class, () -> { assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword(null); cipher.setKeyword(null);
}, "Key cannot be null"); });
assertEquals("", cipher.keyword); assertEquals("", cipher.keyword);
verify(logger, never()).debug(eq("Original key '{}'"), anyString()); verify(logger, never()).debug(eq("Original key '{}'"), anyString());
@@ -270,7 +270,7 @@ public class TestSubstitution{
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
cipher.setInputString(""); cipher.setInputString("");
}, "Input must contain at least 1 letter"); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", ""); verify(logger, times(1)).debug("Original input string '{}'", "");
@@ -288,7 +288,7 @@ public class TestSubstitution{
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
cipher.setInputString(null); cipher.setInputString(null);
}, "Input cannot be null"); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, never()).debug(eq("Original input string '{}'"), anyString()); verify(logger, never()).debug(eq("Original input string '{}'"), anyString());

View File

@@ -194,7 +194,7 @@ public class TestVigenere{
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
cipher.setInputString(""); cipher.setInputString("");
}, "Input cannot be null"); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", ""); verify(logger, times(1)).debug("Original input string '{}'", "");
@@ -214,7 +214,7 @@ public class TestVigenere{
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
cipher.setInputString(null); cipher.setInputString(null);
}, "Input must contain at least 1 letter"); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, never()).debug(eq("Original input string '{}'"), anyString()); verify(logger, never()).debug(eq("Original input string '{}'"), anyString());
@@ -240,7 +240,7 @@ public class TestVigenere{
public void testSetKeyword_blank(){ public void testSetKeyword_blank(){
assertThrows(InvalidKeywordException.class, () -> { assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword(""); cipher.setKeyword("");
}, "Keyword must contain at least 2 letters"); });
assertEquals("", cipher.keyword); assertEquals("", cipher.keyword);
assertEquals(new ArrayList<>(), cipher.offset); assertEquals(new ArrayList<>(), cipher.offset);
@@ -254,7 +254,7 @@ public class TestVigenere{
public void testSetKeyword_null(){ public void testSetKeyword_null(){
assertThrows(InvalidKeywordException.class, () -> { assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword(null); cipher.setKeyword(null);
}, "Keyword must contain at least 2 letters"); });
assertEquals("", cipher.keyword); assertEquals("", cipher.keyword);
assertEquals(new ArrayList<>(), cipher.offset); assertEquals(new ArrayList<>(), cipher.offset);