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,31 +1,42 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Substitution.java
//Mattrixwv
// Created: 02-22-22
//Modified: 02-22-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 Substitution{
private String inputString;
private String outputString;
private String key;
private boolean preserveCapitals;
private boolean preserveWhitespace;
private boolean preserveSymbols;
private static final Logger logger = LoggerFactory.getLogger(Substitution.class);
//Fields
private String inputString; //The string that needs encoded/decoded
private String outputString; //The encoded/decoded string
private String key; //The keyword used to encode/decode the input
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
//Ensures key constraints are followed
private void setKey(String key) throws InvalidKeywordException{
if(key == null){
throw new InvalidKeywordException("Key cannot be null");
}
logger.debug("Original key '{}'", key);
//Transform all letters to uppercase
logger.debug("Removing case");
key = key.toUpperCase();
//Make sure the key contains no duplicate mappings
logger.debug("Ensuring there are no duplicate mappings");
String tempKey = key.replaceAll("(.)\\1{2}", "");
if(!tempKey.equals(key)){
throw new InvalidKeywordException("The key cannot contain duplicate mappings");
@@ -33,6 +44,8 @@ public class Substitution{
//Make sure the key is a valid length
if(key.length() == 26){
logger.debug("Ensuring there are only letters in the key");
//Make sure the key contains all valid characters
tempKey = key.replaceAll("[^A-Z]", "");
if(!tempKey.equals(key)){
@@ -40,6 +53,8 @@ public class Substitution{
}
}
else if(key.length() == 36){
logger.debug("Ensure there are only alpha-numeric characters in the key");
//Make sure the key contains all valid characters
tempKey = key.replaceAll("[^A-Z0-9]", "");
if(!tempKey.equals(key)){
@@ -51,25 +66,36 @@ public class Substitution{
}
//Save the key
logger.debug("Cleaned key '{}'", key);
this.key = key;
}
//Ensure intput constraints are followed
private void setInputString(String inputString) throws InvalidInputException{
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
}
logger.debug("Original input string '{}'", inputString);
//Remove any data that should not be preserved
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 inputString
logger.debug("Cleaned input string '{}'", inputString);
this.inputString = inputString;
//Make sure there is still input
@@ -77,46 +103,71 @@ public class Substitution{
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
//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 convert it
for(char ch : inputString.toCharArray()){
logger.debug("Working character {}", ch);
if(Character.isUpperCase(ch)){
logger.debug("Encoding uppercase");
output.append(Character.toUpperCase(key.charAt(ch - 'A')));
}
else if(Character.isLowerCase(ch)){
logger.debug("Encoding lowercase");
output.append(Character.toLowerCase(key.charAt(ch - 'a')));
}
else if(Character.isDigit(ch) && (key.length() == 36)){
logger.debug("Encoding digit");
output.append(key.charAt('Z' - 'A' + Integer.valueOf(Character.toString(ch)) + 1));
}
else{
logger.debug("Passing symbol through");
output.append(ch);
}
}
//Save the output
logger.debug("Encoded message '{}'", output);
this.outputString = output.toString();
}
//Decodes the inputString and stores the result in outputString
private void decode(){
logger.debug("Decoding");
StringBuilder output = new StringBuilder();
//Step through every character in the inputString and convert it
for(char ch : inputString.toCharArray()){
logger.debug("Working character {}", ch);
if(Character.isUpperCase(ch)){
logger.debug("Encoding uppercase");
output.append((char)('A' + key.indexOf(Character.toUpperCase(ch))));
}
else if(Character.isLowerCase(ch)){
logger.debug("Encoding lowercase");
output.append((char)('a' + key.indexOf(Character.toUpperCase(ch))));
}
else if(Character.isDigit(ch) && (key.length() == 36)){
logger.debug("Encoding digit");
output.append((char)('0' + (key.indexOf(Character.toUpperCase(ch)) - 26)));
}
else{
logger.debug("Passing through symbol");
output.append(ch);
}
}
//Save the output
logger.debug("Encoded message '{}'", output);
this.outputString = output.toString();
}
@@ -154,6 +205,8 @@ public class Substitution{
return outputString;
}
public void reset(){
logger.debug("Resetting fields");
inputString = "";
outputString = "";
key = "";