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,20 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Porta.java
//Mattrixwv
// Created: 02-28-22
//Modified: 02-28-22
//Modified: 07-09-22
package com.mattrixwv.cipherstream.monosubstitution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Porta{
private static final Logger logger = LoggerFactory.getLogger(Porta.class);
private static final String[] tableau = {
"NOPQRSTUVWXYZABCDEFGHIJKLM", //A-B
"OPQRSTUVWXYZNMABCDEFGHIJKL", //C-D
@@ -26,48 +31,68 @@ public class Porta{
"ZNOPQRSTUVWXYBCDEFGHIJKLMA" //Y-Z
};
private String inputString;
private String outputString;
private String keyword;
private boolean preserveCapitals;
private boolean preserveWhitespace;
private boolean preserveSymbols;
//Fields
private String inputString; //The string that needs encoded/decoded
private String outputString; //The encoded/decoded string
private String keyword; //The keyword used to encode the input string
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
//Ensure all keyword constraints are followed
private void setKeyword(String keyword) throws InvalidKeywordException{
//Make sure the keyword isn't null
if(keyword == null){
throw new InvalidKeywordException("Keyword cannot be null");
}
logger.debug("Original keyword '{}'", keyword);
//Convert all letters to uppercase
logger.debug("Removing case");
keyword = keyword.toUpperCase();
//Remove all characters except capital letters and save the string
logger.debug("Removing all non-letters");
keyword = keyword.replaceAll("[^A-Z]", "");
//Save the keyword
this.keyword = keyword;
logger.debug("Cleaned keyword '{}'", keyword);
//If after eliminating all ususable characters the keyword is empty throw an exception
if(this.keyword.isBlank() || (this.keyword.length() < 2)){
throw new InvalidKeywordException("Keyword must contain at least 2 letters");
}
}
//Ensure all input constraints are followed
private void setInputString(String inputString) throws InvalidInputException{
//Ensure the input isn't null
if(inputString == null){
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("Removig 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
@@ -75,11 +100,14 @@ public class Porta{
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
//Returns the letter that replaces the passed in letter
private char getReplacer(int keywordCnt, char letter){
logger.debug("Getting letter that replaces {} at {}", letter, keywordCnt);
char keyLetter = keyword.charAt(keywordCnt % keyword.length());
int tableauColumn = (Character.toUpperCase(letter) - 'A');
char replacer;
char replacer;
switch(keyLetter){
case 'A', 'B' -> replacer = tableau[0].charAt(tableauColumn);
case 'C', 'D' -> replacer = tableau[1].charAt(tableauColumn);
@@ -97,32 +125,44 @@ public class Porta{
default -> replacer = letter;
}
logger.debug("Replacer {}", replacer);
return replacer;
}
//Encodes the inputString and stores the result in outputString
private void encode(){
logger.debug("Encoding");
StringBuilder output = new StringBuilder();
//Step through every character in the inputString and advance it the correct amount according to the keyword and tableau
int keywordCnt = 0;
for(char letter : inputString.toCharArray()){
logger.debug("Working character {}", letter);
//If the character is a letter replace with the corresponding character from the tableau
if(Character.isUpperCase(letter)){
logger.debug("Encoding uppercase");
letter = Character.toUpperCase(getReplacer(keywordCnt, letter));
++keywordCnt;
}
else if(Character.isLowerCase(letter)){
logger.debug("Encoding lowercase");
letter = Character.toLowerCase(getReplacer(keywordCnt, letter));
++keywordCnt;
}
//Add the current character to the output
logger.debug("Encoded letter {}", letter);
output.append(letter);
}
//Save the output
logger.debug("Saving output string '{}'", output);
outputString = output.toString();
}
//Decodes the inputString and stores the result in outputString
private void decode(){
logger.debug("Decoding");
//Decoding is the same as encoding
encode();
}
@@ -142,6 +182,7 @@ public class Porta{
reset();
}
//Sets the keyword and inputString and encodes the message
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
//Set the parameters
reset();
@@ -152,6 +193,7 @@ public class Porta{
encode();
return outputString;
}
//Sets the keyword and inputString and decodes the message
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
//Set the parameters
reset();
@@ -163,16 +205,22 @@ public class Porta{
return outputString;
}
//Returns the inputString
public String getInputString(){
return inputString;
}
//Returns the shift
public String getOutputString(){
return outputString;
}
//Returns the outputString
public String getKeyword(){
return keyword;
}
//Makes sure all of the fields are empty
public void reset(){
logger.debug("Resetting fields");
inputString = "";
outputString = "";
keyword = "";