Updating tests
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/RailFence.java
|
||||
//Mattrixwv
|
||||
// Created: 03-21-22
|
||||
//Modified: 07-09-22
|
||||
//Modified: 05-04-23
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
@@ -16,17 +16,18 @@ import com.mattrixwv.cipherstream.exceptions.InvalidBaseException;
|
||||
|
||||
public class RailFence{
|
||||
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
|
||||
private boolean preserveCapitals; //Persist capitals in the output string
|
||||
private boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
private boolean preserveSymbols; //Persist symbols in the output string
|
||||
protected String inputString; //The message that needs to be encoded/decoded
|
||||
protected String outputString; //The encoded/decoded message
|
||||
protected StringBuilder[] fence; //The fence used for encoding/decoding
|
||||
//Settings
|
||||
protected boolean preserveCapitals; //Persist capitals in the output string
|
||||
protected boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
protected boolean preserveSymbols; //Persist symbols in the output string
|
||||
|
||||
|
||||
//Strips invalid characters from the string that needs encoded/decoded
|
||||
private void setInputString(String inputString) throws InvalidInputException{
|
||||
protected void setInputString(String inputString) throws InvalidInputException{
|
||||
//Ensure the input string isn't null
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
@@ -61,7 +62,7 @@ public class RailFence{
|
||||
}
|
||||
}
|
||||
//Ensures the number of rails is valid and sets up the fence
|
||||
private void setNumRails(int numRails) throws InvalidBaseException{
|
||||
protected void setNumRails(int numRails) throws InvalidBaseException{
|
||||
if(numRails < 2){
|
||||
throw new InvalidBaseException("You must use at least 2 rails");
|
||||
}
|
||||
@@ -74,13 +75,13 @@ public class RailFence{
|
||||
}
|
||||
}
|
||||
//Strip the inputString of all non-letter characters
|
||||
private String getCleanInputString(){
|
||||
protected 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){
|
||||
protected void formatOutput(String outputString){
|
||||
logger.debug("Formatting output string");
|
||||
|
||||
StringBuilder output = new StringBuilder();
|
||||
@@ -105,11 +106,11 @@ public class RailFence{
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug("Formatted output '{}'", output);
|
||||
this.outputString = output.toString();
|
||||
logger.debug("Formatted output '{}'", this.outputString);
|
||||
}
|
||||
//Returns the decoded string found in the fence after all characters are placed correctly
|
||||
private String getDecodedStringFromFence(){
|
||||
protected String getDecodedStringFromFence(){
|
||||
logger.debug("Getting decoded string from the fence");
|
||||
|
||||
boolean down = true;
|
||||
@@ -118,7 +119,7 @@ public class RailFence{
|
||||
int insideCol = -1;
|
||||
StringBuilder output = new StringBuilder();
|
||||
while(true){
|
||||
//Get the next character based on what rail you are currently usinig
|
||||
//Get the next character based on what rail you are currently using
|
||||
if(rail == 0){
|
||||
if(outsideCol >= fence[rail].length()){
|
||||
break;
|
||||
@@ -161,13 +162,13 @@ public class RailFence{
|
||||
return output.toString();
|
||||
}
|
||||
//Encodes inputString using the RailFence cipher and stores the result in outputString
|
||||
private void encode(){
|
||||
protected void encode(){
|
||||
logger.debug("Encoding");
|
||||
|
||||
boolean up = true;
|
||||
int rail = 0;
|
||||
for(char ch : getCleanInputString().toCharArray()){
|
||||
logger.debug("Working character {}", ch);
|
||||
logger.debug("Working character '{}'", ch);
|
||||
|
||||
fence[rail].append(ch);
|
||||
//Advance to the next rail
|
||||
@@ -204,7 +205,7 @@ public class RailFence{
|
||||
formatOutput(output.toString());
|
||||
}
|
||||
//Decodes inputString using the RailFence cipher and stores the result in outputString
|
||||
private void decode(){
|
||||
protected void decode(){
|
||||
logger.debug("Decoding");
|
||||
|
||||
//Determine the number of characters on each rail
|
||||
@@ -260,11 +261,13 @@ public class RailFence{
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
reset();
|
||||
}
|
||||
public RailFence(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
reset();
|
||||
}
|
||||
|
||||
//Encodes inputString using a Rail Fence of length numRails and returns the result
|
||||
@@ -296,7 +299,8 @@ public class RailFence{
|
||||
outputString = "";
|
||||
fence = null;
|
||||
}
|
||||
//Gets
|
||||
|
||||
//Getters
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user