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,12 +1,15 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGVX.java
//Mattrixwv
// Created: 01-26-22
//Modified: 07-03-22
//Modified: 07-09-22
package com.mattrixwv.cipherstream.combination;
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,9 +18,16 @@ import com.mattrixwv.cipherstream.polysubstitution.PolybiusSquare;
public class ADFGVX{
private static final Logger logger = LoggerFactory.getLogger(ADFGVX.class);
//Internal classes
private class LargePolybiusSquare extends PolybiusSquare{
private static final Logger logger = LoggerFactory.getLogger(LargePolybiusSquare.class);
@Override
protected void createGrid(){
logger.debug("Creating grid");
for(int row = 0;row < 6;++row){
for(int col = 0;col < 6;++col){
char letter = keyword.charAt((6 * row) + col);
@@ -31,16 +41,22 @@ public class ADFGVX{
throw new NullPointerException("Input cannot be null");
}
logger.debug("Original input string '{}'", inputString);
//Change to upper 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-Z0-9\\s]", "");
}
@@ -56,14 +72,21 @@ public class ADFGVX{
//Save the string
this.inputString = inputString;
logger.debug("Cleaned input string '{}'", inputString);
if(this.inputString.isBlank() || getPreparedInputStringEncoding().isBlank()){
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
@Override
protected String getPreparedInputStringEncoding(){
logger.debug("Preparing input string for encoding");
String cleanString = inputString.toUpperCase();
cleanString = cleanString.replaceAll("[^A-Z0-9]", "");
logger.debug("Cleaned input string '{}'", cleanString);
return cleanString;
}
@Override
@@ -71,6 +94,9 @@ public class ADFGVX{
if(keyword == null){
throw new NullPointerException("Keyword cannot be null");
}
logger.debug("Original keyword '{}'", keyword);
//Change everything to uppercase
keyword = keyword.toUpperCase();
@@ -85,45 +111,67 @@ public class ADFGVX{
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();
}
@Override
protected void addCharactersToCleanStringEncode(String cleanString){
logger.debug("Formatting output string");
int outputCnt = 0;
StringBuilder fullOutput = new StringBuilder();
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
logger.debug("Current character {}", inputString.charAt(inputCnt));
//Add both numbers of any letters to the output
if(Character.isAlphabetic(inputString.charAt(inputCnt)) || Character.isDigit(inputString.charAt(inputCnt))){
logger.debug("Appending character");
fullOutput.append(cleanString.charAt(outputCnt++));
fullOutput.append(cleanString.charAt(outputCnt++));
}
//Add any other characters that appear to the output
else{
logger.debug("Appending symbol");
fullOutput.append(inputString.charAt(inputCnt));
}
}
outputString = fullOutput.toString();
}
@Override
protected void addCharactersToCleanStringDecode(String cleanString){
logger.debug("Formatting output string");
int outputCnt = 0;
StringBuilder fullOutput = new StringBuilder();
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
logger.debug("Current character {}", inputString.charAt(inputCnt));
//Add the letter to the output and skip the second number
if(Character.isDigit(inputString.charAt(inputCnt)) || Character.isAlphabetic(inputString.charAt(inputCnt))){
logger.debug("Appending character");
fullOutput.append(cleanString.charAt(outputCnt++));
++inputCnt;
}
//Add any other characters that appear to the output
else{
logger.debug("Appending symbol");
fullOutput.append(inputString.charAt(inputCnt));
}
}
outputString = fullOutput.toString();
}
@Override
public void reset(){
logger.debug("reseting");
grid = new char[6][6];
inputString = "";
outputString = "";
@@ -134,55 +182,77 @@ public class ADFGVX{
super(preserveWhitespace, preserveSymbols);
}
}
private boolean preserveCapitals;
private boolean preserveWhitespace;
private boolean preserveSymbols;
private String inputString;
private String outputString;
private String squareKeyword;
private String keyword;
private LargePolybiusSquare largePolybiusSquare;
private Columnar columnar;
//Fields
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
private String inputString; //The string that needs encoded/decoded
private String outputString; //The string that is output after encoding/decoding
private String squareKeyword; //The keyword used in the Polybius Square
private String keyword; //The Keyword used in the Columnar cipher
//Internal ciphers
private LargePolybiusSquare largePolybiusSquare; //The first step in encoding
private Columnar columnar; //The second step in encoding
//Ensures Polybius keyword constraints
private void setSquareKeyword(String squareKeyword) throws InvalidKeywordException{
if(squareKeyword == null){
throw new InvalidKeywordException("Square Keyword cannot be null");
}
logger.debug("Square Keyword '{}'", squareKeyword);
this.squareKeyword = squareKeyword;
}
//Ensures Columnar keyword constraints
private void setKeyword(String keyword) throws InvalidKeywordException{
if(keyword == null){
throw new InvalidKeywordException("Keyword cannot be null");
}
logger.debug("Keyword '{}'", keyword);
this.keyword = keyword;
}
//Ensures inputString constraints
private void setInputString(String inputString) throws InvalidInputException{
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
}
logger.debug("Original input string '{}'", inputString);
if(!preserveCapitals){
logger.debug("Removing capitals");
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]", "");
}
this.inputString = inputString;
logger.debug("Cleaned input string '{}'", inputString);
if(this.inputString.isBlank()){
throw new InvalidInputException("Input cannot be blank");
}
}
//Format the output string with capitals, symbols, and numbers that are in the input string
private void formatOutputStringEncode(){
logger.debug("Formatting output string");
StringBuilder output = new StringBuilder();
int outputLocation = 0;
for(char ch : inputString.toCharArray()){
logger.debug("Input string {}", ch);
if(Character.isUpperCase(ch)){
output.append(Character.toUpperCase(outputString.charAt(outputLocation++)));
output.append(Character.toUpperCase(outputString.charAt(outputLocation++)));
@@ -196,13 +266,17 @@ public class ADFGVX{
}
}
logger.debug("Saving output string '{}'", output);
outputString = output.toString();
}
private void formatOutputStringDecode(){
logger.debug("Formatting output string");
StringBuilder output = new StringBuilder();
int outputLocation = 0;
for(int inputLocation = 0;inputLocation < inputString.length();++inputLocation){
char ch = inputString.charAt(inputLocation);
logger.debug("Input character {}", ch);
if(Character.isUpperCase(ch)){
output.append(Character.toUpperCase(outputString.charAt(outputLocation++)));
++inputLocation;
@@ -215,15 +289,21 @@ public class ADFGVX{
output.append(ch);
}
}
logger.debug("Saving output string '{}'", output);
outputString = output.toString();
}
//Encodes the inputString and stores the result in outputString
private String encode() throws InvalidCharacterException, InvalidInputException, InvalidKeywordException{
//Encode the input with polybius
logger.debug("Encoding using Polybius Square");
String polybiusOutput = largePolybiusSquare.encode(squareKeyword, inputString);
//Change polybius to use the correct symbols
logger.debug("Replacing coordinates with letters");
polybiusOutput = polybiusOutput.replace("1", "A").replace("2", "D").replace("3", "F").replace("4", "G").replace("5", "V").replace("6", "X");
//Encode polybius's output with columnar
logger.debug("Encoding with columnar");
String columnarOutput = columnar.encode(keyword, polybiusOutput);
outputString = columnarOutput;
@@ -232,13 +312,17 @@ public class ADFGVX{
return outputString;
}
//Decodes the inputString and stores the result in outputString
private String decode() throws InvalidKeywordException, InvalidCharacterException, InvalidInputException{
//Decode the input with columnar
logger.debug("Decoding columnar");
String columnarOutput = columnar.decode(keyword, inputString);
//Change the symbols to the correct ones for polybius
logger.debug("Replacing characters with coordinates");
columnarOutput = columnarOutput.replace("A", "1").replace("D", "2").replace("F", "3").replace("G", "4").replace("V", "5").replace("X", "6");
//Decode with polybius
logger.debug("Decoding Polybius Square");
String polybiusOutput = largePolybiusSquare.decode(squareKeyword, columnarOutput);
outputString = polybiusOutput;
@@ -248,6 +332,8 @@ public class ADFGVX{
return outputString;
}
//Constructor
public ADFGVX() throws InvalidCharacterException{
preserveCapitals = false;
preserveWhitespace = false;
@@ -261,31 +347,41 @@ public class ADFGVX{
reset();
}
//Encodes inputString using keyword and returns the result
public String encode(String squareKeyword, String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
setSquareKeyword(squareKeyword);
setKeyword(keyword);
setInputString(inputString);
return encode();
}
//Decodes inputString using keyword and returns the result
public String decode(String squareKeyword, String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
setSquareKeyword(squareKeyword);
setKeyword(keyword);
setInputString(inputString);
return decode();
}
//Returns the cleaned inputString
public String getInputString(){
return inputString;
}
//Returns the outputString
public String getOutputString(){
return outputString;
}
//Returns the cleaned Polybius Square keyword
public String getSquareKeyword(){
return squareKeyword;
}
//Returns the cleaned Columnar keyword
public String getKeyword(){
return keyword;
}
//Makes sure all of the variables are empty
public void reset() throws InvalidCharacterException{
logger.debug("Reseting fields");
largePolybiusSquare = new LargePolybiusSquare(false, false);
columnar = new Columnar(false, false, false, true, 'B');
inputString = "";

View File

@@ -1,10 +1,13 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGX.java
//Mattrixwv
// Created: 01-25-22
//Modified: 07-03-22
//Modified: 07-09-22
package com.mattrixwv.cipherstream.combination;
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;
@@ -13,22 +16,28 @@ import com.mattrixwv.cipherstream.polysubstitution.PolybiusSquare;
public class ADFGX{
private String inputString; //This is the string that needs encoded/decoded
private String outputString; //This is the string that is output after encoding/decoding
private String squareKeyword; //The keyword used to create the Polybius Square
private String keyword; //The keyword used to create the Columnar cipher
private static final Logger logger = LoggerFactory.getLogger(ADFGX.class);
//Internal fields
private String inputString; //The string that needs encoded/decoded
private String outputString; //The string that is output after encoding/decoding
private String squareKeyword; //The keyword used in the Polybius Square
private String keyword; //The keyword used in the Columnar cipher
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
//Internal ciphers
private PolybiusSquare polybiusSquare; //The first step in encoding
private Columnar columnar; //The second step in encoding
//Ensures Polybius keyword constraints
private void setSquareKeyword(String squareKeyword) throws InvalidKeywordException{
if(squareKeyword == null){
throw new InvalidKeywordException("Square Keyword cannot be null");
throw new InvalidKeywordException("Square keyword cannot be null");
}
logger.debug("squareKeyword = {}", squareKeyword);
this.squareKeyword = squareKeyword;
}
//Ensures Columnar keyword constraints
@@ -37,6 +46,7 @@ public class ADFGX{
throw new InvalidKeywordException("Keyword cannot be null");
}
logger.debug("keyword = {}", keyword);
this.keyword = keyword;
}
//Ensures inputString constraints
@@ -45,13 +55,20 @@ public class ADFGX{
throw new InvalidInputException("Input cannot be null");
}
logger.debug("original input string '{}'", inputString);
if(!preserveCapitals){
logger.debug("Removing capitals");
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]", "");
}
@@ -60,55 +77,79 @@ public class ADFGX{
if(this.inputString.isBlank()){
throw new InvalidInputException("Input cannot be blank");
}
logger.debug("cleaned input string '{}'", inputString);
}
//Format the output string with capitals, symbols, and numbers that are in the input string
private void formatOutputStringEncode(){
logger.debug("Formatting output string to match input string");
StringBuilder output = new StringBuilder();
int outputLocation = 0;
for(char ch : inputString.toCharArray()){
logger.debug("Input character {}", ch);
if(Character.isUpperCase(ch)){
logger.debug("Converting output to uppercase");
output.append(Character.toUpperCase(outputString.charAt(outputLocation++)));
output.append(Character.toUpperCase(outputString.charAt(outputLocation++)));
}
else if(Character.isLowerCase(ch)){
logger.debug("Converting output to lowercase");
output.append(Character.toLowerCase(outputString.charAt(outputLocation++)));
output.append(Character.toLowerCase(outputString.charAt(outputLocation++)));
}
else{
logger.debug("Appending symbol to output");
output.append(ch);
}
}
logger.debug("Saving output string '{}'", outputString);
outputString = output.toString();
}
private void formatOutputStringDecode(){
logger.debug("Formatting output string to match input string");
StringBuilder output = new StringBuilder();
int outputLocation = 0;
for(int inputLocation = 0;inputLocation < inputString.length();++inputLocation){
char ch = inputString.charAt(inputLocation);
logger.debug("Input character {}", ch);
if(Character.isUpperCase(ch)){
logger.debug("Converting output to uppercase");
output.append(Character.toUpperCase(outputString.charAt(outputLocation++)));
++inputLocation;
}
else if(Character.isLowerCase(ch)){
logger.debug("Converting output to lowercase");
output.append(Character.toLowerCase(outputString.charAt(outputLocation++)));
++inputLocation;
}
else{
logger.debug("Appending symbol to output");
output.append(ch);
}
}
logger.debug("Saving output string '{}'", output);
outputString = output.toString();
}
//Encodes the inputString and stores the result in outputString
private void encode() throws InvalidCharacterException, InvalidInputException, InvalidKeywordException{
//Encode the input with polybius
logger.debug("Encoding using Polybius Square");
String polybiusOutput = polybiusSquare.encode(squareKeyword, inputString);
squareKeyword = polybiusSquare.getKeyword();
//Change polybius to use the correct symbols
logger.debug("Replacing coordinates with letters");
polybiusOutput = polybiusOutput.replace("1", "A").replace("2", "D").replace("3", "F").replace("4", "G").replace("5", "X");
//Encode polybius's output with columnar
logger.debug("Encoding using columnar");
String columnarOutput = columnar.encode(keyword, polybiusOutput);
keyword = columnar.getKeyword();
outputString = columnarOutput;
@@ -119,12 +160,15 @@ public class ADFGX{
//Decodes the inputString and stores the result in outputString
private void decode() throws InvalidKeywordException, InvalidCharacterException, InvalidInputException{
//Decode the input with columnar
logger.debug("Decoding using columnar");
String columnarOutput = columnar.decode(keyword, inputString);
keyword = columnar.getKeyword();
//Change the symbols to the correct ones for polybius
logger.debug("Replacing letters with coordinates");
columnarOutput = columnarOutput.replace("A", "1").replace("D", "2").replace("F", "3").replace("G", "4").replace("X", "5");
//Decode with polybius
logger.debug("Decoding using Polybius Square");
String polybiusOutput = polybiusSquare.decode(squareKeyword, columnarOutput);
squareKeyword = polybiusSquare.getKeyword();
outputString = polybiusOutput;
@@ -183,6 +227,8 @@ public class ADFGX{
}
//Makes sure all of the variables are empty
public void reset() throws InvalidCharacterException{
logger.debug("Resetting fields");
polybiusSquare = new PolybiusSquare(false, false);
columnar = new Columnar(false, false, false, true, 'B');
inputString = "";

View File

@@ -0,0 +1,21 @@
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/exceptions/InvalidKeyException.java
//Matrixwv
// Created: 07-09-22
//Modified: 07-09-22
package com.mattrixwv.cipherstream.exceptions;
public class InvalidKeyException extends RuntimeException{
public InvalidKeyException(){
super();
}
public InvalidKeyException(String message){
super(message);
}
public InvalidKeyException(Throwable error){
super(error);
}
public InvalidKeyException(String message, Throwable error){
super(message, error);
}
}

View File

@@ -1,10 +1,13 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Affine.java
//Mattrixwv
// Created: 01-26-22
//Modified: 02-17-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;
@@ -12,15 +15,21 @@ import mattrixwv.NumberAlgorithms;
public class Affine{
private boolean preserveCapitals;
private boolean preserveSymbols;
private boolean preserveWhitespace;
private String inputString;
private String outputString;
private int key1; //Key1 must be relatively prime to 26
private int key2;
private static final Logger logger = LoggerFactory.getLogger(Affine.class);
//Fields
private boolean preserveCapitals; //Whether to respect capitals in the output string
private boolean preserveSymbols; //Whether to respect symbols in the output string
private boolean preserveWhitespace; //Whether to respect whitespace in the output string
private String inputString; //The string that needs encoded/decoded
private String outputString; //The string that is output after encoding/decoding
private int key1; //The multiplicative key. Key1 must be relatively prime to 26
private int key2; //The additive key
//Ensures key1 constraints
private void setKey1(int key1) throws InvalidKeywordException{
logger.debug("Setting key1 {}", key1);
//If the key is negative change it to possitive
if(key1 < 0){
key1 %= 26;
@@ -34,8 +43,13 @@ public class Affine{
//Save the key
this.key1 = key1;
logger.debug("Cleaned key1 {}", key1);
}
//Ensures key2 constraints
private void setKey2(int key2){
logger.debug("Setting key2 {}", key2);
//If the key is negative change it to possitive
if(key2 < 0){
key2 %= 26;
@@ -44,32 +58,49 @@ public class Affine{
//Save the key
this.key2 = key2;
logger.debug("Cleaned key2 {}", key2);
}
//Ensures inputString constraints
private void setInputString(String inputString) throws InvalidInputException{
if(inputString == null){
throw new InvalidInputException("Input must not be null");
}
logger.debug("Original input string '{}'", inputString);
if(!preserveCapitals){
logger.debug("Removing case");
inputString = inputString.toLowerCase();
}
if(!preserveWhitespace){
logger.debug("Removing whitespace");
inputString = inputString.replaceAll("\\s", "");
}
if(!preserveSymbols){
logger.debug("Removing symbols");
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
}
this.inputString = inputString;
logger.debug("Cleaned input string '{}'", inputString);
if(this.inputString.isBlank()){
throw new InvalidInputException("Input cannot be blank");
}
}
//Encodes the inputString and stores the result in outputString
private String encode(){
logger.debug("Encoding");
//Step through every character in the input and encode it if needed
StringBuilder output = new StringBuilder();
for(char ch : inputString.toCharArray()){
logger.debug("Current char {}", ch);
//Encode all letters
if(Character.isUpperCase(ch)){
//Change the character to a number
@@ -79,6 +110,8 @@ public class Affine{
//Change the new number back to a character and append it to the output
char newChar = (char)(letter + 65);
output.append(newChar);
logger.debug("Encoded char {}", newChar);
}
else if(Character.isLowerCase(ch)){
//Change the character to a number
@@ -88,6 +121,8 @@ public class Affine{
//Change the new number back to a character and append it to the output
char newChar = (char)(letter + 97);
output.append(newChar);
logger.debug("Encoded char {}", newChar);
}
//Pass all other characters through
else{
@@ -96,19 +131,25 @@ public class Affine{
}
//Save and return the output
logger.debug("Saving output string '{}'", output);
outputString = output.toString();
return outputString;
}
//Decodes the inputString and stores the result in outputString
private String decode(){
logger.debug("Decoding");
//Find the multiplicative inverse of key1
int key1I = 1;
while(((key1 * key1I) % 26) != 1){
++key1I;
}
logger.debug("Key1 inverse {}", key1I);
//Step through every character in the input and decode it if needed
StringBuilder output = new StringBuilder();
for(char ch : inputString.toCharArray()){
logger.debug("Current char {}", ch);
//Encode all letters
if(Character.isUpperCase(ch)){
//Change the letter to a number
@@ -121,6 +162,8 @@ public class Affine{
//Change the new number back to a character and append it to the output
char newChar = (char)(letter + 65);
output.append(newChar);
logger.debug("Decoded char {}", newChar);
}
else if(Character.isLowerCase(ch)){
//Change the letter to a number
@@ -133,6 +176,8 @@ public class Affine{
//Change the new number back to a character and append it to the output
char newChar = (char)(letter + 97);
output.append(newChar);
logger.debug("Decoded char {}", newChar);
}
//Pass all other characters through
else{
@@ -141,10 +186,13 @@ public class Affine{
}
//Save and return the output
logger.debug("Saving output string '{}'", output);
outputString = output.toString();
return outputString;
}
//Constructor
public Affine(){
preserveCapitals = false;
preserveSymbols = false;
@@ -157,12 +205,15 @@ public class Affine{
this.preserveWhitespace = preserveWhitespace;
reset();
}
//Encodes inputString using key1 and key2 and returns the result
public String encode(int key1, int key2, String inputString) throws InvalidKeywordException, InvalidInputException{
setKey1(key1);
setKey2(key2);
setInputString(inputString);
return encode();
}
//Decodes inputString using key1 and key2 and returns the result
public String decode(int key1, int key2, String inputString) throws InvalidKeywordException, InvalidInputException{
setKey1(key1);
setKey2(key2);
@@ -170,22 +221,29 @@ public class Affine{
return decode();
}
//Returns the cleaned inputString
public String getInputString(){
return inputString;
}
//Returns the outputString
public String getOutputString(){
return outputString;
}
//Returns the cleaned key1
public int getKey1(){
return key1;
}
//Returns the cleaned key2
public int getKey2(){
return key2;
}
//Makes sure all of the variables are empty
public void reset(){
logger.debug("Resetting fields");
inputString = "";
outputString = "";
key1 = 0;
key2 = 0;
}
public String getInputString(){
return inputString;
}
public String getOutputString(){
return outputString;
}
public int getKey1(){
return key1;
}
public int getKey2(){
return key2;
}
}

View File

@@ -1,25 +1,34 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Atbash.java
//Mattrixwv
// Created: 07-25-21
//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;
public class Atbash{
private static final Logger logger = LoggerFactory.getLogger(Atbash.class);
private String inputString; //Holds the string that needs encoded or decoded
private String outputString; //The encoded/decoded 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
//Encodes inputString and stores in outputString
private String encode(){
logger.debug("Encoding");
StringBuilder output = new StringBuilder();
//Step through every element in the inputString and shift it the correct amount
for(int cnt = 0;cnt < inputString.length();++cnt){
char currentChar = inputString.charAt(cnt);
logger.debug("Encoding char {}", currentChar);
//Decode if the letter is alphabetic
if(Character.isAlphabetic(currentChar)){
//Use either uppercase or lowercase for the base
@@ -37,6 +46,8 @@ public class Atbash{
}
}
//Return the output
logger.debug("Saving output string '{}'", output);
outputString = output.toString();
return outputString;
}
@@ -46,15 +57,23 @@ public class Atbash{
throw new NullPointerException("Input cannot be null");
}
logger.debug("Original input string '{}'", inputString);
if(!preserveCapitals){
logger.debug("Removing case");
//Convert all letters to lowercase
inputString = inputString.toUpperCase();
}
if(!preserveWhitespace){
logger.debug("Removing whitespace");
//Remove all characters except capital letters
inputString = inputString.replaceAll("\\s", "");
}
if(!preserveSymbols){
logger.debug("Removing symbols");
//Remove all non-alpha numeric and whitespace symbols
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
}
@@ -62,12 +81,15 @@ public class Atbash{
//Save the string
this.inputString = inputString;
logger.debug("Cleaned input string '{}'", inputString);
if(this.inputString.isBlank()){
throw new InvalidInputException("Input must contain at least 1 character");
}
}
//Constructor
public Atbash(){
reset();
preserveCapitals = false;
@@ -80,22 +102,32 @@ public class Atbash{
this.preserveWhitespace = preserveWhitespace;
this.preserveSymbols = preserveSymbols;
}
public String getInputString(){
return inputString;
}
public String getOutputString(){
return outputString;
}
//Encodes inputString and returns the result
public String encode(String inputString) throws InvalidInputException{
//Make sure everything is empty before you begin
reset();
setInputString(inputString);
return encode();
}
//Decodes inputString and returns the result
public String decode(String inputString) throws InvalidInputException{
return encode(inputString);
}
//Returns the cleaned input string
public String getInputString(){
return inputString;
}
//Returns the output string
public String getOutputString(){
return outputString;
}
//Makes sure all of the variables are empty
public void reset(){
inputString = outputString = "";
logger.debug("Resetting fields");
inputString = "";
outputString = "";
}
}

View File

@@ -1,47 +1,65 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Autokey.java
//Mattrixwv
// Created: 07-25-21
//Modified: 07-03-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 Autokey extends Vigenere{
private static final Logger logger = LoggerFactory.getLogger(Autokey.class);
//Special rules for setting the strings for encoding
private void encodeSet(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
logger.debug("Setting fields for encoding");
//Set the input
setInputString(inputString);
StringBuilder newKey = new StringBuilder();
//Remove all unneccessary elements from the key
logger.debug("Setting keyword");
setKeyword(keyword);
newKey.append(keyword);
//Remove all unneccessary elements from the input
logger.debug("Adding input to keyword");
setKeyword(inputString);
newKey.append(getKeyword());
//Make sure the key is not any longer than the input
logger.debug("Removing last letters in the keyword");
keyword = newKey.substring(0, getKeyword().length());
//Set the new keyword
setKeyword(keyword);
//Make sure to update the offset
offset.clear();
setOffset();
}
//Setting the strings for decoding
private void decodeSet(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
logger.debug("Setting fields for decoding");
//Remove all unneccessary elements from the key
logger.debug("Setting keyword");
setKeyword(keyword);
//Remove all unneccessary elements from the input
logger.debug("Setting input string");
setInputString(inputString);
}
//Decodes the inputString
@Override
protected String decode(){
logger.debug("Decoding");
//Decode what the key will allow, add that to the key and continue
StringBuilder currentOutput = new StringBuilder();
StringBuilder fullOutput = new StringBuilder();
@@ -51,36 +69,55 @@ public class Autokey extends Vigenere{
for(int letterCnt = 0;letterCnt < inputString.length();++letterCnt){
//If we have reached the end of the keyword add what we have to it and continue
if(offsetCnt == keyword.length()){
logger.debug("Appending partial output to keyword");
setKeyword(keyword + currentOutput.toString());
fullOutput.append(currentOutput);
currentOutput = new StringBuilder();
}
char letter = inputString.charAt(letterCnt);
logger.debug("Working character {}", letter);
if(Character.isUpperCase(letter)){
logger.debug("Appending uppercase");
letter -= offset.get((offsetCnt++) % offset.size());
if(letter < 'A'){
logger.debug("Wrapping around to Z");
letter += 26;
}
else if(letter > 'Z'){
logger.debug("Wrapping around to A");
letter -= 26;
}
}
else if(Character.isLowerCase(letter)){
logger.debug("Appending lowercase");
letter -= offset.get((offsetCnt++) % offset.size());
if(letter < 'a'){
logger.debug("Wrapping around to z");
letter += 26;
}
else if(letter > 'z'){
logger.debug("Wrapping around to a");
letter -= 26;
}
}
logger.debug("Decoded letter {}", letter);
currentOutput.append(letter);
}
//Empty the last character that were decoded
fullOutput.append(currentOutput);
//Save and return the results
logger.debug("Saving output string '{}'", fullOutput);
outputString = fullOutput.toString();
return outputString;
}

View File

@@ -1,17 +1,25 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/Baconian.java
//Mattrixwv
// Created: 01-12-22
//Modified: 01-16-22
//Modified: 07-09-22
package com.mattrixwv.cipherstream.monosubstitution;
import java.util.ArrayList;
import java.util.Arrays;
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 Baconian{
private static final Logger logger = LoggerFactory.getLogger(Baconian.class);
//Conversions
private static final ArrayList<String> code = new ArrayList<>(Arrays.asList(
"aaaaa", "aaaab", "aaaba", "aaabb", "aabaa", "aabab", "aabba","aabbb", "abaaa", "abaaa", "abaab", "ababa", "ababb", //A-M
"abbaa", "abbab", "abbba", "abbbb", "baaaa", "baaab", "baaba", "baabb", "baabb", "babaa", "babab", "babba", "babbb" //N-Z
@@ -26,14 +34,20 @@ public class Baconian{
throw new NullPointerException("Input cannot be null");
}
logger.debug("Setting input string for encoding '{}'", inputString);
//Remove all whitespace and symbols
inputString = inputString.replaceAll("[^A-Za-z]", "");
if(!preserveCapitals){
logger.debug("Removing case");
inputString = inputString.toLowerCase();
}
this.inputString = inputString;
logger.debug("Cleaned input string '{}'", inputString);
if(this.inputString.isBlank()){
throw new InvalidInputException("Input must contain at least 1 letter");
}
@@ -43,17 +57,26 @@ public class Baconian{
throw new NullPointerException("Input cannot be null");
}
logger.debug("Setting input string for decoding '{}'", inputString);
if(!preserveCapitals){
logger.debug("Removing case");
inputString = inputString.toLowerCase();
}
//Check each Baconian Cipher letter for validity
logger.debug("Ensuring all 'letters' contain 5 characters");
for(String str : inputString.split(" ")){
logger.debug("Current 'letter' {}", str);
//Make sure each letter contains 5 characters
if(str.length() != 5){
throw new InvalidCharacterException("All Baconian letters contain exactly 5 characters: " + str);
}
//Make sure the letter contains only a's and b's
logger.debug("Replacing all non-abAB characters");
String temp = str.replaceAll("[^abAB]", "");
if(!temp.equals(str)){
throw new InvalidCharacterException("Baconian letters contain only a's and b's: " + str);
@@ -62,54 +85,76 @@ public class Baconian{
this.inputString = inputString;
logger.debug("Cleaned input string '{}'", inputString);
if(this.inputString.isBlank()){
throw new InvalidInputException("Input cannot be empty");
}
}
//Encodes the inputString and stores the result in outputString
private String encode(){
logger.debug("Encoding");
StringJoiner output = new StringJoiner(" ");
//Go through every character in the inputString and encode it
for(char ch : inputString.toCharArray()){
logger.debug("Working character {}", ch);
//Convert the character to a binary string with A = 0
String binary = null;
if(Character.isUpperCase(ch)){
logger.debug("Encoding uppercase");
binary = code.get(ch - 'A').toUpperCase();
}
else{
logger.debug("Encoding lowercase");
binary = code.get(ch - 'a').toLowerCase();
}
//Add the encoded character to the output
logger.debug("Output 'letter' {}", binary);
output.add(binary);
}
//Save and return the output
logger.debug("Saving output string '{}'", output);
outputString = output.toString();
return outputString;
}
//Decodes the inputString and stores the result in outputString
private String decode(){
logger.debug("Decoding");
StringBuilder output = new StringBuilder();
//Go through every Baconian Cipher character in the inputString and decode it
for(String baconianCharacter : inputString.split(" ")){
logger.debug("Working letter {}", baconianCharacter);
//Get the location of the Baconian character in the array
int location = code.indexOf(baconianCharacter.toLowerCase());
logger.debug("Location of letter {}", location);
//Convert the Baconian character to an ASCII character
char ch;
if(Character.isUpperCase(baconianCharacter.charAt(0))){
logger.debug("Decoding uppercase");
ch = (char)(location + 'A');
}
else{
logger.debug("Decoding lowercase");
ch = (char)(location + 'a');
}
//Add the decoded character to the output
logger.debug("Decoded character {}", ch);
output.append(ch);
}
//Save and return the output
logger.debug("Saving output string '{}'", output);
outputString = output.toString();
return outputString;
}
@@ -145,6 +190,8 @@ public class Baconian{
}
//Makes sure all of the variables are empty
public void reset(){
logger.debug("Resetting fields");
inputString = "";
outputString = "";
}

View File

@@ -1,18 +1,23 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/BaseX.java
//Mattrixwv
// Created: 01-08-22
//Modified: 01-09-22
//Modified: 07-09-22
package com.mattrixwv.cipherstream.monosubstitution;
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.InvalidBaseException;
public class BaseX{
private static final Logger logger = LoggerFactory.getLogger(BaseX.class);
private String inputString; //The string that needs encoded/decoded
private String outputString; //The encoded/decoded string
private int base; //The base that the number will be encoded at
@@ -23,6 +28,8 @@ public class BaseX{
throw new NullPointerException("Input cannot be null");
}
logger.debug("Setting input string for encoding '{}'", inputString);
this.inputString = inputString;
if(this.inputString.isBlank()){
@@ -34,15 +41,27 @@ public class BaseX{
throw new NullPointerException("Input cannot be null");
}
logger.debug("Setting input string for decoding '{}'", inputString);
//Create a string of valid 'numbers'
logger.debug("Creating string of valid 'numbers'");
StringBuilder validNumbers = new StringBuilder();
for(int cnt = 0;cnt < base;++cnt){
validNumbers.append(Integer.toString(cnt, base).toUpperCase());
String number = Integer.toString(cnt, base).toUpperCase();
logger.debug("Current number {}, converted {}", cnt, number);
validNumbers.append(number);
}
//Remove all invalid characters
logger.debug("Checking for invalid characters");
this.inputString = inputString.replaceAll("[^" + validNumbers.toString() + "\\s]", "");
//Throw an exception if there were any invalid characters
if(!this.inputString.equals(inputString)){
throw new InvalidCharacterException("inputString cannot contain anything except numbers 0-" + Integer.toString(base - 1, base) + ", and whitespace");
}
logger.debug("Cleaned input string '{}'", inputString);
if(this.inputString.isBlank()){
throw new InvalidInputException("Input must contain at least 1 letter");
}
@@ -53,32 +72,47 @@ public class BaseX{
throw new InvalidBaseException("Base cannot be a negative number");
}
logger.debug("Setting base {}", base);
this.base = base;
}
//Encode inputString, store it in outputString, and return it
private String encode(){
logger.debug("Encoding");
//Encode every character in inputString
StringJoiner output = new StringJoiner(" ");
for(int cnt = 0;cnt < inputString.length();++cnt){
//Get the next character
char ch = inputString.charAt(cnt);
logger.debug("Working number {}", ch);
//Encode the character to binary and add it to the output
output.add(Integer.toString(ch, base));
String convertedNum = Integer.toString(ch, base);
output.add(convertedNum);
logger.debug("Converted number {}", convertedNum);
}
//Save the output
outputString = output.toString().toUpperCase();
logger.debug("Saving output string '{}'", outputString);
//Return the output
return outputString;
}
//Decode inputString, store it in outputString, and return it
private String decode() throws InvalidCharacterException{
logger.debug("Decoding");
//Decode every binary number in the string
StringBuilder output = new StringBuilder();
for(String baseXString : inputString.split(" ")){
logger.debug("Current number {}", baseXString);
//Decode the current binary number
int num = Integer.valueOf(baseXString, base);
logger.debug("Decoded number {}", num);
//Make sure it is in a valid range
if((num < 0) && (num > 255)){
throw new InvalidCharacterException("The base" + base + " string '" + baseXString + "' is not a valid character");
@@ -89,6 +123,7 @@ public class BaseX{
}
//Save the output
logger.debug("Saving output string '{}'", output);
outputString = output.toString();
//Return the output
@@ -142,6 +177,8 @@ public class BaseX{
}
//Makes sure all of the variables are empty
public void reset(){
logger.debug("Resetting fields");
inputString = "";
outputString = "";
}

View File

@@ -1,21 +1,28 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Beaufort.java
//Mattrixwv
// Created: 02-23-22
//Modified: 02-23-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 Beaufort{
private static final Logger logger = LoggerFactory.getLogger(Beaufort.class);
//Fields
private String inputString; //This is the string that needs encoded/decoded
private String outputString; //This is the string that is output after encoding/decoding
private String keyword; //This is the keyword that is responsible for determining the offsets that you change each character by
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
//Internal ciphers
private Atbash atbash; //The first step in encoding/decoding the cipher
private Caesar caesar; //The second step in encoding/decoding the cipher
private Vigenere vigenere; //The third step in encoding/decoding the cipher
@@ -27,18 +34,27 @@ public class Beaufort{
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;
//Make sure the string isn't blank
@@ -53,11 +69,17 @@ public class Beaufort{
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
logger.debug("Removing all non-letters");
keyword = keyword.replaceAll("[^A-Z]", "");
//Save the string
logger.debug("Cleaned keyword '{}'", keyword);
this.keyword = keyword;
//If after all the elimination of unusable characters the keyword is empty throw an exception
@@ -67,19 +89,27 @@ public class Beaufort{
}
//Encodes the inputString and stores the result in outputString
public void encode() throws InvalidKeywordException, InvalidInputException{
logger.debug("Encoding");
//Reverse the string
logger.debug("Encoding with Atbash");
String atbashString = atbash.encode(inputString);
//Shift the reversal by 1
//?Not quite sure why this is needed. Need to look into this cipher a bit more closely
logger.debug("Shifting all letters by 1");
String caesarString = caesar.encode(1, atbashString);
//Shift each letter according to the key
logger.debug("Encoding with Vigenere");
String vigenereString = vigenere.encode(keyword, caesarString);
//Save the output
logger.debug("Saving output string '{}'", vigenereString);
this.outputString = vigenereString;
}
//Decodes the inputString and stores the result in outputString
public void decode() throws InvalidKeywordException, InvalidInputException{
logger.debug("Decoding");
//Decoding is just encoding again
encode();
}
@@ -138,6 +168,8 @@ public class Beaufort{
}
//Makes sure all of the variables are empty
public void reset(){
logger.debug("Resetting fields");
inputString = "";
outputString = "";
keyword = "";

View File

@@ -1,24 +1,35 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Caesar.java
//Matthew Ellison
// Created: 07-25-21
//Modified: 02-17-22
//Modified: 07-09-22
package com.mattrixwv.cipherstream.monosubstitution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
public class Caesar{
private static final Logger logger = LoggerFactory.getLogger(Caesar.class);
//Fields
private String inputString; //The string that needs encoded/decoded
private String outputString; //The encoded/decoded string
private int shift; //The amount that you need to shift each letter
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
//Sets shift and makes sure it is within the propper bounds
private void setShift(int shiftAmount){
logger.debug("Setting shift {}", shiftAmount);
//If you shift more than 26 you will just be wrapping back around again
shift = shiftAmount % 26;
logger.debug("Cleaned shift {}", shift);
}
//Sets the input string
private void setInputString(String inputString) throws InvalidInputException{
@@ -26,16 +37,25 @@ public class Caesar{
throw new NullPointerException("Input cannot be null");
}
logger.debug("Original input string '{}'", inputString);
if(!preserveCapitals){
logger.debug("Removing case");
inputString = inputString.toLowerCase();
}
if(!preserveWhitespace){
logger.debug("Removing whitespace");
inputString = inputString.replaceAll("\\s", "");
}
if(!preserveSymbols){
logger.debug("Removing symbols");
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
}
logger.debug("Cleaned input string '{}'", inputString);
this.inputString = inputString;
if(this.inputString.isBlank()){
@@ -44,63 +64,93 @@ public class Caesar{
}
//Encodes the inputString and stores the result in outputString
private String encode(){
logger.debug("Encoding");
StringBuilder output = new StringBuilder();
for(int cnt = 0;cnt < inputString.length();++cnt){
char currentChar = inputString.charAt(cnt); //A temperary holder for the current working character
logger.debug("Working character {}", currentChar);
//If it is an upper case letter shift it and wrap if necessary
if(Character.isUpperCase(currentChar)){
logger.debug("Encoding uppercase");
currentChar += shift;
//Wrap around if the letter is now out of bounds
if(currentChar < 'A'){
logger.debug("Wrapping around to Z");
currentChar += 26;
}
else if(currentChar > 'Z'){
logger.debug("Wrapping around to A");
currentChar -= 26;
}
}
//If it is a lower case letter shift it and wrap if necessary
else if(Character.isLowerCase(currentChar)){
logger.debug("Encoding lowercase");
currentChar += shift;
//Wrap around if the letter is now out of bounds
if(currentChar < 'a'){
logger.debug("Wrapping around to z");
currentChar += 26;
}
else if(currentChar > 'z'){
logger.debug("Wrapping around to a");
currentChar -= 26;
}
}
//If it is whitespace, number, or punctuation just let it pass through
//Add it to the output string
logger.debug("Encoded character {}", currentChar);
output.append(currentChar);
}
logger.debug("Saving encoded string '{}'", output);
outputString = output.toString();
return outputString;
}
//Decodes the inputString and stores the result in outputString
private String decode(){
logger.debug("Decoding");
StringBuilder output = new StringBuilder();
for(int cnt = 0;cnt < inputString.length();++cnt){
char currentChar = inputString.charAt(cnt); //A temperary holder for the current working character
logger.debug("Working character {}", currentChar);
//If it is an upper case letter shift it and wrap if necessary
if(Character.isUpperCase(currentChar)){
logger.debug("Decoding uppercase");
currentChar -= shift;
//Wrap around if the letter is now out of bounds
if(currentChar < 'A'){
logger.debug("Wrapping around to Z");
currentChar += 26;
}
else if(currentChar > 'Z'){
logger.debug("Wrapping around to A");
currentChar -= 26;
}
}
//If it is a lower case letter shift it and wrap if necessary
else if(Character.isLowerCase(currentChar)){
logger.debug("Decoding lowercase");
currentChar -= shift;
//Wrap around if the letter is now out of bounds
if(currentChar < 'a'){
logger.debug("Wrapping around to z");
currentChar += 26;
}
else if(currentChar > 'z'){
logger.debug("Wrapping around to a");
currentChar -= 26;
}
}
@@ -109,6 +159,7 @@ public class Caesar{
output.append(currentChar);
}
logger.debug("Saving decoded string '{}'", output);
outputString = output.toString();
return outputString;
}
@@ -126,18 +177,7 @@ public class Caesar{
this.preserveWhitespace = preserveWhitespace;
this.preserveSymbols = preserveSymbols;
}
//Returns the inputString
public String getInputString(){
return inputString;
}
//Returns shift
public int getShift(){
return shift;
}
//Returns the outputString
public String getOutputString(){
return outputString;
}
//Sets the shift and inputString and encodes the message
public String encode(int shiftAmount, String inputString) throws InvalidInputException{
reset();
@@ -152,9 +192,25 @@ public class Caesar{
setInputString(inputString);
return decode();
}
//Returns the inputString
public String getInputString(){
return inputString;
}
//Returns shift
public int getShift(){
return shift;
}
//Returns the outputString
public String getOutputString(){
return outputString;
}
//Makes sure all of the variables are empty
public void reset(){
inputString = outputString = "";
logger.debug("Resetting fields");
inputString = "";
outputString = "";
shift = 0;
}
}

View File

@@ -1,15 +1,20 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/OneTimePad.java
//Mattrixwv
// Created: 02-23-22
//Modified: 02-23-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 OneTimePad extends Vigenere{
private static final Logger logger = LoggerFactory.getLogger(OneTimePad.class);
//?Add some kind of entropy calculator?
//?Add some kind of "book passage includer"?
@@ -28,6 +33,9 @@ public class OneTimePad extends Vigenere{
if(keyword.length() < inputString.length()){
throw new InvalidKeywordException("Key must be at least as long as the input");
}
logger.debug("Encoding");
return super.encode(keyword, inputString);
}
//Decodes input using key and returns the result
@@ -36,6 +44,9 @@ public class OneTimePad extends Vigenere{
if(keyword.length() < inputString.length()){
throw new InvalidKeywordException("Key must be at least as long as the input");
}
logger.debug("Decoding");
return super.decode(keyword, inputString);
}
}

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 = "";

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 = "";

View File

@@ -1,18 +1,24 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Vigenere.java
//Matthew Ellison
// Created: 07-25-21
//Modified: 02-22-22
//Modified: 07-09-22
package com.mattrixwv.cipherstream.monosubstitution;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Vigenere{
private static final Logger logger = LoggerFactory.getLogger(Vigenere.class);
//Fields
protected String inputString; //This is the string that needs encoded/decoded
protected String outputString; //This is the string that is output after encoding/decoding
protected String keyword; //This is the keyword that is resposible for determining the offsets that you change each character by
@@ -23,6 +29,8 @@ public class Vigenere{
//Uses keyword to calculate the offset for the Caesar cipher for each character
protected void setOffset(){
logger.debug("Setting offset array from keyword");
//Reserve the correct size to increase speed later
offset.ensureCapacity(keyword.length());
@@ -31,6 +39,8 @@ public class Vigenere{
char letter = keyword.charAt(cnt);
offset.add((letter - 'A') % 26);
}
logger.debug("Offset {}", offset);
}
//Sets inputString
protected void setInputString(String inputString) throws InvalidInputException{
@@ -38,16 +48,25 @@ public class Vigenere{
throw new NullPointerException("Input cannot be null");
}
logger.debug("Original input string '{}'", inputString);
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]", "");
}
logger.debug("Cleaned input string '{}'", inputString);
this.inputString = inputString;
if(this.inputString.isBlank()){
@@ -60,11 +79,17 @@ public class Vigenere{
throw new NullPointerException("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
logger.debug("Removing all non-letter characters");
keyword = keyword.replaceAll("[^A-Z]", "");
//Save the string
logger.debug("Clean keyword '{}'", keyword);
this.keyword = keyword;
//Make sure offset is empty before adding to it
@@ -78,67 +103,113 @@ public class Vigenere{
}
//Encodes inputString and stores the result in outputString
protected String encode(){
logger.debug("Encoding");
StringBuilder output = new StringBuilder();
//Step through every character in the inputString and advance it the correct amount, according to offset
int offsetCnt = 0;
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
char letter = inputString.charAt(inputCnt);
logger.debug("Working character {}", letter);
if(Character.isUpperCase(letter)){
logger.debug("Encoding uppercase");
letter += offset.get((offsetCnt++) % offset.size());
//Make sure the character is still a letter, if not, wrap around
if(letter < 'A'){
logger.debug("Wrapping around to Z");
letter += 26;
}
else if(letter > 'Z'){
logger.debug("Wrapping around to A");
letter -= 26;
}
}
else if(Character.isLowerCase(letter)){
logger.debug("Encoding lowercase");
letter += offset.get((offsetCnt++) % offset.size());
//Make sure the character is still a letter, if not, wrap around
if(letter < 'a'){
logger.debug("Wrapping around to z");
letter += 26;
}
else if(letter > 'z'){
logger.debug("Wrapping around to a");
letter -= 26;
}
}
logger.debug("Encoded character {}", letter);
output.append(letter);
}
//Save output
logger.debug("Encoded message '{}'", output);
outputString = output.toString();
return outputString;
}
//Decodes inputString and stores the result in outputString
protected String decode(){
logger.debug("Decoding");
StringBuilder output = new StringBuilder();
//Step through every character in the inputString and advance it the correct amount, according to offset
int offsetCnt = 0;
for(int letterCnt = 0;letterCnt < inputString.length();++letterCnt){
char letter = inputString.charAt(letterCnt);
logger.debug("Working character {}", letter);
if(Character.isUpperCase(letter)){
logger.debug("Decoding uppercase");
letter -= offset.get((offsetCnt++) % offset.size());
if(letter < 'A'){
logger.debug("Wrapping around to Z");
letter += 26;
}
else if(letter > 'Z'){
logger.debug("Wrapping around to A");
letter -= 26;
}
}
else if(Character.isLowerCase(letter)){
logger.debug("Decoding lowercase");
letter -= offset.get((offsetCnt++) % offset.size());
if(letter < 'a'){
logger.debug("Wrapping around to z");
letter += 26;
}
else if(letter > 'z'){
logger.debug("Wrapping around to a");
letter -= 26;
}
}
//Add letter to output
logger.debug("Encoded letter {}", letter);
output.append(letter);
}
//Save output
logger.debug("Encoded message '{}'", output);
outputString = output.toString();
return outputString;
}
@@ -191,6 +262,8 @@ public class Vigenere{
}
//Makes sure all of the variables are empty
public void reset(){
logger.debug("Resetting fields");
inputString = "";
outputString = "";
keyword = "";

View File

@@ -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 = "";
}
}

View File

@@ -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 = "";

View File

@@ -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;

View File

@@ -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();

View File

@@ -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();

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;

View File

@@ -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){