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,17 +1,22 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/PolybiusSquare.java
//Mattrixwv
// Created: 01-04-22
//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 PolybiusSquare{
private static final Logger logger = LoggerFactory.getLogger(PolybiusSquare.class);
//A class representing the location of a character in the grid
protected class CharLocation{
private int x;
@@ -28,7 +33,7 @@ public class PolybiusSquare{
}
}
//Variables
//Fields
protected String inputString; //The message that needs to be encoded/decoded
protected String outputString; //The encoded/decoded message
protected String keyword; //The keyword used to create the grid
@@ -40,12 +45,16 @@ public class PolybiusSquare{
//Create the grid from the keyword
protected 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("Created grid\n{}", getGrid());
}
//Strips invalid characters from the string that needs encoded/decoded
protected void setInputStringEncoding(String inputString) throws InvalidCharacterException, InvalidInputException{
@@ -53,7 +62,10 @@ public class PolybiusSquare{
throw new NullPointerException("Input cannot be null");
}
logger.debug("Setting input string for encoding '{}'", inputString);
//Make sure the string doesn't contain any numbers
logger.debug("Checking for digits");
for(char ch = '0';ch <= '9';++ch){
if(inputString.contains(Character.toString(ch))){
throw new InvalidCharacterException("Inputs for encoding cannot contain numbers");
@@ -61,15 +73,20 @@ public class PolybiusSquare{
}
//Change to upper case
logger.debug("Removing case");
inputString = inputString.toUpperCase();
//Remove any whitespace if selected
if(!preserveWhitespace){
logger.debug("Removing whitespace");
inputString = inputString.replaceAll("\\s", "");
}
//Remove any symbols if selected
if(!preserveSymbols){
logger.debug("Removing symbols");
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
}
@@ -83,9 +100,11 @@ public class PolybiusSquare{
}
//Replace any characters that need replaced
inputString = inputString.replaceAll(Character.toString(replaced), Character.toString(replacer));
logger.debug("Replacing {} with {}", replaced, replacer);
inputString = inputString.replace(Character.toString(replaced), Character.toString(replacer));
//Save the string
logger.debug("Cleaned input string '{}'", inputString);
this.inputString = inputString;
if(this.inputString.isBlank() || getPreparedInputStringEncoding().isBlank()){
@@ -96,7 +115,11 @@ public class PolybiusSquare{
if(inputString == null){
throw new NullPointerException("Input cannot be null");
}
logger.debug("Setting input string for decoding '{}'", inputString);
//Make sure the string contains an even number of digits and no letters
logger.debug("Checking for letters");
int numberOfDigits = 0;
for(int cnt = 0;cnt < inputString.length();++cnt){
char ch = inputString.charAt(cnt);
@@ -113,15 +136,20 @@ public class PolybiusSquare{
//Remove any whitespace if selected
if(!preserveWhitespace){
logger.debug("Removing whitespace");
inputString = inputString.replaceAll("\\s", "");
}
//Remove any symbols if selected
if(!preserveSymbols){
logger.debug("Removing symbols");
inputString = inputString.replaceAll("[^0-9\\s]", "");
}
//Save the string
logger.debug("Cleaned input string '{}'", inputString);
this.inputString = inputString;
if(this.inputString.isBlank() || getPreparedInputStringDecoding().isBlank()){
@@ -130,93 +158,139 @@ public class PolybiusSquare{
}
//Returns the input string ready for encoding
protected String getPreparedInputStringEncoding(){
logger.debug("Preparing input string for encoding");
String cleanString = inputString.toUpperCase();
cleanString = cleanString.replaceAll("[^A-Z]", "");
logger.debug("Prepared string '{}'", cleanString);
return cleanString;
}
protected String getPreparedInputStringDecoding(){
return inputString.replaceAll("\\D", "");
logger.debug("Prepareing input string for decoding");
String cleanString = inputString.replaceAll("\\D", "");
logger.debug("Prepared string '{}'", cleanString);
return cleanString;
}
//Strips invalid characters from the keyword and creates the grid
protected void setKeyword(String keyword){
if(keyword == null){
throw new NullPointerException("Keyword cannot be null");
}
logger.debug("Original keyword {}", keyword);
//Change everything to uppercase
logger.debug("Removing case");
keyword = keyword.toUpperCase();
//Remove everything except capital letters
logger.debug("Removing all non-letters");
keyword = keyword.replaceAll("[^A-Z]", "");
//Add all letters in the alphabet to the key
logger.debug("Appending entire alphabet");
keyword += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//Replace all replaced characters
logger.debug("Replacing {} with {}", replaced, replacer);
keyword = keyword.replaceAll(Character.toString(replaced), Character.toString(replacer));
//Remove all duplicate characters
StringBuilder uniqueKey = new StringBuilder();
keyword.chars().distinct().forEach(c -> uniqueKey.append((char)c));
this.keyword = uniqueKey.toString();
logger.debug("Cleaned keyword {}", keyword);
//Create the grid from the sanitized keyword
createGrid();
}
//Returns the location of the given charcter in the grid
protected CharLocation findChar(char letter) throws InvalidInputException{
logger.debug("Finding {} 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");
}
//Adds characters that aren't letters to the output
protected void addCharactersToCleanStringEncode(String cleanString){
logger.debug("Formatting output string for encoding");
int outputCnt = 0;
StringBuilder fullOutput = new StringBuilder();
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
logger.debug("Working character {}", inputString.charAt(inputCnt));
//Add both numbers of any letters to the output
if(Character.isAlphabetic(inputString.charAt(inputCnt))){
logger.debug("Adding encoded characters");
fullOutput.append(cleanString.charAt(outputCnt++));
fullOutput.append(cleanString.charAt(outputCnt++));
}
//Add any other characters that appear to the output
else{
logger.debug("Adding symbols");
fullOutput.append(inputString.charAt(inputCnt));
}
}
logger.debug("Formatted output '{}'", fullOutput);
outputString = fullOutput.toString();
}
protected void addCharactersToCleanStringDecode(String cleanString){
logger.debug("Formatting output string to decoding");
int outputCnt = 0;
StringBuilder fullOutput = new StringBuilder();
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
logger.debug("Working character {}", inputString.charAt(inputCnt));
//Add the letter to the output and skip the second number
if(Character.isDigit(inputString.charAt(inputCnt))){
logger.debug("Adding decoded characters");
fullOutput.append(cleanString.charAt(outputCnt++));
++inputCnt;
}
//Add any other characters that appear to the output
else{
logger.debug("Adding symbols");
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();
String cleanString = getPreparedInputStringEncoding();
for(int cnt = 0;cnt < cleanString.length();++cnt){
//Get the next character to be encoded
char ch = cleanString.charAt(cnt);
logger.debug("Working character {}", ch);
//Find the letter in the grid
CharLocation location = findChar(ch);
logger.debug("Location {}, {}", location.getX() + 1, location.getY() + 1);
//Add the grid location to the output
output.append(location.getX() + 1);
@@ -231,6 +305,8 @@ public class PolybiusSquare{
}
//Decodes inputString using the Playfair cipher and stores the result in outputString
private String decode(){
logger.debug("Decoding");
StringBuilder output = new StringBuilder();
String cleanString = getPreparedInputStringDecoding();
for(int cnt = 0;cnt < cleanString.length();){
@@ -238,9 +314,13 @@ public class PolybiusSquare{
char firstDigit = cleanString.charAt(cnt++);
char secondDigit = cleanString.charAt(cnt++);
logger.debug("Digits to decode {} {}", firstDigit, secondDigit);
//Get the next character
char letter = grid[Integer.valueOf(Character.toString(firstDigit)) - 1][Integer.valueOf(Character.toString(secondDigit)) - 1];
logger.debug("Decoded letter {}", letter);
//Add the new letter to the output
output.append(letter);
}
@@ -296,6 +376,8 @@ public class PolybiusSquare{
//Makes sure all variables are empty
public void reset(){
logger.debug("Resetting fields");
grid = new char[5][5];
inputString = "";
outputString = "";
@@ -340,11 +422,15 @@ public class PolybiusSquare{
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();