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,18 +1,23 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/RailFence.java
//Mattrixwv
// Created: 03-21-22
//Modified: 03-22-22
//Modified: 07-09-22
package com.mattrixwv.cipherstream.polysubstitution;
import java.math.BigDecimal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidBaseException;
public class RailFence{
//Variables
private static final Logger logger = LoggerFactory.getLogger(RailFence.class);
//Fields
private String inputString; //The message that needs to be encoded/decoded
private String outputString; //The encoded/decoded message
private StringBuilder[] fence; //The fence used for encoding/decoding
@@ -27,18 +32,27 @@ public class RailFence{
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("Clean input string '{}'", inputString);
this.inputString = inputString;
//Ensure the string isn't blank
@@ -52,6 +66,8 @@ public class RailFence{
throw new InvalidBaseException("You must use at least 2 rails");
}
logger.debug("Creating {} rails", numRails);
fence = new StringBuilder[numRails];
for(int cnt = 0;cnt < numRails;++cnt){
fence[cnt] = new StringBuilder();
@@ -59,28 +75,43 @@ public class RailFence{
}
//Strip the inputString of all non-letter characters
private String getCleanInputString(){
logger.debug("Getting input string for encoding");
return inputString.replaceAll("[^a-zA-Z]", "");
}
//Ensures capitals, lowercase, and symbols are displayed in the output string
private void formatOutput(String outputString){
logger.debug("Formatting output string");
StringBuilder output = new StringBuilder();
int outputLoc = 0;
for(char ch : inputString.toCharArray()){
logger.debug("Working character {}", ch);
if(Character.isUpperCase(ch)){
logger.debug("Formatting uppercase");
output.append(Character.toUpperCase(outputString.charAt(outputLoc++)));
}
else if(Character.isLowerCase(ch)){
logger.debug("Formatting lowercase");
output.append(Character.toLowerCase(outputString.charAt(outputLoc++)));
}
else{
logger.debug("Inserting symbol");
output.append(ch);
}
}
logger.debug("Formatted output '{}'", output);
this.outputString = output.toString();
}
//Returns the decoded string found in the fence after all characters are placed correctly
private String getDecodedStringFromFence(){
logger.debug("Getting decoded string from the fence");
boolean down = true;
int rail = 0;
int outsideCol = 0;
@@ -126,41 +157,56 @@ public class RailFence{
}
}
logger.debug("Fence output '{}'", output);
return output.toString();
}
//Encodes inputString using the RailFence cipher and stores the result in outputString
private void encode(){
logger.debug("Encoding");
boolean up = true;
int rail = 0;
for(char ch : getCleanInputString().toCharArray()){
logger.debug("Working character {}", ch);
fence[rail].append(ch);
//Advance to the next rail
if(up){
logger.debug("Moving up");
++rail;
}
else{
logger.debug("Moving down");
--rail;
}
//Make sure you're still in bounds
if(rail == fence.length){
logger.debug("Swapping to down");
up = false;
rail -= 2;
}
else if(rail == -1){
logger.debug("Swapping to up");
up = true;
rail += 2;
}
}
//Append the fence rows to come up with a single string
logger.debug("Appending rows from the fence");
StringBuilder output = new StringBuilder();
for(StringBuilder segment : fence){
output.append(segment);
}
//Format the output
formatOutput(output.toString());
}
//Decodes inputString using the RailFence cipher and stores the result in outputString
private void decode(){
logger.debug("Decoding");
//Determine the number of characters on each rail
String cleanInputString = getCleanInputString();
int cycleLength = 2 * (fence.length - 1);
@@ -182,7 +228,12 @@ public class RailFence{
middleNum = (cycleLength - (k.remainder(BigDecimal.ONE).multiply(new BigDecimal(cycleLength))).intValue());
}
logger.debug("Number of characters in the top rail {}", numInTopRail);
logger.debug("Number of characters in the middle rails {}", numInMiddleRails);
logger.debug("Number of characters in the bottom rail {}", numInBottomRail);
//Add the correct number of characters to each rail
logger.debug("Adding characters to the rails");
fence[0].append(cleanInputString.substring(0, numInTopRail));
int start = numInTopRail;
int end = numInTopRail + numInMiddleRails;
@@ -195,10 +246,12 @@ public class RailFence{
end += numInMiddleRails;
}
end = start + numInBottomRail;
logger.debug("Appending the bottom rail");
fence[fence.length - 1].append(cleanInputString.substring(start, end));
//Get the decoded string from the constructed fence
String output = getDecodedStringFromFence();
logger.debug("Fence output '{}'", output);
formatOutput(output);
}
@@ -237,6 +290,8 @@ public class RailFence{
//Makes sure all variables are empty
public void reset(){
logger.debug("Resetting fields");
inputString = "";
outputString = "";
fence = null;