Added logging

This commit is contained in:
2022-07-09 16:55:32 -04:00
parent e9c8397b86
commit 2d7382ba8f
44 changed files with 2341 additions and 1265 deletions

View File

@@ -1,15 +1,22 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Playfair.java
//Matthew Ellison
// Created: 07-30-21
//Modified: 02-17-22
//Modified: 07-09-22
package com.mattrixwv.cipherstream.polysubstitution;
import java.util.StringJoiner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
public class Playfair{
private static final Logger logger = LoggerFactory.getLogger(Playfair.class);
//A class representing the location of a character in the grid
private class CharLocation{
private int x;
@@ -26,7 +33,7 @@ public class Playfair{
}
}
//Variables
//Fields
private boolean preserveCapitals; //Whether to respect captials 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
@@ -39,12 +46,16 @@ public class Playfair{
private char[][] grid; //The grid used to encode/decode the message
//Create the grid from the keyword
private void createGrid(){
logger.debug("Creating grid from keyword");
for(int row = 0;row < 5;++row){
for(int col = 0;col < 5;++col){
char letter = keyword.charAt((5 * row) + col);
grid[row][col] = letter;
}
}
logger.debug("Grid\n{}", getGrid());
}
//Strips invalid characters from the string that needs encoded/decoded
private void setInputString(String inputString, boolean encoding) throws InvalidCharacterException, InvalidInputException{
@@ -53,26 +64,35 @@ public class Playfair{
throw new NullPointerException("The input string cannot be null");
}
logger.debug("Original input string {}", inputString);
//Set the options
if(!preserveCapitals){
logger.debug("Removing case");
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]", "");
}
//Make replace all of the replacers with replaced
inputString = inputString.replaceAll(Character.toString(replaced), Character.toString(replacer));
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(inputString.isBlank()){
throw new InvalidCharacterException("The input string cannot be blank");
}
//If this is encoding parse it and clean up an problems
//If this is encoding parse it and clean up any problems
if(encoding){
setEncodingInputString(inputString);
}
@@ -83,6 +103,7 @@ public class Playfair{
throw new InvalidCharacterException("An encoded message cannot contain a letter that needs replaced");
}
logger.debug("Clean input string '{}'", inputString);
this.inputString = inputString;
}
@@ -92,7 +113,7 @@ public class Playfair{
}
private void setEncodingInputString(String inputString){
//Replace characters that need replaced
inputString = inputString.replaceAll(Character.toString(replaced), Character.toString(replacer));
inputString = inputString.replace(Character.toString(replaced), Character.toString(replacer));
//Check if there are any doubled characters
StringBuilder cleanInput = new StringBuilder();
@@ -152,12 +173,18 @@ public class Playfair{
}
}
logger.debug("Cleaned input string '{}'", cleanInput);
this.inputString = cleanInput.toString();
}
//Returns the input string ready for encoding
private String getPreparedInputString(){
logger.debug("Getting input string ready for encoding");
String cleanString = inputString.toUpperCase();
cleanString = cleanString.replaceAll("[^A-Z]", "");
logger.debug("Prepared string '{}'", cleanString);
return cleanString;
}
//Strips invalid characters from the keyword and creates the grid
@@ -166,19 +193,26 @@ public class Playfair{
throw new NullPointerException("Keyword cannot be null");
}
logger.debug("Original keyword {}", keyword);
//Change everything to uppercase
logger.debug("Removing case");
keyword = keyword.toUpperCase();
//Removing everything except capital letters
logger.debug("Removing all non-letter characters");
keyword = keyword.replaceAll("[^A-Z]", "");
//Add all letters in the alphabet to the key
logger.debug("Appending the alphabet to the keyword");
keyword += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//Replace all replaced characters
logger.debug("Replacing {} with {}", replaced, replacer);
keyword = keyword.replaceAll(Character.toString(replaced), Character.toString(replacer));
//Remove all duplicate chatacters
logger.debug("Removing duplicated characters");
StringBuilder uniqueKey = new StringBuilder();
keyword.chars().distinct().forEach(c -> uniqueKey.append((char)c));
this.keyword = uniqueKey.toString();
@@ -188,45 +222,69 @@ public class Playfair{
}
//Returns the location of the given character in the grid
private CharLocation findChar(char letter) throws InvalidInputException{
logger.debug("Finding character in grid {}", letter);
for(int row = 0;row < grid.length;++row){
for(int col = 0;col < grid[row].length;++col){
if(grid[row][col] == letter){
logger.debug("Found at {}, {}", row, col);
return new CharLocation(row, col);
}
}
}
//If it was not found something went wrong
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
private char getGridChar(int x, int y){
logger.debug("Getting character from grid[{}][{}]", x, y);
if(x < 0){
x += 5;
}
if(y < 0){
y += 5;
}
return grid[x % 5][y % 5];
char letter = grid[x % 5][y % 5];
logger.debug("Character {}", letter);
return letter;
}
//Adds characters that aren't letters to the output
private void addCharactersToCleanString(String cleanString){
logger.debug("Formatting output string");
int outputCnt = 0;
StringBuilder fullOutput = new StringBuilder();
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
logger.debug("Working character {}", inputString.charAt(inputCnt));
if(Character.isUpperCase(inputString.charAt(inputCnt))){
logger.debug("Adjusting uppercase");
fullOutput.append(cleanString.charAt(outputCnt++));
}
else if(Character.isLowerCase(inputString.charAt(inputCnt))){
logger.debug("Adjusting lowercase");
fullOutput.append(Character.toLowerCase(cleanString.charAt(outputCnt++)));
}
else{
logger.debug("Inserting symbol");
fullOutput.append(inputString.charAt(inputCnt));
}
}
logger.debug("Formatted output '{}'", fullOutput);
outputString = fullOutput.toString();
}
//Encodes inputString using the Playfair cipher and stores the result in outputString
private String encode() throws InvalidInputException{
logger.debug("Encoding");
StringBuilder output = new StringBuilder();
int inputCnt = 0;
String cleanString = getPreparedInputString();
@@ -235,25 +293,31 @@ public class Playfair{
char firstLetter = cleanString.charAt(inputCnt++);
char secondLetter = cleanString.charAt(inputCnt++);
logger.debug("Letters {} {}", firstLetter, secondLetter);
//Find the letters in the grid
CharLocation firstLocation = findChar(firstLetter);
CharLocation secondLocation = findChar(secondLetter);
//Encode the letters
if(firstLocation.getX() == secondLocation.getX()){
logger.debug("Row encoding");
firstLetter = getGridChar(firstLocation.getX(), firstLocation.getY() + 1);
secondLetter = getGridChar(secondLocation.getX(), secondLocation.getY() + 1);
}
else if(firstLocation.getY() == secondLocation.getY()){
logger.debug("Column encoding");
firstLetter = getGridChar(firstLocation.getX() + 1, firstLocation.getY());
secondLetter = getGridChar(secondLocation.getX() + 1, secondLocation.getY());
}
else{
logger.debug("Corner encoding");
firstLetter = getGridChar(firstLocation.getX(), secondLocation.getY());
secondLetter = getGridChar(secondLocation.getX(), firstLocation.getY());
}
//Add the new letters to the output string
logger.debug("Encoded letters {} {}", firstLetter, secondLetter);
output.append(firstLetter);
output.append(secondLetter);
}
@@ -266,6 +330,8 @@ public class Playfair{
}
//Decodes inputString using the Playfair cipher and stores the result in outputString
private String decode() throws InvalidInputException{
logger.debug("Decoding");
StringBuilder output = new StringBuilder();
int inputCnt = 0;
String cleanString = getPreparedInputString();
@@ -274,25 +340,31 @@ public class Playfair{
char firstLetter = cleanString.charAt(inputCnt++);
char secondLetter = cleanString.charAt(inputCnt++);
logger.debug("Letters {} {}", firstLetter, secondLetter);
//Find the letters in the grid
CharLocation firstLocation = findChar(firstLetter);
CharLocation secondLocation = findChar(secondLetter);
//Decode the letters
if(firstLocation.getX() == secondLocation.getX()){
logger.debug("Decoding row");
firstLetter = getGridChar(firstLocation.getX(), firstLocation.getY() - 1);
secondLetter = getGridChar(secondLocation.getX(), secondLocation.getY() - 1);
}
else if(firstLocation.getY() == secondLocation.getY()){
logger.debug("Decoding col");
firstLetter = getGridChar(firstLocation.getX() - 1, firstLocation.getY());
secondLetter = getGridChar(secondLocation.getX() - 1, secondLocation.getY());
}
else{
logger.debug("Decoding corners");
firstLetter = getGridChar(firstLocation.getX(), secondLocation.getY());
secondLetter = getGridChar(secondLocation.getX(), firstLocation.getY());
}
//Add the new letters to the output string
logger.debug("Decoded letters {} {}", firstLetter, secondLetter);
output.append(firstLetter);
output.append(secondLetter);
}
@@ -348,6 +420,8 @@ public class Playfair{
//Makes sure all variables are empty
public void reset(){
logger.debug("Resetting fields");
grid = new char[5][5];
inputString = "";
outputString = "";
@@ -425,11 +499,15 @@ public class Playfair{
return outputString;
}
public String getGrid(){
StringBuilder gridString = new StringBuilder();
logger.debug("Creating string from grid");
StringJoiner gridString = new StringJoiner("\n");
for(char[] row : grid){
StringJoiner rowString = new StringJoiner(" ", "[", "]");
for(char col : row){
gridString.append(col);
rowString.add(Character.toString(col));
}
gridString.add(rowString.toString());
}
return gridString.toString();