Added logging
This commit is contained in:
@@ -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 = "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//MattrixwvWebsite/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Columnar.java
|
||||
//Mattrixwv
|
||||
// Created: 01-16-22
|
||||
//Modified: 03-03-22
|
||||
//Modified: 07-09-22
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
@@ -9,12 +9,18 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
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 Columnar{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Columnar.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
|
||||
@@ -28,10 +34,13 @@ public class Columnar{
|
||||
|
||||
//Strip the inputString of all non-letter characters and change them to capitals
|
||||
private String getCleanInputString(){
|
||||
logger.debug("Cleaning input string");
|
||||
return inputString.toUpperCase().replaceAll("[^A-Z]", "");
|
||||
}
|
||||
//Create the grid from the keyword
|
||||
private void createGridEncode(){
|
||||
logger.debug("Creating grid for encoding");
|
||||
|
||||
//Add the keyword to the first row in the array
|
||||
grid = new ArrayList<>();
|
||||
grid.add(new ArrayList<>(Arrays.asList(keyword.chars().mapToObj(c -> (char)c).toArray(Character[]::new))));
|
||||
@@ -49,6 +58,8 @@ public class Columnar{
|
||||
}
|
||||
}
|
||||
private void createGridDecode(){
|
||||
logger.debug("Creating grid for decoding");
|
||||
|
||||
//Add the keyword to the first row in the array
|
||||
grid = new ArrayList<>();
|
||||
StringBuilder orderedKeyword = new StringBuilder();
|
||||
@@ -76,19 +87,29 @@ public class Columnar{
|
||||
}
|
||||
//Strips invalid characters from the string that needs encoded/decoded
|
||||
private void setInputStringEncode(String inputString) throws InvalidInputException{
|
||||
logger.debug("Setting input string for encoding");
|
||||
|
||||
//Ensure the input isn't null
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input must not 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("Remoing symbols");
|
||||
|
||||
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
|
||||
}
|
||||
|
||||
@@ -100,6 +121,8 @@ public class Columnar{
|
||||
int charsToAdd = (cleanLength % keyword.length());
|
||||
if(charsToAdd != 0){
|
||||
charsToAdd = keyword.length() - charsToAdd;
|
||||
|
||||
logger.debug("Appending {} characters", charsToAdd);
|
||||
}
|
||||
for(int cnt = 0;cnt < charsToAdd;++cnt){
|
||||
inputStringBuilder.append(characterToAdd);
|
||||
@@ -108,6 +131,7 @@ public class Columnar{
|
||||
inputString = inputStringBuilder.toString();
|
||||
|
||||
//Save the string
|
||||
logger.debug("Cleaned input string '{}'", inputString);
|
||||
this.inputString = inputString;
|
||||
|
||||
//Ensure the string isn't blank
|
||||
@@ -116,19 +140,29 @@ public class Columnar{
|
||||
}
|
||||
}
|
||||
private void setInputStringDecode(String inputString) throws InvalidInputException{
|
||||
logger.debug("Setting input string for decoding");
|
||||
|
||||
//Ensure the input isn't null
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input must not 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]", "");
|
||||
}
|
||||
|
||||
@@ -182,6 +216,7 @@ public class Columnar{
|
||||
}
|
||||
|
||||
//Save the input
|
||||
logger.debug("Cleaned input string '{}'", inputString);
|
||||
this.inputString = inputString;
|
||||
|
||||
//Ensure the string isn't blank
|
||||
@@ -191,7 +226,10 @@ public class Columnar{
|
||||
}
|
||||
//Creates the output string from the grid
|
||||
private void createOutputStringFromColumns(){
|
||||
logger.debug("Creating output string for encoding");
|
||||
|
||||
//Get the current rows of any characters that you added
|
||||
logger.debug("Getting added characters");
|
||||
ArrayList<Integer> colsAddedTo = new ArrayList<>();
|
||||
if(removePadding){
|
||||
ArrayList<Integer> cols = getKeywordOriginalLocations();
|
||||
@@ -205,6 +243,7 @@ public class Columnar{
|
||||
}
|
||||
|
||||
//Turn the grid into a string
|
||||
logger.debug("Turning grid into string");
|
||||
StringBuilder gridOutput = new StringBuilder();
|
||||
for(int col = 0;col < grid.get(0).size();++col){
|
||||
for(int row = 1;row < grid.size();++row){
|
||||
@@ -216,6 +255,7 @@ public class Columnar{
|
||||
}
|
||||
|
||||
//Preserve any remaining symbols in the string
|
||||
logger.debug("Formatting output string");
|
||||
StringBuilder output = new StringBuilder();
|
||||
for(int outputLoc = 0, inputLoc = 0;inputLoc < (inputString.length() - charsAdded);){
|
||||
char inputChar = inputString.charAt(inputLoc++);
|
||||
@@ -231,10 +271,14 @@ public class Columnar{
|
||||
}
|
||||
|
||||
//Save and return the output
|
||||
logger.debug("Output string '{}'", output);
|
||||
outputString = output.toString();
|
||||
}
|
||||
private void createOutputStringFromRows(){
|
||||
logger.debug("Creating output string for decoding");
|
||||
|
||||
//Turn the grid into a string
|
||||
logger.debug("Transforming grid to a string");
|
||||
StringBuilder gridOutput = new StringBuilder();
|
||||
for(int row = 1;row < grid.size();++row){
|
||||
for(int col = 0;col < grid.get(row).size();++col){
|
||||
@@ -242,6 +286,7 @@ public class Columnar{
|
||||
}
|
||||
}
|
||||
//Remove any added characters
|
||||
logger.debug("Removing padding");
|
||||
if(removePadding){
|
||||
for(int cnt = 0;cnt < charsAdded;++cnt){
|
||||
gridOutput.deleteCharAt(gridOutput.length() - 1);
|
||||
@@ -260,6 +305,7 @@ public class Columnar{
|
||||
}
|
||||
}
|
||||
//Preserve any remaining symbols in the string
|
||||
logger.debug("Formatting output string");
|
||||
StringBuilder output = new StringBuilder();
|
||||
for(int outputLoc = 0, inputLoc = 0, row = 1, col = 0;inputLoc < inputString.length();){
|
||||
char inputChar = inputString.charAt(inputLoc++);
|
||||
@@ -288,6 +334,7 @@ public class Columnar{
|
||||
}
|
||||
|
||||
//Save and return the output
|
||||
logger.debug("Decoded output string '{}'", output);
|
||||
outputString = output.toString();
|
||||
}
|
||||
//Strips invalid characters from the keyword and creates the grid
|
||||
@@ -297,9 +344,13 @@ public class Columnar{
|
||||
throw new NullPointerException("Keyword cannot be null");
|
||||
}
|
||||
|
||||
logger.debug("Original keyword {}", keyword);
|
||||
|
||||
//Strip all non-letter characters and change them to uppercase
|
||||
this.keyword = keyword.toUpperCase().replaceAll("[^A-Z]", "");
|
||||
|
||||
logger.debug("Cleaned keyword {}", keyword);
|
||||
|
||||
//Make sure a valid keyword is present
|
||||
if(this.keyword.length() < 2){
|
||||
throw new InvalidKeywordException("The keyword must contain at least 2 letters");
|
||||
@@ -311,17 +362,23 @@ public class Columnar{
|
||||
throw new InvalidCharacterException("Character to add must be a letter");
|
||||
}
|
||||
|
||||
logger.debug("Setting character to add");
|
||||
|
||||
if(!preserveCapitals){
|
||||
this.characterToAdd = Character.toUpperCase(characterToAdd);
|
||||
}
|
||||
else{
|
||||
this.characterToAdd = characterToAdd;
|
||||
}
|
||||
|
||||
logger.debug("Character to add for padding {}", characterToAdd);
|
||||
}
|
||||
//Returns a list of integers that represents the location of the characters of the keyword in alphabetic order
|
||||
private ArrayList<Integer> getKeywordAlphaLocations(){
|
||||
logger.debug("Creating an array of keyword letter locations");
|
||||
|
||||
ArrayList<Integer> orderedLocations = new ArrayList<>();
|
||||
//go through every letter and check it against the keyword
|
||||
//Go through every letter and check it against the keyword
|
||||
for(char ch = 'A';ch <= 'Z';++ch){
|
||||
for(int cnt = 0;cnt < keyword.length();++cnt){
|
||||
//If the current letter is the same as the letter we are looking for add the location to the array
|
||||
@@ -332,9 +389,12 @@ public class Columnar{
|
||||
}
|
||||
|
||||
//Return the alphabetic locations
|
||||
logger.debug("Array of keyword letters {}", orderedLocations);
|
||||
return orderedLocations;
|
||||
}
|
||||
private ArrayList<Integer> getKeywordOriginalLocations(){
|
||||
logger.debug("Creating array of original keyword locations");
|
||||
|
||||
//Figure out the order the columns are in
|
||||
ArrayList<Integer> orderedLocations = getKeywordAlphaLocations();
|
||||
//Figure out what order the columns need rearanged to
|
||||
@@ -347,10 +407,15 @@ public class Columnar{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Returning the locations
|
||||
logger.debug("Array of keyword letters {}", orderedLocations);
|
||||
return originalOrder;
|
||||
}
|
||||
//Rearanges the grid based on the list of numbers given
|
||||
private void rearangeGrid(ArrayList<Integer> listOrder){
|
||||
logger.debug("Rearanging grid");
|
||||
|
||||
//Create a new grid and make sure it is the same size as the original grid
|
||||
int numCol = grid.get(0).size();
|
||||
ArrayList<ArrayList<Character>> newGrid = new ArrayList<>(grid.size());
|
||||
@@ -366,10 +431,13 @@ public class Columnar{
|
||||
}
|
||||
|
||||
//Save the new grid
|
||||
logger.debug("New grid {}", newGrid);
|
||||
grid = newGrid;
|
||||
}
|
||||
//Encodes inputString using the Columnar cipher and stores the result in outputString
|
||||
private void encode(){
|
||||
logger.debug("Encoding");
|
||||
|
||||
//Create the grid
|
||||
createGridEncode();
|
||||
//Figure out the new column order
|
||||
@@ -381,6 +449,8 @@ public class Columnar{
|
||||
}
|
||||
//Decodes inputString using the Columnar cipher and stores the result in outputString
|
||||
private void decode(){
|
||||
logger.debug("Decoding");
|
||||
|
||||
//Create the grid
|
||||
createGridDecode();
|
||||
ArrayList<Integer> originalOrder = getKeywordOriginalLocations();
|
||||
@@ -440,6 +510,8 @@ public class Columnar{
|
||||
|
||||
//Makes sure all variables are empty
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
keyword = "";
|
||||
|
||||
@@ -5,11 +5,11 @@
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeyException;
|
||||
import com.mattrixwv.matrix.ModMatrix;
|
||||
import com.mattrixwv.matrix.exceptions.InvalidGeometryException;
|
||||
import com.mattrixwv.matrix.exceptions.InvalidScalarException;
|
||||
|
||||
@@ -1,15 +1,22 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Playfair.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-30-21
|
||||
//Modified: 02-17-22
|
||||
//Modified: 07-09-22
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
|
||||
|
||||
public class Playfair{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Playfair.class);
|
||||
|
||||
//A class representing the location of a character in the grid
|
||||
private class CharLocation{
|
||||
private int x;
|
||||
@@ -26,7 +33,7 @@ public class Playfair{
|
||||
}
|
||||
}
|
||||
|
||||
//Variables
|
||||
//Fields
|
||||
private boolean preserveCapitals; //Whether to respect captials 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
|
||||
@@ -39,12 +46,16 @@ public class Playfair{
|
||||
private char[][] grid; //The grid used to encode/decode the message
|
||||
//Create the grid from the keyword
|
||||
private void createGrid(){
|
||||
logger.debug("Creating grid from keyword");
|
||||
|
||||
for(int row = 0;row < 5;++row){
|
||||
for(int col = 0;col < 5;++col){
|
||||
char letter = keyword.charAt((5 * row) + col);
|
||||
grid[row][col] = letter;
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug("Grid\n{}", getGrid());
|
||||
}
|
||||
//Strips invalid characters from the string that needs encoded/decoded
|
||||
private void setInputString(String inputString, boolean encoding) throws InvalidCharacterException, InvalidInputException{
|
||||
@@ -53,26 +64,35 @@ public class Playfair{
|
||||
throw new NullPointerException("The input string cannot be null");
|
||||
}
|
||||
|
||||
logger.debug("Original input string {}", inputString);
|
||||
|
||||
//Set the 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]", "");
|
||||
}
|
||||
|
||||
//Make replace all of the replacers with replaced
|
||||
inputString = inputString.replaceAll(Character.toString(replaced), Character.toString(replacer));
|
||||
logger.debug("Replacing all {} with {}", replaced, replacer);
|
||||
inputString = inputString.replace(Character.toString(replaced), Character.toString(replacer));
|
||||
|
||||
//If there is nothing in the input string throw an exception
|
||||
if(inputString.isBlank()){
|
||||
throw new InvalidCharacterException("The input string cannot be blank");
|
||||
}
|
||||
|
||||
//If this is encoding parse it and clean up an problems
|
||||
//If this is encoding parse it and clean up any problems
|
||||
if(encoding){
|
||||
setEncodingInputString(inputString);
|
||||
}
|
||||
@@ -83,6 +103,7 @@ public class Playfair{
|
||||
throw new InvalidCharacterException("An encoded message cannot contain a letter that needs replaced");
|
||||
}
|
||||
|
||||
logger.debug("Clean input string '{}'", inputString);
|
||||
this.inputString = inputString;
|
||||
}
|
||||
|
||||
@@ -92,7 +113,7 @@ public class Playfair{
|
||||
}
|
||||
private void setEncodingInputString(String inputString){
|
||||
//Replace characters that need replaced
|
||||
inputString = inputString.replaceAll(Character.toString(replaced), Character.toString(replacer));
|
||||
inputString = inputString.replace(Character.toString(replaced), Character.toString(replacer));
|
||||
|
||||
//Check if there are any doubled characters
|
||||
StringBuilder cleanInput = new StringBuilder();
|
||||
@@ -152,12 +173,18 @@ public class Playfair{
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug("Cleaned input string '{}'", cleanInput);
|
||||
this.inputString = cleanInput.toString();
|
||||
}
|
||||
//Returns the input string ready for encoding
|
||||
private String getPreparedInputString(){
|
||||
logger.debug("Getting input string ready for encoding");
|
||||
|
||||
String cleanString = inputString.toUpperCase();
|
||||
cleanString = cleanString.replaceAll("[^A-Z]", "");
|
||||
|
||||
logger.debug("Prepared string '{}'", cleanString);
|
||||
|
||||
return cleanString;
|
||||
}
|
||||
//Strips invalid characters from the keyword and creates the grid
|
||||
@@ -166,19 +193,26 @@ public class Playfair{
|
||||
throw new NullPointerException("Keyword cannot be null");
|
||||
}
|
||||
|
||||
logger.debug("Original keyword {}", keyword);
|
||||
|
||||
//Change everything to uppercase
|
||||
logger.debug("Removing case");
|
||||
keyword = keyword.toUpperCase();
|
||||
|
||||
//Removing everything except capital letters
|
||||
logger.debug("Removing all non-letter characters");
|
||||
keyword = keyword.replaceAll("[^A-Z]", "");
|
||||
|
||||
//Add all letters in the alphabet to the key
|
||||
logger.debug("Appending the alphabet to the keyword");
|
||||
keyword += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
//Replace all replaced characters
|
||||
logger.debug("Replacing {} with {}", replaced, replacer);
|
||||
keyword = keyword.replaceAll(Character.toString(replaced), Character.toString(replacer));
|
||||
|
||||
//Remove all duplicate chatacters
|
||||
logger.debug("Removing duplicated characters");
|
||||
StringBuilder uniqueKey = new StringBuilder();
|
||||
keyword.chars().distinct().forEach(c -> uniqueKey.append((char)c));
|
||||
this.keyword = uniqueKey.toString();
|
||||
@@ -188,45 +222,69 @@ public class Playfair{
|
||||
}
|
||||
//Returns the location of the given character in the grid
|
||||
private CharLocation findChar(char letter) throws InvalidInputException{
|
||||
logger.debug("Finding character in grid {}", letter);
|
||||
|
||||
for(int row = 0;row < grid.length;++row){
|
||||
for(int col = 0;col < grid[row].length;++col){
|
||||
if(grid[row][col] == letter){
|
||||
logger.debug("Found at {}, {}", row, col);
|
||||
|
||||
return new CharLocation(row, col);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//If it was not found something went wrong
|
||||
throw new InvalidInputException("The character '" + letter + "' was not found in the grid");
|
||||
}
|
||||
//Returns the location in the grid of x and y, adjusting for out of bounds
|
||||
private char getGridChar(int x, int y){
|
||||
logger.debug("Getting character from grid[{}][{}]", x, y);
|
||||
|
||||
if(x < 0){
|
||||
x += 5;
|
||||
}
|
||||
if(y < 0){
|
||||
y += 5;
|
||||
}
|
||||
return grid[x % 5][y % 5];
|
||||
|
||||
char letter = grid[x % 5][y % 5];
|
||||
logger.debug("Character {}", letter);
|
||||
return letter;
|
||||
}
|
||||
//Adds characters that aren't letters to the output
|
||||
private void addCharactersToCleanString(String cleanString){
|
||||
logger.debug("Formatting output string");
|
||||
|
||||
int outputCnt = 0;
|
||||
StringBuilder fullOutput = new StringBuilder();
|
||||
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
|
||||
logger.debug("Working character {}", inputString.charAt(inputCnt));
|
||||
|
||||
if(Character.isUpperCase(inputString.charAt(inputCnt))){
|
||||
logger.debug("Adjusting uppercase");
|
||||
|
||||
fullOutput.append(cleanString.charAt(outputCnt++));
|
||||
}
|
||||
else if(Character.isLowerCase(inputString.charAt(inputCnt))){
|
||||
logger.debug("Adjusting lowercase");
|
||||
|
||||
fullOutput.append(Character.toLowerCase(cleanString.charAt(outputCnt++)));
|
||||
}
|
||||
else{
|
||||
logger.debug("Inserting symbol");
|
||||
|
||||
fullOutput.append(inputString.charAt(inputCnt));
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug("Formatted output '{}'", fullOutput);
|
||||
outputString = fullOutput.toString();
|
||||
}
|
||||
//Encodes inputString using the Playfair cipher and stores the result in outputString
|
||||
private String encode() throws InvalidInputException{
|
||||
logger.debug("Encoding");
|
||||
|
||||
StringBuilder output = new StringBuilder();
|
||||
int inputCnt = 0;
|
||||
String cleanString = getPreparedInputString();
|
||||
@@ -235,25 +293,31 @@ public class Playfair{
|
||||
char firstLetter = cleanString.charAt(inputCnt++);
|
||||
char secondLetter = cleanString.charAt(inputCnt++);
|
||||
|
||||
logger.debug("Letters {} {}", firstLetter, secondLetter);
|
||||
|
||||
//Find the letters in the grid
|
||||
CharLocation firstLocation = findChar(firstLetter);
|
||||
CharLocation secondLocation = findChar(secondLetter);
|
||||
|
||||
//Encode the letters
|
||||
if(firstLocation.getX() == secondLocation.getX()){
|
||||
logger.debug("Row encoding");
|
||||
firstLetter = getGridChar(firstLocation.getX(), firstLocation.getY() + 1);
|
||||
secondLetter = getGridChar(secondLocation.getX(), secondLocation.getY() + 1);
|
||||
}
|
||||
else if(firstLocation.getY() == secondLocation.getY()){
|
||||
logger.debug("Column encoding");
|
||||
firstLetter = getGridChar(firstLocation.getX() + 1, firstLocation.getY());
|
||||
secondLetter = getGridChar(secondLocation.getX() + 1, secondLocation.getY());
|
||||
}
|
||||
else{
|
||||
logger.debug("Corner encoding");
|
||||
firstLetter = getGridChar(firstLocation.getX(), secondLocation.getY());
|
||||
secondLetter = getGridChar(secondLocation.getX(), firstLocation.getY());
|
||||
}
|
||||
|
||||
//Add the new letters to the output string
|
||||
logger.debug("Encoded letters {} {}", firstLetter, secondLetter);
|
||||
output.append(firstLetter);
|
||||
output.append(secondLetter);
|
||||
}
|
||||
@@ -266,6 +330,8 @@ public class Playfair{
|
||||
}
|
||||
//Decodes inputString using the Playfair cipher and stores the result in outputString
|
||||
private String decode() throws InvalidInputException{
|
||||
logger.debug("Decoding");
|
||||
|
||||
StringBuilder output = new StringBuilder();
|
||||
int inputCnt = 0;
|
||||
String cleanString = getPreparedInputString();
|
||||
@@ -274,25 +340,31 @@ public class Playfair{
|
||||
char firstLetter = cleanString.charAt(inputCnt++);
|
||||
char secondLetter = cleanString.charAt(inputCnt++);
|
||||
|
||||
logger.debug("Letters {} {}", firstLetter, secondLetter);
|
||||
|
||||
//Find the letters in the grid
|
||||
CharLocation firstLocation = findChar(firstLetter);
|
||||
CharLocation secondLocation = findChar(secondLetter);
|
||||
|
||||
//Decode the letters
|
||||
if(firstLocation.getX() == secondLocation.getX()){
|
||||
logger.debug("Decoding row");
|
||||
firstLetter = getGridChar(firstLocation.getX(), firstLocation.getY() - 1);
|
||||
secondLetter = getGridChar(secondLocation.getX(), secondLocation.getY() - 1);
|
||||
}
|
||||
else if(firstLocation.getY() == secondLocation.getY()){
|
||||
logger.debug("Decoding col");
|
||||
firstLetter = getGridChar(firstLocation.getX() - 1, firstLocation.getY());
|
||||
secondLetter = getGridChar(secondLocation.getX() - 1, secondLocation.getY());
|
||||
}
|
||||
else{
|
||||
logger.debug("Decoding corners");
|
||||
firstLetter = getGridChar(firstLocation.getX(), secondLocation.getY());
|
||||
secondLetter = getGridChar(secondLocation.getX(), firstLocation.getY());
|
||||
}
|
||||
|
||||
//Add the new letters to the output string
|
||||
logger.debug("Decoded letters {} {}", firstLetter, secondLetter);
|
||||
output.append(firstLetter);
|
||||
output.append(secondLetter);
|
||||
}
|
||||
@@ -348,6 +420,8 @@ public class Playfair{
|
||||
|
||||
//Makes sure all variables are empty
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
grid = new char[5][5];
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
@@ -425,11 +499,15 @@ public class Playfair{
|
||||
return outputString;
|
||||
}
|
||||
public String getGrid(){
|
||||
StringBuilder gridString = new StringBuilder();
|
||||
logger.debug("Creating string from grid");
|
||||
|
||||
StringJoiner gridString = new StringJoiner("\n");
|
||||
for(char[] row : grid){
|
||||
StringJoiner rowString = new StringJoiner(" ", "[", "]");
|
||||
for(char col : row){
|
||||
gridString.append(col);
|
||||
rowString.add(Character.toString(col));
|
||||
}
|
||||
gridString.add(rowString.toString());
|
||||
}
|
||||
|
||||
return gridString.toString();
|
||||
|
||||
@@ -1,17 +1,22 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/PolybiusSquare.java
|
||||
//Mattrixwv
|
||||
// Created: 01-04-22
|
||||
//Modified: 02-17-22
|
||||
//Modified: 07-09-22
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
|
||||
|
||||
public class PolybiusSquare{
|
||||
private static final Logger logger = LoggerFactory.getLogger(PolybiusSquare.class);
|
||||
|
||||
//A class representing the location of a character in the grid
|
||||
protected class CharLocation{
|
||||
private int x;
|
||||
@@ -28,7 +33,7 @@ public class PolybiusSquare{
|
||||
}
|
||||
}
|
||||
|
||||
//Variables
|
||||
//Fields
|
||||
protected String inputString; //The message that needs to be encoded/decoded
|
||||
protected String outputString; //The encoded/decoded message
|
||||
protected String keyword; //The keyword used to create the grid
|
||||
@@ -40,12 +45,16 @@ public class PolybiusSquare{
|
||||
|
||||
//Create the grid from the keyword
|
||||
protected void createGrid(){
|
||||
logger.debug("Creating grid from keyword");
|
||||
|
||||
for(int row = 0;row < 5;++row){
|
||||
for(int col = 0;col < 5;++col){
|
||||
char letter = keyword.charAt((5 * row) + col);
|
||||
grid[row][col] = letter;
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug("Created grid\n{}", getGrid());
|
||||
}
|
||||
//Strips invalid characters from the string that needs encoded/decoded
|
||||
protected void setInputStringEncoding(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
@@ -53,7 +62,10 @@ public class PolybiusSquare{
|
||||
throw new NullPointerException("Input cannot be null");
|
||||
}
|
||||
|
||||
logger.debug("Setting input string for encoding '{}'", inputString);
|
||||
|
||||
//Make sure the string doesn't contain any numbers
|
||||
logger.debug("Checking for digits");
|
||||
for(char ch = '0';ch <= '9';++ch){
|
||||
if(inputString.contains(Character.toString(ch))){
|
||||
throw new InvalidCharacterException("Inputs for encoding cannot contain numbers");
|
||||
@@ -61,15 +73,20 @@ public class PolybiusSquare{
|
||||
}
|
||||
|
||||
//Change to upper case
|
||||
logger.debug("Removing case");
|
||||
inputString = inputString.toUpperCase();
|
||||
|
||||
//Remove any whitespace if selected
|
||||
if(!preserveWhitespace){
|
||||
logger.debug("Removing whitespace");
|
||||
|
||||
inputString = inputString.replaceAll("\\s", "");
|
||||
}
|
||||
|
||||
//Remove any symbols if selected
|
||||
if(!preserveSymbols){
|
||||
logger.debug("Removing symbols");
|
||||
|
||||
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
|
||||
}
|
||||
|
||||
@@ -83,9 +100,11 @@ public class PolybiusSquare{
|
||||
}
|
||||
|
||||
//Replace any characters that need replaced
|
||||
inputString = inputString.replaceAll(Character.toString(replaced), Character.toString(replacer));
|
||||
logger.debug("Replacing {} with {}", replaced, replacer);
|
||||
inputString = inputString.replace(Character.toString(replaced), Character.toString(replacer));
|
||||
|
||||
//Save the string
|
||||
logger.debug("Cleaned input string '{}'", inputString);
|
||||
this.inputString = inputString;
|
||||
|
||||
if(this.inputString.isBlank() || getPreparedInputStringEncoding().isBlank()){
|
||||
@@ -96,7 +115,11 @@ public class PolybiusSquare{
|
||||
if(inputString == null){
|
||||
throw new NullPointerException("Input cannot be null");
|
||||
}
|
||||
|
||||
logger.debug("Setting input string for decoding '{}'", inputString);
|
||||
|
||||
//Make sure the string contains an even number of digits and no letters
|
||||
logger.debug("Checking for letters");
|
||||
int numberOfDigits = 0;
|
||||
for(int cnt = 0;cnt < inputString.length();++cnt){
|
||||
char ch = inputString.charAt(cnt);
|
||||
@@ -113,15 +136,20 @@ public class PolybiusSquare{
|
||||
|
||||
//Remove any whitespace if selected
|
||||
if(!preserveWhitespace){
|
||||
logger.debug("Removing whitespace");
|
||||
|
||||
inputString = inputString.replaceAll("\\s", "");
|
||||
}
|
||||
|
||||
//Remove any symbols if selected
|
||||
if(!preserveSymbols){
|
||||
logger.debug("Removing symbols");
|
||||
|
||||
inputString = inputString.replaceAll("[^0-9\\s]", "");
|
||||
}
|
||||
|
||||
//Save the string
|
||||
logger.debug("Cleaned input string '{}'", inputString);
|
||||
this.inputString = inputString;
|
||||
|
||||
if(this.inputString.isBlank() || getPreparedInputStringDecoding().isBlank()){
|
||||
@@ -130,93 +158,139 @@ public class PolybiusSquare{
|
||||
}
|
||||
//Returns the input string ready for encoding
|
||||
protected String getPreparedInputStringEncoding(){
|
||||
logger.debug("Preparing input string for encoding");
|
||||
|
||||
String cleanString = inputString.toUpperCase();
|
||||
cleanString = cleanString.replaceAll("[^A-Z]", "");
|
||||
|
||||
logger.debug("Prepared string '{}'", cleanString);
|
||||
return cleanString;
|
||||
}
|
||||
protected String getPreparedInputStringDecoding(){
|
||||
return inputString.replaceAll("\\D", "");
|
||||
logger.debug("Prepareing input string for decoding");
|
||||
|
||||
String cleanString = inputString.replaceAll("\\D", "");
|
||||
|
||||
logger.debug("Prepared string '{}'", cleanString);
|
||||
return cleanString;
|
||||
}
|
||||
//Strips invalid characters from the keyword and creates the grid
|
||||
protected void setKeyword(String keyword){
|
||||
if(keyword == null){
|
||||
throw new NullPointerException("Keyword cannot be null");
|
||||
}
|
||||
|
||||
logger.debug("Original keyword {}", keyword);
|
||||
|
||||
//Change everything to uppercase
|
||||
logger.debug("Removing case");
|
||||
keyword = keyword.toUpperCase();
|
||||
|
||||
//Remove everything except capital letters
|
||||
logger.debug("Removing all non-letters");
|
||||
keyword = keyword.replaceAll("[^A-Z]", "");
|
||||
|
||||
//Add all letters in the alphabet to the key
|
||||
logger.debug("Appending entire alphabet");
|
||||
keyword += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
//Replace all replaced characters
|
||||
logger.debug("Replacing {} with {}", replaced, replacer);
|
||||
keyword = keyword.replaceAll(Character.toString(replaced), Character.toString(replacer));
|
||||
|
||||
//Remove all duplicate characters
|
||||
StringBuilder uniqueKey = new StringBuilder();
|
||||
keyword.chars().distinct().forEach(c -> uniqueKey.append((char)c));
|
||||
this.keyword = uniqueKey.toString();
|
||||
logger.debug("Cleaned keyword {}", keyword);
|
||||
|
||||
//Create the grid from the sanitized keyword
|
||||
createGrid();
|
||||
}
|
||||
//Returns the location of the given charcter in the grid
|
||||
protected CharLocation findChar(char letter) throws InvalidInputException{
|
||||
logger.debug("Finding {} in grid", letter);
|
||||
|
||||
for(int row = 0;row < grid.length;++row){
|
||||
for(int col = 0;col < grid[row].length;++col){
|
||||
if(grid[row][col] == letter){
|
||||
logger.debug("Found at {}, {}", row, col);
|
||||
return new CharLocation(row, col);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//If it was not found something went wrong
|
||||
throw new InvalidInputException("The character '" + letter + "' was not found in the grid");
|
||||
}
|
||||
//Adds characters that aren't letters to the output
|
||||
protected void addCharactersToCleanStringEncode(String cleanString){
|
||||
logger.debug("Formatting output string for encoding");
|
||||
|
||||
int outputCnt = 0;
|
||||
StringBuilder fullOutput = new StringBuilder();
|
||||
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
|
||||
logger.debug("Working character {}", inputString.charAt(inputCnt));
|
||||
|
||||
//Add both numbers of any letters to the output
|
||||
if(Character.isAlphabetic(inputString.charAt(inputCnt))){
|
||||
logger.debug("Adding encoded characters");
|
||||
|
||||
fullOutput.append(cleanString.charAt(outputCnt++));
|
||||
fullOutput.append(cleanString.charAt(outputCnt++));
|
||||
}
|
||||
//Add any other characters that appear to the output
|
||||
else{
|
||||
logger.debug("Adding symbols");
|
||||
|
||||
fullOutput.append(inputString.charAt(inputCnt));
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug("Formatted output '{}'", fullOutput);
|
||||
outputString = fullOutput.toString();
|
||||
}
|
||||
protected void addCharactersToCleanStringDecode(String cleanString){
|
||||
logger.debug("Formatting output string to decoding");
|
||||
|
||||
int outputCnt = 0;
|
||||
StringBuilder fullOutput = new StringBuilder();
|
||||
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
|
||||
logger.debug("Working character {}", inputString.charAt(inputCnt));
|
||||
|
||||
//Add the letter to the output and skip the second number
|
||||
if(Character.isDigit(inputString.charAt(inputCnt))){
|
||||
logger.debug("Adding decoded characters");
|
||||
|
||||
fullOutput.append(cleanString.charAt(outputCnt++));
|
||||
++inputCnt;
|
||||
}
|
||||
//Add any other characters that appear to the output
|
||||
else{
|
||||
logger.debug("Adding symbols");
|
||||
|
||||
fullOutput.append(inputString.charAt(inputCnt));
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug("Formatted output '{}'", fullOutput);
|
||||
outputString = fullOutput.toString();
|
||||
}
|
||||
//Encodes inputString using the Playfair cipher and stores the result in outputString
|
||||
private String encode() throws InvalidInputException{
|
||||
logger.debug("Encoding");
|
||||
|
||||
StringBuilder output = new StringBuilder();
|
||||
String cleanString = getPreparedInputStringEncoding();
|
||||
for(int cnt = 0;cnt < cleanString.length();++cnt){
|
||||
//Get the next character to be encoded
|
||||
char ch = cleanString.charAt(cnt);
|
||||
|
||||
logger.debug("Working character {}", ch);
|
||||
|
||||
//Find the letter in the grid
|
||||
CharLocation location = findChar(ch);
|
||||
logger.debug("Location {}, {}", location.getX() + 1, location.getY() + 1);
|
||||
|
||||
//Add the grid location to the output
|
||||
output.append(location.getX() + 1);
|
||||
@@ -231,6 +305,8 @@ public class PolybiusSquare{
|
||||
}
|
||||
//Decodes inputString using the Playfair cipher and stores the result in outputString
|
||||
private String decode(){
|
||||
logger.debug("Decoding");
|
||||
|
||||
StringBuilder output = new StringBuilder();
|
||||
String cleanString = getPreparedInputStringDecoding();
|
||||
for(int cnt = 0;cnt < cleanString.length();){
|
||||
@@ -238,9 +314,13 @@ public class PolybiusSquare{
|
||||
char firstDigit = cleanString.charAt(cnt++);
|
||||
char secondDigit = cleanString.charAt(cnt++);
|
||||
|
||||
logger.debug("Digits to decode {} {}", firstDigit, secondDigit);
|
||||
|
||||
//Get the next character
|
||||
char letter = grid[Integer.valueOf(Character.toString(firstDigit)) - 1][Integer.valueOf(Character.toString(secondDigit)) - 1];
|
||||
|
||||
logger.debug("Decoded letter {}", letter);
|
||||
|
||||
//Add the new letter to the output
|
||||
output.append(letter);
|
||||
}
|
||||
@@ -296,6 +376,8 @@ public class PolybiusSquare{
|
||||
|
||||
//Makes sure all variables are empty
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
grid = new char[5][5];
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
@@ -340,11 +422,15 @@ public class PolybiusSquare{
|
||||
return outputString;
|
||||
}
|
||||
public String getGrid(){
|
||||
StringBuilder gridString = new StringBuilder();
|
||||
logger.debug("Creating string from grid");
|
||||
|
||||
StringJoiner gridString = new StringJoiner("\n");
|
||||
for(char[] row : grid){
|
||||
StringJoiner rowString = new StringJoiner(" ", "[", "]");
|
||||
for(char col : row){
|
||||
gridString.append(col);
|
||||
rowString.add(Character.toString(col));
|
||||
}
|
||||
gridString.add(rowString.toString());
|
||||
}
|
||||
|
||||
return gridString.toString();
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -8,6 +8,9 @@ package com.mattrixwv.cipherstream.polysubstitution;
|
||||
import java.util.ArrayList;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
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;
|
||||
@@ -15,6 +18,8 @@ import com.mattrixwv.cipherstream.exceptions.InvalidBaseException;
|
||||
|
||||
|
||||
public class Trifid{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Trifid.class);
|
||||
|
||||
//A class representing the location of a character in the grid
|
||||
private class CharLocation{
|
||||
private int x;
|
||||
@@ -39,7 +44,7 @@ public class Trifid{
|
||||
}
|
||||
}
|
||||
|
||||
//Variables
|
||||
//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
|
||||
@@ -61,6 +66,8 @@ public class Trifid{
|
||||
throw new InvalidCharacterException("Fill in must not be a letter");
|
||||
}
|
||||
|
||||
logger.debug("Setting fill in {}", fillIn);
|
||||
|
||||
//Save the fillIn character
|
||||
this.fillIn = fillIn;
|
||||
}
|
||||
@@ -71,25 +78,35 @@ public class Trifid{
|
||||
throw new InvalidKeywordException("Keyword cannot be null");
|
||||
}
|
||||
|
||||
logger.debug("Original keyword {}", keyword);
|
||||
|
||||
//Change everything to uppercase
|
||||
logger.debug("Removing case");
|
||||
keyword = keyword.toUpperCase();
|
||||
|
||||
//Remove everything except capital letters
|
||||
logger.debug("Removing all invalid characters");
|
||||
keyword = keyword.replaceAll("[^A-Z" + fillIn + "]", "");
|
||||
|
||||
//Add all letters in the alphabet and fillIn to the key
|
||||
logger.debug("Appending entire alphabet to keyword");
|
||||
keyword += "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + fillIn;
|
||||
|
||||
//Remove all duplicate characters
|
||||
logger.debug("Removing duplicated characters");
|
||||
StringBuilder uniqueKey = new StringBuilder();
|
||||
keyword.chars().distinct().forEach(c -> uniqueKey.append((char)c));
|
||||
this.keyword = uniqueKey.toString();
|
||||
|
||||
logger.debug("Cleaned keyword {}", this.keyword);
|
||||
|
||||
//Create the grid from the sanitized keyword
|
||||
createGrid();
|
||||
}
|
||||
//Creates the grid from the keyword
|
||||
private void createGrid(){
|
||||
logger.debug("Creating grid from keyword");
|
||||
|
||||
for(int layerCnt = 0;layerCnt < grid.length;++layerCnt){
|
||||
for(int rowCnt = 0;rowCnt < grid[layerCnt].length;++rowCnt){
|
||||
for(int colCnt = 0;colCnt < grid[layerCnt][rowCnt].length;++colCnt){
|
||||
@@ -99,6 +116,8 @@ public class Trifid{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug("Completed grid\n{}", getGrid());
|
||||
}
|
||||
//Ensures groupSize constraints
|
||||
private void setGroupSize(int groupSize) throws InvalidBaseException{
|
||||
@@ -106,6 +125,8 @@ public class Trifid{
|
||||
throw new InvalidBaseException("Group size must be > 0");
|
||||
}
|
||||
|
||||
logger.debug("Setting group size");
|
||||
|
||||
this.groupSize = groupSize;
|
||||
}
|
||||
//Ensures inputString constraints
|
||||
@@ -115,21 +136,27 @@ public class Trifid{
|
||||
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){
|
||||
if(Character.isWhitespace(fillIn)){
|
||||
throw new InvalidInputException("If fillIn is whitespace, whitespace must be preserved");
|
||||
}
|
||||
logger.debug("Removing whitespace");
|
||||
inputString = inputString.replaceAll("\\s", "");
|
||||
}
|
||||
if(!preserveSymbols){
|
||||
logger.debug("Removing symbols");
|
||||
inputString = inputString.replaceAll("[^a-zA-Z" + fillIn + "\\s]", "");
|
||||
}
|
||||
|
||||
//Save the string
|
||||
logger.debug("Cleaned input string '{}'", inputString);
|
||||
this.inputString = inputString;
|
||||
|
||||
//Ensure the string isn't blank
|
||||
@@ -139,14 +166,19 @@ public class Trifid{
|
||||
}
|
||||
//Returns the inputString with only letters
|
||||
private String getCleanInputString(){
|
||||
logger.debug("Cleaning input string for encoding");
|
||||
return inputString.toUpperCase().replaceAll("[^A-Z" + fillIn + "]", "");
|
||||
}
|
||||
//Returns the location of the given character in the grid
|
||||
private CharLocation findChar(char letter) throws InvalidCharacterException{
|
||||
logger.debug("Finding character {} in grid", letter);
|
||||
|
||||
for(int layer = 0;layer < grid.length;++layer){
|
||||
for(int row = 0;row < grid[layer].length;++row){
|
||||
for(int col = 0;col < grid[layer][row].length;++col){
|
||||
if(grid[layer][row][col] == letter){
|
||||
logger.debug("Found at {} {} {}", layer, row, col);
|
||||
|
||||
return new CharLocation(row, col, layer);
|
||||
}
|
||||
}
|
||||
@@ -168,32 +200,44 @@ public class Trifid{
|
||||
throw new InvalidCharacterException("z cannot be larget than 2");
|
||||
}
|
||||
|
||||
logger.debug("Getting character at {} {} {}", location.getZ(), location.getX(), location.getY());
|
||||
|
||||
return grid[location.getZ()][location.getX()][location.getY()];
|
||||
}
|
||||
//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("Working character {}", ch);
|
||||
if(Character.isUpperCase(ch)){
|
||||
logger.debug("Formatting uppercase");
|
||||
output.append(Character.toUpperCase(outputString.charAt(outputCnt++)));
|
||||
}
|
||||
else if(Character.isLowerCase(ch)){
|
||||
logger.debug("Formatting lowercase");
|
||||
output.append(Character.toLowerCase(outputString.charAt(outputCnt++)));
|
||||
}
|
||||
else{
|
||||
logger.debug("Appending symbol");
|
||||
output.append(ch);
|
||||
}
|
||||
}
|
||||
|
||||
//Save the output
|
||||
logger.debug("Formatted output '{}'", output);
|
||||
this.outputString = output.toString();
|
||||
}
|
||||
//Encodes inputString using a polybius square and stores the result in outputString
|
||||
private void encode() throws InvalidCharacterException{
|
||||
logger.debug("Encoding");
|
||||
|
||||
//Step through every element in the sanitized inputString encoding the letters
|
||||
logger.debug("Conveting letters to coordinates");
|
||||
ArrayList<CharLocation> locations = new ArrayList<>();
|
||||
for(char ch : getCleanInputString().toCharArray()){
|
||||
//Get the location of the char in the grid
|
||||
@@ -202,6 +246,7 @@ public class Trifid{
|
||||
}
|
||||
|
||||
//Split the locations up by group
|
||||
logger.debug("Splitting locations into groups");
|
||||
int numGroups = inputString.length() / groupSize;
|
||||
if(numGroups == 0){
|
||||
numGroups = 1;
|
||||
@@ -220,6 +265,7 @@ public class Trifid{
|
||||
}
|
||||
|
||||
//Split the coordinates into rows
|
||||
logger.debug("Splitting groups into rows");
|
||||
ArrayList<Integer> coordinates = new ArrayList<>(locations.size() * 3);
|
||||
for(ArrayList<CharLocation> group : groups){
|
||||
//Split the coordinates up into 3 rows
|
||||
@@ -236,6 +282,7 @@ public class Trifid{
|
||||
coordinates.addAll(cols);
|
||||
}
|
||||
//Create new locations from the rows of coordinates
|
||||
logger.debug("Converting split locations into new locations");
|
||||
ArrayList<CharLocation> newLocations = new ArrayList<>(locations.size());
|
||||
for(int cnt = 0;cnt < coordinates.size();){
|
||||
int z = coordinates.get(cnt++);
|
||||
@@ -245,6 +292,7 @@ public class Trifid{
|
||||
}
|
||||
|
||||
//Get the new letters from the grid
|
||||
logger.debug("Converting new locations into characters");
|
||||
StringBuilder output = new StringBuilder();
|
||||
for(CharLocation loc : newLocations){
|
||||
output.append(getChar(loc));
|
||||
@@ -255,7 +303,10 @@ public class Trifid{
|
||||
}
|
||||
//Decodes inputString using a polybius square and stores the result in outputString
|
||||
private void decode() throws InvalidCharacterException{
|
||||
logger.debug("Decoding");
|
||||
|
||||
//Step through every element in the sanitized inputString encoding the letters
|
||||
logger.debug("Converting letters to coordinates");
|
||||
ArrayList<CharLocation> locations = new ArrayList<>();
|
||||
for(char ch : getCleanInputString().toCharArray()){
|
||||
//Get the location of the char in the grid
|
||||
@@ -264,6 +315,7 @@ public class Trifid{
|
||||
}
|
||||
|
||||
//Split the locations up by group
|
||||
logger.debug("Splitting locations into groups");
|
||||
int numGroups = inputString.length() / groupSize;
|
||||
if(numGroups == 0){
|
||||
numGroups = 1;
|
||||
@@ -282,6 +334,7 @@ public class Trifid{
|
||||
}
|
||||
|
||||
//Split the coordinates into rows by group and create the original grid locations
|
||||
logger.debug("Putting locations into rows");
|
||||
ArrayList<CharLocation> originalLocations = new ArrayList<>(locations.size());
|
||||
for(ArrayList<CharLocation> group : groups){
|
||||
//Read all of the coordinates from the group out into a row
|
||||
@@ -293,6 +346,7 @@ public class Trifid{
|
||||
}
|
||||
|
||||
//Read out the coordinates into new locations
|
||||
logger.debug("Converting locations into new locations");
|
||||
ArrayList<CharLocation> originalGroup = new ArrayList<>(group.size());
|
||||
for(int cnt = 0;cnt < group.size();++cnt){
|
||||
originalGroup.add(new CharLocation(0, 0, 0));
|
||||
@@ -311,6 +365,7 @@ public class Trifid{
|
||||
}
|
||||
|
||||
//Get the original letters from the grid
|
||||
logger.debug("Converting new locations into letters");
|
||||
StringBuilder output = new StringBuilder();
|
||||
for(CharLocation loc : originalLocations){
|
||||
output.append(getChar(loc));
|
||||
@@ -374,6 +429,8 @@ public class Trifid{
|
||||
|
||||
//Makes sure all variables are empty
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
keyword = "";
|
||||
@@ -397,6 +454,8 @@ public class Trifid{
|
||||
return fillIn;
|
||||
}
|
||||
public String getGrid(){
|
||||
logger.debug("Creating string from grid");
|
||||
|
||||
StringJoiner layers = new StringJoiner("\n\n");
|
||||
|
||||
for(char[][] layer : grid){
|
||||
|
||||
Reference in New Issue
Block a user