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,14 +1,22 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Bifid.java
//Mattrixwv
// Created: 03-03-22
//Modified: 03-03-22
//Modified: 07-09-22
package com.mattrixwv.cipherstream.polysubstitution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Bifid{
private static final Logger logger = LoggerFactory.getLogger(Bifid.class);
//Fields
private String inputString; //The message that needs to be encoded/decoded
private String outputString; //The encoded/decoded message
private String keyword; //The keyword used to create the grid
@@ -17,6 +25,7 @@ public class Bifid{
private boolean preserveWhitespace; //Persist whitespace in the output string
private boolean preserveSymbols; //Persist symbols in the output string
//Strips invalid characters from the keyword and creates the grid
private void setKeyword(String keyword) throws InvalidKeywordException{
//Ensure the keyword isn't null
@@ -24,6 +33,8 @@ public class Bifid{
throw new InvalidKeywordException("Keyword cannot be null");
}
logger.debug("Setting keyword '{}'", keyword);
//Save the key for polybius to deal with
this.keyword = keyword;
}
@@ -34,18 +45,27 @@ public class Bifid{
throw new InvalidInputException("Input cannot be null");
}
logger.debug("Original input string '{}'", inputString);
//Apply removal 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]", "");
}
//Save the string
logger.debug("Cleaned input string '{}'", inputString);
this.inputString = inputString;
//Ensure the string isn't blank
@@ -55,47 +75,65 @@ public class Bifid{
}
//Adds all non-letter characters back to the output string
private void formatOutput(String outputString){
logger.debug("Formatting output");
//Keep track of where you are in the output
int outputCnt = 0;
StringBuilder output = new StringBuilder();
//Check every character in the input and apply the correct rules to the output
for(char ch : inputString.toCharArray()){
logger.debug("Current character {}", ch);
if(Character.isUpperCase(ch)){
logger.debug("Altering uppercase");
output.append(Character.toUpperCase(outputString.charAt(outputCnt++)));
}
else if(Character.isLowerCase(ch)){
logger.debug("Altering lowercase");
output.append(Character.toLowerCase(outputString.charAt(outputCnt++)));
}
else{
logger.debug("Adding symbol");
output.append(ch);
}
}
//Save the output
logger.debug("Formatted output string '{}'", output);
this.outputString = output.toString();
}
//Encodes inputString using a polybius square and stores the result in outputString
private void encode() throws InvalidCharacterException, InvalidInputException{
logger.debug("Encoding");
//Get the encoded numbers from a polybius square
String numberResult = polybiusSquare.encode(keyword, inputString).replaceAll("\\s", "");
logger.debug("Encoding polybius");
String polybiusMessage = polybiusSquare.encode(keyword, inputString).replaceAll("\\s", "");
keyword = polybiusSquare.getKeyword(); //Save the cleaned keyword
//Split the numbers into 2 rows and rejoin the rows to create a new string
StringBuilder row0 = new StringBuilder();
StringBuilder row1 = new StringBuilder();
boolean firstNum = true;
for(char ch : numberResult.toCharArray()){
logger.debug("Splitting Polybius Square message");
for(char ch : polybiusMessage.toCharArray()){
logger.debug("Current character {}", ch);
if(firstNum){
row0.append(ch);
}
else{
row1.append(ch);
}
firstNum = !firstNum;
}
String shuffledResult = row0.toString() + row1.toString();
//Take the new string and decode the numbers using polybius
logger.debug("Decoding Polybius Square");
String letterResult = polybiusSquare.decode(keyword, shuffledResult);
//Format the output
@@ -103,7 +141,10 @@ public class Bifid{
}
//Decodes inputString using a polybius square and stores the result in outputString
private void decode() throws InvalidCharacterException, InvalidInputException{
logger.debug("Decoding");
//Get the decoded number from a polybius square
logger.debug("Encoding Polybius Square");
String numberResult = polybiusSquare.encode(keyword, inputString).replaceAll("\\s", "");
keyword = polybiusSquare.getKeyword();
@@ -111,12 +152,16 @@ public class Bifid{
String row0 = numberResult.substring(0, numberResult.length() / 2);
String row1 = numberResult.substring(numberResult.length() / 2);
StringBuilder unshuffledResult = new StringBuilder();
logger.debug("Splitting Polybius Square message");
for(int cnt = 0;cnt < row0.length();++cnt){
logger.debug("Current characters {} {}", row0.charAt(cnt), row1.charAt(cnt));
unshuffledResult.append(row0.charAt(cnt));
unshuffledResult.append(row1.charAt(cnt));
}
//Take the new string and decode the numbers using polybius
logger.debug("Decoding Polybius Square");
String letterResult = polybiusSquare.decode(keyword, unshuffledResult.toString());
//Format the outputString
@@ -162,12 +207,6 @@ public class Bifid{
return outputString;
}
//Makes sure all variables are empty
public void reset(){
inputString = "";
outputString = "";
keyword = "";
}
//Gets
public String getInputString(){
return inputString;
@@ -178,4 +217,12 @@ public class Bifid{
public String getKeyword(){
return keyword;
}
//Makes sure all variables are empty
public void reset(){
logger.debug("Resetting fields");
inputString = "";
outputString = "";
keyword = "";
}
}