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