Updating tests

This commit is contained in:
2023-05-05 20:19:27 -04:00
parent b3ca4754ea
commit 9c41f10576
45 changed files with 2729 additions and 2239 deletions

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGVX.java
//Mattrixwv
// Created: 01-26-22
//Modified: 04-21-23
//Modified: 05-04-23
package com.mattrixwv.cipherstream.combination;
@@ -16,16 +16,16 @@ import com.mattrixwv.cipherstream.polysubstitution.LargePolybiusSquare;
public class ADFGVX{
protected static Logger logger = LoggerFactory.getLogger(ADFGVX.class);
private static final Logger logger = LoggerFactory.getLogger(ADFGVX.class);
//Fields
protected boolean preserveCapitals; //Whether to respect capitals in the output string
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
protected boolean preserveSymbols; //Whether to respect symbols in the output string
protected String inputString; //The string that needs encoded/decoded
protected String outputString; //The string that is output after encoding/decoding
protected String squareKeyword; //The keyword used in the Polybius Square
protected String keyword; //The Keyword used in the Columnar cipher
protected String inputString; //The string that needs encoded/decoded
protected String outputString; //The string that is output after encoding/decoding
protected String squareKeyword; //The keyword used in the Polybius Square
protected String keyword; //The Keyword used in the Columnar cipher
//Settings
protected boolean preserveCapitals; //Persist capitals in the output string
protected boolean preserveWhitespace; //Persist whitespace in the output string
protected boolean preserveSymbols; //Persist symbols in the output string
//Internal ciphers
protected LargePolybiusSquare largePolybiusSquare; //The first step in encoding
protected Columnar columnar; //The second step in encoding
@@ -37,7 +37,7 @@ public class ADFGVX{
throw new InvalidKeywordException("Square Keyword cannot be null");
}
logger.debug("squareKeyword = {}", squareKeyword);
logger.debug("squareKeyword '{}'", squareKeyword);
this.squareKeyword = squareKeyword;
}
//Ensures Columnar keyword constraints
@@ -46,7 +46,7 @@ public class ADFGVX{
throw new InvalidKeywordException("Keyword cannot be null");
}
logger.debug("keyword = {}", keyword);
logger.debug("keyword '{}'", keyword);
this.keyword = keyword;
}
//Ensures inputString constraints
@@ -210,23 +210,20 @@ public class ADFGVX{
return decode();
}
//Returns the cleaned inputString
//Getters
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
//Makes sure all variables are empty
public void reset() throws InvalidCharacterException{
logger.debug("Resetting fields");

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGX.java
//Mattrixwv
// Created: 01-25-22
//Modified: 04-14-23
//Modified: 05-04-23
package com.mattrixwv.cipherstream.combination;
@@ -16,16 +16,17 @@ import com.mattrixwv.cipherstream.polysubstitution.PolybiusSquare;
public class ADFGX{
protected static Logger logger = LoggerFactory.getLogger(ADFGX.class);
private static final Logger logger = LoggerFactory.getLogger(ADFGX.class);
//Internal fields
protected String inputString; //The string that needs encoded/decoded
protected String inputString; //The string that needs encoded/decoded
protected String outputString; //The string that is output after encoding/decoding
protected String squareKeyword; //The keyword used in the Polybius Square
protected String keyword; //The keyword used in the Columnar cipher
protected boolean preserveCapitals; //Whether to respect capitals in the output string
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
protected boolean preserveSymbols; //Whether to respect symbols in the output string
protected String keyword; //The keyword used in the Columnar cipher
//Settings
protected boolean preserveCapitals; //Persist capitals in the output string
protected boolean preserveWhitespace; //Persist whitespace in the output string
protected boolean preserveSymbols; //Persist symbols in the output string
//Internal ciphers
protected PolybiusSquare polybiusSquare; //The first step in encoding
protected Columnar columnar; //The second step in encoding
@@ -37,7 +38,7 @@ public class ADFGX{
throw new InvalidKeywordException("Square keyword cannot be null");
}
logger.debug("squareKeyword = {}", squareKeyword);
logger.debug("Square keyword '{}'", squareKeyword);
this.squareKeyword = squareKeyword;
}
//Ensures Columnar keyword constraints
@@ -46,7 +47,7 @@ public class ADFGX{
throw new InvalidKeywordException("Keyword cannot be null");
}
logger.debug("keyword = {}", keyword);
logger.debug("Keyword '{}'", keyword);
this.keyword = keyword;
}
//Ensures inputString constraints
@@ -74,10 +75,10 @@ public class ADFGX{
this.inputString = inputString;
logger.debug("Cleaned input string '{}'", inputString);
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
protected void formatOutputStringEncode(){
@@ -209,23 +210,20 @@ public class ADFGX{
return outputString;
}
//Returns the cleaned inputString
//Getters
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
//Makes sure all variables are empty
public void reset() throws InvalidCharacterException{
logger.debug("Resetting fields");

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Affine.java
//Mattrixwv
// Created: 01-26-22
//Modified: 04-15-23
//Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -15,16 +15,17 @@ import com.mattrixwv.NumberAlgorithms;
public class Affine{
protected static Logger logger = LoggerFactory.getLogger(Affine.class);
private static final Logger logger = LoggerFactory.getLogger(Affine.class);
//Fields
protected boolean preserveCapitals; //Whether to respect capitals in the output string
protected boolean preserveSymbols; //Whether to respect symbols in the output string
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
protected String inputString; //The string that needs encoded/decoded
protected String inputString; //The string that needs encoded/decoded
protected String outputString; //The string that is output after encoding/decoding
protected int key1; //The multiplicative key. Key1 must be relatively prime to 26
protected int key2; //The additive key
//Settings
protected boolean preserveCapitals; //Persist capitals in the output string
protected boolean preserveSymbols; //Persist symbols in the output string
protected boolean preserveWhitespace; //Persist whitespace in the output string
//Ensures key1 constraints
protected void setKey1(int key1) throws InvalidKeywordException{
@@ -225,19 +226,16 @@ public class Affine{
return outputString;
}
//Returns the cleaned inputString
//Getters
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;
}

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Atbash.java
//Mattrixwv
// Created: 07-25-21
//Modified: 04-15-23
//Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -12,17 +12,18 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
public class Atbash{
protected static Logger logger = LoggerFactory.getLogger(Atbash.class);
private static final Logger logger = LoggerFactory.getLogger(Atbash.class);
//Fields
protected String inputString; //Holds the string that needs encoded or decoded
protected String outputString; //The encoded/decoded string
protected boolean preserveCapitals; //Whether to respect capitals in the output string
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
protected boolean preserveSymbols; //Whether to respect symbols in the output string
//Settings
protected boolean preserveCapitals; //Persist capitals in the output string
protected boolean preserveWhitespace; //Persist whitespace in the output string
protected boolean preserveSymbols; //Persist symbols in the output string
//Encodes inputString and stores in outputString
protected String encode(){
protected void encode(){
logger.debug("Encoding");
StringBuilder output = new StringBuilder();
//Step through every element in the inputString and shift it the correct amount
@@ -34,22 +35,27 @@ public class Atbash{
//Use either uppercase or lowercase for the base
//(letterbase + 25 - (currentChar - letterBase))
if(Character.isUpperCase(currentChar)){
logger.debug("Encoding uppercase");
output.append((char)(155 - currentChar));
}
else{
logger.debug("Encoding lowercase");
output.append((char)(219 - currentChar));
}
}
//Keep any punctuatio/whitespace the way it is
else{
logger.debug("Appending symbol");
output.append(currentChar);
}
}
//Return the output
logger.debug("Saving output string '{}'", output);
outputString = output.toString();
return outputString;
logger.debug("Saving output string '{}'", outputString);
}
//Removes all invalid characters and sets inputString
protected void setInputString(String inputString) throws InvalidInputException{
@@ -108,22 +114,22 @@ public class Atbash{
//Make sure everything is empty before you begin
reset();
setInputString(inputString);
return encode();
encode();
return outputString;
}
//Decodes inputString and returns the result
public String decode(String inputString) throws InvalidInputException{
return encode(inputString);
}
//Returns the cleaned input string
//Getters
public String getInputString(){
return inputString;
}
//Returns the output string
public String getOutputString(){
return outputString;
}
//Makes sure all of the variables are empty
//Makes sure all variables are empty
public void reset(){
logger.debug("Resetting fields");

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Autokey.java
//Mattrixwv
// Created: 07-25-21
//Modified: 04-15-23
//Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -13,7 +13,8 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Autokey extends Vigenere{
protected static Logger logger = LoggerFactory.getLogger(Autokey.class);
private static final Logger logger = LoggerFactory.getLogger(Autokey.class);
//Special rules for setting the strings for encoding
protected void encodeSet(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
@@ -57,7 +58,7 @@ public class Autokey extends Vigenere{
}
//Decodes the inputString
@Override
protected String decode(){
protected void decode(){
logger.debug("Decoding");
//Decode what the key will allow, add that to the key and continue
@@ -109,7 +110,6 @@ public class Autokey extends Vigenere{
//Save and return the results
outputString = fullOutput.toString();
logger.debug("Saving output string '{}'", outputString);
return outputString;
}
@@ -125,13 +125,15 @@ public class Autokey extends Vigenere{
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
reset();
encodeSet(keyword, inputString);
return encode();
encode();
return outputString;
}
//Decodes inputString using the Autokey cipher
@Override
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
reset();
decodeSet(keyword, inputString);
return decode();
decode();
return outputString;
}
}

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/Baconian.java
//Mattrixwv
// Created: 01-12-22
//Modified: 04-15-23
//Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -17,7 +17,7 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
public class Baconian{
protected static Logger logger = LoggerFactory.getLogger(Baconian.class);
private static final Logger logger = LoggerFactory.getLogger(Baconian.class);
//Conversions
protected static final ArrayList<String> code = new ArrayList<>(Arrays.asList(
@@ -25,8 +25,9 @@ public class Baconian{
"abbaa", "abbab", "abbba", "abbbb", "baaaa", "baaab", "baaba", "baabb", "baabb", "babaa", "babab", "babba", "babbb" //N-Z
));
protected String inputString; //The string that needs encoded/decoded
protected String outputString; //The encoded/decoded string
protected boolean preserveCapitals; //Whether to respect capitals in the output string
protected String outputString; //The encoded/decoded string
protected boolean preserveCapitals; //Persist capitals in the output string
//Sets the input string
protected void setInputStringEncode(String inputString) throws InvalidInputException{
@@ -121,7 +122,7 @@ public class Baconian{
logger.debug("Saving output string '{}'", outputString);
}
//Decodes the inputString and stores the result in outputString
protected String decode(){
protected void decode(){
logger.debug("Decoding");
StringBuilder output = new StringBuilder();
@@ -154,7 +155,6 @@ public class Baconian{
//Save and return the output
outputString = output.toString();
logger.debug("Saving output string '{}'", outputString);
return outputString;
}
//Constructor
@@ -166,14 +166,7 @@ public class Baconian{
reset();
this.preserveCapitals = preserveCapitals;
}
//Returns the outputString
public String getOutputString(){
return outputString;
}
//Returns the inputString
public String getInputString(){
return inputString;
}
//Sets the inputString and encodes the message
public String encode(String inputString) throws InvalidInputException{
reset();
@@ -185,9 +178,18 @@ public class Baconian{
public String decode(String inputString) throws InvalidCharacterException, InvalidInputException{
reset();
setInputStringDecode(inputString);
return decode();
decode();
return outputString;
}
//Makes sure all of the variables are empty
//Getters
public String getInputString(){
return inputString;
}
public String getOutputString(){
return outputString;
}
//Makes sure all variables are empty
public void reset(){
logger.debug("Resetting fields");

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/BaseX.java
//Mattrixwv
// Created: 01-08-22
//Modified: 04-16-23
//Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -16,12 +16,14 @@ import com.mattrixwv.cipherstream.exceptions.InvalidBaseException;
public class BaseX{
protected static Logger logger = LoggerFactory.getLogger(BaseX.class);
private static final Logger logger = LoggerFactory.getLogger(BaseX.class);
//Fields
protected String inputString; //The string that needs encoded/decoded
protected String outputString; //The encoded/decoded string
//Settings
protected int base; //The base that the number will be encoded at
//Sets the input string
protected void setInputStringEncode(String inputString) throws InvalidInputException{
if(inputString == null){
@@ -80,7 +82,7 @@ public class BaseX{
this.base = base;
}
//Encode inputString, store it in outputString, and return it
protected String encode(){
protected void encode(){
logger.debug("Encoding");
//Encode every character in inputString
@@ -99,12 +101,9 @@ public class BaseX{
//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
protected String decode() throws InvalidCharacterException{
protected void decode() throws InvalidCharacterException{
logger.debug("Decoding");
//Decode every binary number in the string
@@ -128,9 +127,6 @@ public class BaseX{
//Save the output
outputString = output.toString();
logger.debug("Saving output string '{}'", outputString);
//Return the output
return outputString;
}
//Constructor
@@ -142,43 +138,47 @@ public class BaseX{
reset();
setBase(base);
}
//Returns the inputString
public String getInputString(){
return inputString;
}
//Returns the outputString
public String getOutputString(){
return outputString;
}
//Returns the base
public int getBase(){
return base;
}
//Sets the inputString and encodes the message
public String encode(String inputString) throws InvalidInputException{
reset();
setInputStringEncode(inputString);
return encode();
encode();
return outputString;
}
public String encode(int base, String inputString) throws InvalidBaseException, InvalidInputException{
reset();
setBase(base);
setInputStringEncode(inputString);
return encode();
encode();
return outputString;
}
//Sets the inputString and decodes the message
public String decode(String inputString) throws InvalidCharacterException, InvalidInputException{
reset();
setInputStringDecode(inputString);
return decode();
decode();
return outputString;
}
public String decode(int base, String inputString) throws InvalidBaseException, InvalidCharacterException, InvalidInputException{
reset();
setBase(base);
setInputStringDecode(inputString);
return decode();
decode();
return outputString;
}
//Makes sure all of the variables are empty
//Getters
public String getInputString(){
return inputString;
}
public String getOutputString(){
return outputString;
}
public int getBase(){
return base;
}
//Makes sure all variables are empty
public void reset(){
logger.debug("Resetting fields");

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Beaufort.java
//Mattrixwv
// Created: 02-23-22
//Modified: 04-16-23
//Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -13,20 +13,21 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Beaufort{
protected static Logger logger = LoggerFactory.getLogger(Beaufort.class);
private static final Logger logger = LoggerFactory.getLogger(Beaufort.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 responsible for determining the offsets that you change each character by
protected boolean preserveCapitals; //Whether to respect capitals in the output string
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
protected boolean preserveSymbols; //Whether to respect symbols in the output string
//Settings
protected boolean preserveCapitals; //Persist capitals in the output string
protected boolean preserveWhitespace; //Persist whitespace in the output string
protected boolean preserveSymbols; //Persist symbols in the output string
//Internal ciphers
protected Atbash atbash; //The first step in encoding/decoding the cipher
protected Caesar caesar; //The second step in encoding/decoding the cipher
protected Vigenere vigenere; //The third step in encoding/decoding the cipher
//Ensures inputString constraints
public void setInputString(String inputString) throws InvalidInputException{
//Make sure the input isn't null
@@ -138,18 +139,7 @@ public class Beaufort{
vigenere = new Vigenere(preserveCapitals, preserveWhitespace, preserveSymbols);
reset();
}
//Returns the current inputString
public String getInputString(){
return inputString;
}
//Returns the current outputString
public String getOutputString(){
return outputString;
}
//Returns the current keyword
public String getKeyword(){
return keyword;
}
//Encodes inputString using keyword and returns the result
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
//Set the parameters
@@ -170,7 +160,18 @@ public class Beaufort{
decode();
return outputString;
}
//Makes sure all of the variables are empty
//Getters
public String getInputString(){
return inputString;
}
public String getOutputString(){
return outputString;
}
public String getKeyword(){
return keyword;
}
//Makes sure all variables are empty
public void reset(){
logger.debug("Resetting fields");

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Caesar.java
//Matthew Ellison
// Created: 07-25-21
//Modified: 04-16-23
//Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -12,15 +12,16 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
public class Caesar{
protected static Logger logger = LoggerFactory.getLogger(Caesar.class);
private static final Logger logger = LoggerFactory.getLogger(Caesar.class);
//Fields
protected String inputString; //The string that needs encoded/decoded
protected String outputString; //The encoded/decoded string
protected int shift; //The amount that you need to shift each letter
protected boolean preserveCapitals; //Whether to respect capitals in the output string
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
protected boolean preserveSymbols; //Whether to respect symbols in the output string
//Settings
protected boolean preserveCapitals; //Persist capitals in the output string
protected boolean preserveWhitespace; //Persist whitespace in the output string
protected boolean preserveSymbols; //Persist symbols in the output string
//Sets shift and makes sure it is within the propper bounds
protected void setShift(int shiftAmount){
@@ -63,7 +64,7 @@ public class Caesar{
}
}
//Encodes the inputString and stores the result in outputString
protected String encode(){
protected void encode(){
logger.debug("Encoding");
StringBuilder output = new StringBuilder();
@@ -109,10 +110,9 @@ public class Caesar{
outputString = output.toString();
logger.debug("Saving encoded string '{}'", outputString);
return outputString;
}
//Decodes the inputString and stores the result in outputString
protected String decode(){
protected void decode(){
logger.debug("Decoding");
StringBuilder output = new StringBuilder();
@@ -162,7 +162,6 @@ public class Caesar{
outputString = output.toString();
logger.debug("Saving decoded string '{}'", outputString);
return outputString;
}
//Constructor
@@ -184,29 +183,29 @@ public class Caesar{
reset();
setShift(shiftAmount);
setInputString(inputString);
return encode();
encode();
return outputString;
}
//Sets the shift and inputString and decodes the message
public String decode(int shiftAmount, String inputString) throws InvalidInputException{
reset();
setShift(shiftAmount);
setInputString(inputString);
return decode();
decode();
return outputString;
}
//Returns the inputString
//Getters
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
//Makes sure all variables are empty
public void reset(){
logger.debug("Resetting fields");

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/OneTimePad.java
//Mattrixwv
// Created: 02-23-22
//Modified: 07-09-22
//Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -13,10 +13,9 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class OneTimePad extends Vigenere{
protected static Logger logger = LoggerFactory.getLogger(OneTimePad.class);
private static final Logger logger = LoggerFactory.getLogger(OneTimePad.class);
//?Add some kind of entropy calculator?
//?Add some kind of "book passage includer"?
//Constructor

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Porta.java
//Mattrixwv
// Created: 02-28-22
//Modified: 04-17-23
//Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -13,7 +13,7 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Porta{
protected static Logger logger = LoggerFactory.getLogger(Porta.class);
private static final Logger logger = LoggerFactory.getLogger(Porta.class);
private static final String[] tableau = {
"NOPQRSTUVWXYZABCDEFGHIJKLM", //A-B
@@ -32,12 +32,14 @@ public class Porta{
};
//Fields
protected String inputString; //The string that needs encoded/decoded
protected String inputString; //The string that needs encoded/decoded
protected String outputString; //The encoded/decoded string
protected String keyword; //The keyword used to encode the input string
protected boolean preserveCapitals; //Whether to respect capitals in the output string
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
protected boolean preserveSymbols; //Whether to respect symbols in the output string
//Settings
protected boolean preserveCapitals; //Persist capitals in the output string
protected boolean preserveWhitespace; //Persist whitespace in the output string
protected boolean preserveSymbols; //Persist symbols in the output string
//Ensure all keyword constraints are followed
protected void setKeyword(String keyword) throws InvalidKeywordException{
@@ -210,19 +212,17 @@ public class Porta{
return outputString;
}
//Returns the inputString
//Getters
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
//Makes sure all fields are empty
public void reset(){
logger.debug("Resetting fields");

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Substitution.java
//Mattrixwv
// Created: 02-22-22
//Modified: 04-18-23
//Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -13,15 +13,16 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Substitution{
protected static Logger logger = LoggerFactory.getLogger(Substitution.class);
private static final Logger logger = LoggerFactory.getLogger(Substitution.class);
//Fields
protected String inputString; //The string that needs encoded/decoded
protected String outputString; //The encoded/decoded string
protected String keyword; //The keyword used to encode/decode the input
protected boolean preserveCapitals; //Whether to respect capitals in the output string
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
protected boolean preserveSymbols; //Whether to respect symbols in the output string
//Getters
protected boolean preserveCapitals; //Persist capitals in the output string
protected boolean preserveWhitespace; //Persist whitespace in the output string
protected boolean preserveSymbols; //Persist symbols in the output string
//Ensures key constraints are followed
protected void setKeyword(String key) throws InvalidKeywordException{
@@ -172,6 +173,7 @@ public class Substitution{
logger.debug("Decoded message '{}'", outputString);
}
//Constructors
public Substitution(){
preserveCapitals = false;
preserveWhitespace = false;
@@ -184,6 +186,23 @@ public class Substitution{
this.preserveSymbols = preserveSymbols;
reset();
}
//Encodes inputString using the provided key
public String encode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{
setKeyword(key);
setInputString(inputString);
encode();
return outputString;
}
//Decodes inputString using the provided key
public String decode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{
setKeyword(key);
setInputString(inputString);
decode();
return outputString;
}
//Getters
public String getInputString(){
return inputString;
}
@@ -193,18 +212,7 @@ public class Substitution{
public String getKeyword(){
return keyword;
}
public String encode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{
setKeyword(key);
setInputString(inputString);
encode();
return outputString;
}
public String decode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{
setKeyword(key);
setInputString(inputString);
decode();
return outputString;
}
//Makes sure all variables are empty
public void reset(){
logger.debug("Resetting fields");

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Vigenere.java
//Matthew Ellison
// Created: 07-25-21
//Modified: 04-18-23
//Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -16,16 +16,17 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Vigenere{
protected static Logger logger = LoggerFactory.getLogger(Vigenere.class);
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
protected ArrayList<Integer> offset; //Holds the offsets coputed from each character in the keyword
protected boolean preserveCapitals; //Whether to respect capitals in the output string
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
protected boolean preserveSymbols; //Whether to respect symbols in the output string
//Settings
protected boolean preserveCapitals; //Persist capitals in the output string
protected boolean preserveWhitespace; //Persist whitespace in the output string
protected boolean preserveSymbols; //Persist symbols in the output string
//Uses keyword to calculate the offset for the Caesar cipher for each character
protected void setOffset(){
@@ -102,7 +103,7 @@ public class Vigenere{
}
}
//Encodes inputString and stores the result in outputString
protected String encode(){
protected void encode(){
logger.debug("Encoding");
StringBuilder output = new StringBuilder();
@@ -145,10 +146,9 @@ public class Vigenere{
//Save output
outputString = output.toString();
logger.debug("Encoded message '{}'", outputString);
return outputString;
}
//Decodes inputString and stores the result in outputString
protected String decode(){
protected void decode(){
logger.debug("Decoding");
StringBuilder output = new StringBuilder();
@@ -191,7 +191,6 @@ public class Vigenere{
//Save output
outputString = output.toString();
logger.debug("Decoded message '{}'", outputString);
return outputString;
}
@@ -210,37 +209,39 @@ public class Vigenere{
this.preserveWhitespace = preserveWhitespace;
this.preserveSymbols = preserveSymbols;
}
//Returns the current inputString
public String getInputString(){
return inputString;
}
//Returns the current outputString
public String getOutputString(){
return outputString;
}
//Returns the current keyword
public String getKeyword(){
return keyword;
}
//Returns the current offsets (Used mostly in bug fixing)
public List<Integer> getOffsets(){
return offset;
}
//Encodes input using key and returns the result
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
reset();
setKeyword(keyword);
setInputString(inputString);
return encode();
encode();
return outputString;
}
//Decodes input using key and returns the result
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
reset();
setKeyword(keyword);
setInputString(inputString);
return decode();
decode();
return outputString;
}
//Makes sure all of the variables are empty
//Getters
public String getInputString(){
return inputString;
}
public String getOutputString(){
return outputString;
}
public String getKeyword(){
return keyword;
}
public List<Integer> getOffsets(){
return offset;
}
//Makes sure all variables are empty
public void reset(){
logger.debug("Resetting fields");

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Bifid.java
//Mattrixwv
// Created: 03-03-22
//Modified: 04-23-23
//Modified: 05-04-23
package com.mattrixwv.cipherstream.polysubstitution;
@@ -14,16 +14,17 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Bifid{
protected static Logger logger = LoggerFactory.getLogger(Bifid.class);
private static final Logger logger = LoggerFactory.getLogger(Bifid.class);
//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
protected PolybiusSquare polybiusSquare; //Used to encode the message to numbers then back into letters
protected boolean preserveCapitals; //Persist capitals in the output string
//Settings
protected boolean preserveCapitals; //Persist capitals in the output string
protected boolean preserveWhitespace; //Persist whitespace in the output string
protected boolean preserveSymbols; //Persist symbols in the output string
protected boolean preserveSymbols; //Persist symbols in the output string
//Internal ciphers
protected PolybiusSquare polybiusSquare; //Used to encode the message to numbers then back into letters
//Strips invalid characters from the keyword and creates the grid
@@ -169,7 +170,7 @@ public class Bifid{
}
//Constructor
//Constructors
public Bifid() throws InvalidCharacterException{
preserveCapitals = false;
preserveWhitespace = false;
@@ -184,6 +185,7 @@ public class Bifid{
polybiusSquare = new PolybiusSquare(false, false);
reset();
}
//Encodes inputString using keyword and returns the result
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
//Set the parameters
@@ -207,7 +209,7 @@ public class Bifid{
return outputString;
}
//Gets
//Getters
public String getInputString(){
return inputString;
}
@@ -217,6 +219,7 @@ public class Bifid{
public String getKeyword(){
return keyword;
}
//Makes sure all variables are empty
public void reset(){
logger.debug("Resetting fields");

View File

@@ -1,7 +1,7 @@
//MattrixwvWebsite/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Columnar.java
//Mattrixwv
// Created: 01-16-22
//Modified: 04-26-23
//Modified: 05-04-23
package com.mattrixwv.cipherstream.polysubstitution;
@@ -18,19 +18,20 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Columnar{
protected static Logger logger = LoggerFactory.getLogger(Columnar.class);
private static final Logger logger = LoggerFactory.getLogger(Columnar.class);
//Fields
protected String inputString; //The message that needs to be encoded/decoded
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
protected String keyword; //The keyword used to create the grid
protected char characterToAdd; //The character that is added to the end of a string to bring it to the correct length
protected int charsAdded; //The number of characters that were added to the end of the message
protected int charsAdded; //The number of characters that were added to the end of the message
protected ArrayList<ArrayList<Character>> grid; //The grid used to encode/decode the message
protected boolean preserveCapitals; //Persist capitals in the output string
//Settings
protected boolean preserveCapitals; //Persist capitals in the output string
protected boolean preserveWhitespace; //Persist whitespace in the output string
protected boolean preserveSymbols; //Persist symbols in the output string
protected boolean removePadding; //Remove the padding letters added to the cipher
protected boolean preserveSymbols; //Persist symbols in the output string
protected boolean removePadding; //Remove the padding letters added to the cipher
//Strip the inputString of all non-letter characters and change them to capitals
protected String getCleanInputString(){
@@ -467,7 +468,7 @@ public class Columnar{
createOutputStringFromRows();
}
//Constructor
//Constructors
public Columnar() throws InvalidCharacterException{
preserveCapitals = false;
preserveWhitespace = false;
@@ -492,6 +493,7 @@ public class Columnar{
setCharacterToAdd(characterToAdd);
reset();
}
//Encodes inputString using keyword and returns the result
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
//Set the parameters
@@ -525,7 +527,8 @@ public class Columnar{
grid = new ArrayList<>();
charsAdded = 0;
}
//Gets
//Getters
public String getInputString(){
return inputString;
}

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Hill.java
//Mattrixwv
// Created: 01-31-22
//Modified: 04-27-23
//Modified: 05-04-23
package com.mattrixwv.cipherstream.polysubstitution;
@@ -19,17 +19,19 @@ import com.mattrixwv.matrix.exceptions.InvalidScalarException;
public class Hill{
protected static Logger logger = LoggerFactory.getLogger(Hill.class);
private static final Logger logger = LoggerFactory.getLogger(Hill.class);
//Fields
protected String inputString; //The message that needs to be encoded/decoded
protected String outputString; //The encoded/decoded message
protected char characterToAdd; //The keyword used to create the grid
protected ModMatrix key; //The matrix used perform the encoding/decoding
//Settings
protected boolean preserveCapitals; //Persist capitals in the output string
protected boolean preserveWhitespace; //Persist whitespace in the output string
protected boolean preserveSymbols; //Persist symbols in the output string
protected boolean preserveCapitals;
protected boolean preserveWhitespace;
protected boolean preserveSymbols;
protected String inputString;
protected String outputString;
protected char characterToAdd;
protected ModMatrix key;
//Validate and set the key
protected void setKey(ModMatrix key) throws InvalidKeyException{
logger.debug("Setting key");
@@ -55,9 +57,10 @@ public class Hill{
}
//Set the key
logger.debug("key = {}", key);
logger.debug("key\n{}", key);
this.key = new ModMatrix(key);
}
//Validate and set the input string
protected void setInputStringEncode(String inputString) throws InvalidInputException{
logger.debug("Setting input string for encoding");
@@ -142,6 +145,7 @@ public class Hill{
throw new InvalidInputException("Length of input string must be a multiple of the number of rows in the key");
}
}
//Get a perfectly clean copy of input string
protected String getCleanInputString(){
logger.debug("Cleaning inputString");
@@ -150,6 +154,7 @@ public class Hill{
return cleanInputString;
}
//Validate and set character to add
protected void setCharacterToAdd(char characterToAdd) throws InvalidCharacterException{
logger.debug("Setting character to add {}", characterToAdd);
@@ -168,6 +173,7 @@ public class Hill{
logger.debug("Cleaned character {}", characterToAdd);
this.characterToAdd = characterToAdd;
}
//Add capitalization, whitespace, and symbols to the output based on the input
protected String polishOutputString(){
logger.debug("Polishing output string");
@@ -197,6 +203,7 @@ public class Hill{
logger.debug("Polished string '{}'", cleanString);
return cleanString;
}
//Get vectors representing the input string
protected ArrayList<ModMatrix> getInputVectors(){
logger.debug("Generating input vectors");
@@ -230,6 +237,7 @@ public class Hill{
//Return the array of vectors
return vectors;
}
//Get the string represented by the vectors that were passed in
protected String getOutputFromVectors(ArrayList<ModMatrix> outputVectors){
logger.debug("Turning vectors into a string");
@@ -249,6 +257,7 @@ public class Hill{
logger.debug("Converted string '{}'", convertedString);
return convertedString;
}
//Encode inputString and store the value in outputString
protected void encode(){
logger.debug("Encoding");
@@ -272,6 +281,7 @@ public class Hill{
//Add the extra characters back to the output and remove the added characters
outputString = polishOutputString();
}
//Decode inputString and store the value in outputString
protected void decode(){
logger.debug("Decoding");
@@ -298,6 +308,7 @@ public class Hill{
outputString = polishOutputString();
}
//Constructors
public Hill() throws InvalidCharacterException{
preserveCapitals = false;
preserveWhitespace = false;
@@ -320,6 +331,7 @@ public class Hill{
reset();
}
//Encode inputString using the provided key
public String encode(int[][] key, String inputString) throws InvalidKeyException, InvalidInputException{
return encode(new ModMatrix(key, 26), inputString);
}
@@ -329,6 +341,7 @@ public class Hill{
encode();
return outputString;
}
//Decode inputString using the provided key
public String decode(int[][] key, String inputString) throws InvalidKeyException, InvalidInputException{
return decode(new ModMatrix(key, 26), inputString);
}
@@ -338,6 +351,8 @@ public class Hill{
decode();
return outputString;
}
//Makes sure all variables are empty
public void reset(){
logger.debug("Resetting fields");
@@ -345,6 +360,8 @@ public class Hill{
outputString = "";
key = new ModMatrix(26);
}
//Getters
public String getInputString(){
return inputString;
}

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/polysubstitution/LargePolybiusSquare.java
//Mattrixwv
// Created: 04-21-23
// Modified: 04-21-23
// Modified: 05-04-23
package com.mattrixwv.cipherstream.polysubstitution;
@@ -16,9 +16,10 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class LargePolybiusSquare extends PolybiusSquare{
protected static Logger logger = LoggerFactory.getLogger(LargePolybiusSquare.class);
private static final Logger logger = LoggerFactory.getLogger(LargePolybiusSquare.class);
//Create the grid from the keyword
@Override
protected void createGrid(){
logger.debug("Creating grid");
@@ -30,6 +31,7 @@ public class LargePolybiusSquare extends PolybiusSquare{
}
}
}
//Strips invalid characters from the string that needs encoded/decoded
@Override
protected void setInputStringEncode(String inputString) throws InvalidCharacterException, InvalidInputException{
if(inputString == null){
@@ -73,6 +75,7 @@ public class LargePolybiusSquare extends PolybiusSquare{
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
//Returns the input string ready for encoding
@Override
protected String getPreparedInputStringEncode(){
logger.debug("Preparing input string for encoding");
@@ -84,6 +87,7 @@ public class LargePolybiusSquare extends PolybiusSquare{
return cleanString;
}
//Strips invalid characters from the keyword and creates the grid
@Override
protected void setKeyword(String keyword) throws InvalidKeywordException{
if(keyword == null){
@@ -112,6 +116,7 @@ public class LargePolybiusSquare extends PolybiusSquare{
//Create the grid from the sanitized keyword
createGrid();
}
//Adds characters that aren't letters to the output
@Override
protected void addCharactersToCleanStringEncode(String cleanString){
logger.debug("Formatting output string");
@@ -166,6 +171,8 @@ public class LargePolybiusSquare extends PolybiusSquare{
outputString = fullOutput.toString();
logger.debug("Saving output string {}", outputString);
}
//Makes sure all variables are empty
@Override
public void reset(){
logger.debug("Resetting");
@@ -176,6 +183,7 @@ public class LargePolybiusSquare extends PolybiusSquare{
keyword = "";
}
//Constructors
public LargePolybiusSquare() throws InvalidCharacterException{
super();
}

View File

@@ -1,94 +1,151 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Morse.java
//Matthew Ellison
//Mattrixwv
// Created: 07-28-21
//Modified: 01-16-22
//Modified: 05-04-23
package com.mattrixwv.cipherstream.polysubstitution;
import java.util.HashMap;
import java.util.Map;
import java.util.StringJoiner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
public class Morse{
//TODO: This needs updated to match new standards
//Holds the Morse representation of the alphanumeric characters
private static final String[] code = {
private static final Logger logger = LoggerFactory.getLogger(Morse.class);
//Code representations
private static final String[] letters = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", //A-L
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", //M-Z
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." //M-Z
};
private static final String[] numbers = {
"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----." //0-9
};
private String inputString; //The string that needs encoded/decoded
private String outputString; //The encoded/decoded message
private static final Map<String, Character> map; //The map to help convert from Morse to letters and numbers
static{
//Setup the map
map = new HashMap<>();
for(int cnt = 0;cnt < letters.length;++cnt){
String letter = letters[cnt];
map.put(letter, (char)('A' + cnt));
}
for(int cnt = 0;cnt < numbers.length;++cnt){
String number = numbers[cnt];
map.put(number, (char)('0' + cnt));
}
}
//Fields
protected String inputString; //The string that needs encoded/decoded
protected String outputString; //The encoded/decoded message
//Encodes inputString and stores the result in outputString
private String encode(){
StringBuilder output = new StringBuilder();
//Loop through every element in the input string and see what type it is
//Validate and set the input string for encoding
protected void setInputStringEncode(String inputString){
logger.debug("Setting input string for encoding '{}'", inputString);
//Check for null
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
}
//Convert all letters to uppercase
logger.debug("Removing case");
inputString = inputString.toUpperCase();
//Remove all except alpha-numeric characters
logger.debug("Removing whitespace and symbols");
inputString = inputString.replaceAll("[^A-Z0-9]", "");
logger.debug("Cleaned input string '{}'", inputString);
//Check for a blank input
if(inputString.isBlank()){
throw new InvalidInputException("Input must contain at least 1 letter");
}
//Save the input
this.inputString = inputString;
}
//Validate and set the input string for decoding
protected void setInputStringDecode(String inputString){
logger.debug("Setting input string for decoding '{}'", inputString);
//Check for null
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
}
//Remove all non-morse code characters and check if there is a difference
logger.debug("Checking for invalid characters");
if(!inputString.equals(inputString.replaceAll("[^ \\.\\-]", ""))){
throw new InvalidInputException("Invalid Morse characters found");
}
//Check for blank input
if(inputString.isBlank()){
throw new InvalidInputException("Input must contain at least 1 letter");
}
//Save the input
logger.debug("Saving");
this.inputString = inputString;
}
//Encodes the inputString and stores the result in outputString
protected void encode(){
logger.debug("Encoding");
StringJoiner output = new StringJoiner(" ");
//Loop through every element in the input string and encode it
for(char letter : inputString.toCharArray()){
//If the character is a letter get teh correct combination from code and add it to the output
logger.debug("Working character {}", letter);
//If the character is a letter or a number get the correct code and add it to the output
if(Character.isUpperCase(letter)){
output.append(code[letter - 65]);
output.append(' ');
logger.debug("Appending letter");
output.add(letters[letter - 'A']);
}
//If it is a number get the correct combination from code and add it to the output
else if(Character.isDigit(letter)){
int tempNum = Integer.parseInt(Character.toString(letter));
output.append(code[tempNum + 26]);
output.append(' ');
logger.debug("Appending number");
output.add(numbers[letter - '0']);
}
//If the character is not a letter or number throw an exception because it cannot be encoded
else{
throw new InvalidInputException("Invalid characters found in input");
}
}
//Remove the final space from the output
if(output.length() > 0){
output.replace(output.length() - 1, output.length(), "");
}
//Save and return the output
outputString = output.toString();
return outputString;
//Save the output
this.outputString = output.toString();
logger.debug("Saving encoded string '{}'", outputString);
}
//Decodes inputString and stores the result in outputString
private String decode(){
//Decodes the inputString and stores the result in outputString
protected void decode(){
logger.debug("Decoding");
StringBuilder output = new StringBuilder();
//Loop through every element in the input string and encode it
for(String current : inputString.split(" ")){
boolean found = false;
//Loop through the code and see if the current letter
for(int cnt = 0;(cnt < 26) && (!found);++cnt){
//See if current is the same as an element in code
if(current.equals(code[cnt])){
//Add 65 to cnt to get the correct capital letter
output.append((char)(cnt + 'A'));
found = true;
}
logger.debug("Working letter {}", current);
//Get the current letter from the map and append it to the output
if(map.containsKey(current)){
char letter = map.get(current);
logger.debug("Decoded letter {}", letter);
output.append(letter);
}
//Loop through code and see if current is a number
for(int cnt = 26;(cnt < 36) && (!found);++cnt){
if(current.equals(code[cnt])){
//Remove 26 from cnt to get the correct number
output.append(Integer.toString(cnt - 26));
found = true;
}
}
//If it is neither print an error in the output
if(!found){
output.append("<Unknown symbol: " + current + ">");
else{
throw new InvalidInputException("Invalid characters found in input");
}
}
//Save and return the output
outputString = output.toString();
return outputString;
}
//Encodes input and returns the result
private void setEncodeInputString(String input){
//Make sure the letters are all uppercase
input = input.toUpperCase();
//Remove all characters that are not capital letters
input = input.replaceAll("[^A-Z0-9]", "");
//Save the new input
inputString = input;
}
//Decodes input and returns the result
private void setDecodeInputString(String input){
//Remove all characters except ., -, and ' '
input = input.replaceAll("[^ \\.\\-]", "");
//Save the new input
inputString = input;
//Save the output
this.outputString = output.toString();
logger.debug("Saving decoded string '{}'", outputString);
}
@@ -96,26 +153,34 @@ public class Morse{
public Morse(){
reset();
}
//Returns inputString
//Sets the inputString and encodes the message
public String encode(String inputString){
setInputStringEncode(inputString);
encode();
return outputString;
}
//Sets the inputString and decodes the message
public String decode(String inputString){
setInputStringDecode(inputString);
decode();
return outputString;
}
//Getters
public String getInputString(){
return inputString;
}
//Returns outputString
public String getOutputString(){
return outputString;
}
//Encodes input and returns the result
public String encode(String input){
setEncodeInputString(input);
return encode();
}
//Decoes input and returns the result
public String decode(String input){
setDecodeInputString(input);
return decode();
}
//Makes sure all variables are empty
public void reset(){
inputString = outputString = "";
logger.debug("Resetting");
inputString = "";
outputString = "";
}
}

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Playfair.java
//Matthew Ellison
// Created: 07-30-21
//Modified: 04-28-23
//Modified: 05-04-23
package com.mattrixwv.cipherstream.polysubstitution;
@@ -16,7 +16,7 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Playfair{
protected static Logger logger = LoggerFactory.getLogger(Playfair.class);
private static final Logger logger = LoggerFactory.getLogger(Playfair.class);
//A class representing the location of a character in the grid
protected class CharLocation{
@@ -35,16 +35,17 @@ public class Playfair{
}
//Fields
protected boolean preserveCapitals; //Whether to respect captials in the output string
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
protected boolean preserveSymbols; //Whether to respect symbols in the output string
protected char replaced; //The letter that will need to be replaced in the grid and any input string or keyword
protected char replacer; //The letter that replaced replaced in the input string or keyword
protected char doubled; //The letter that will be placed between double letters in the input string if necessary or to make the string length even
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
protected char[][] grid; //The grid used to encode/decode the message
protected char replaced; //The letter that will need to be replaced in the grid and any input string or keyword
protected char replacer; //The letter that replaced replaced in the input string or keyword
protected char doubled; //The letter that will be placed between double letters in the input string if necessary or to make the string length even
//Settings
protected boolean preserveCapitals; //Persist captials in the output string
protected boolean preserveWhitespace; //Persist whitespace in the output string
protected boolean preserveSymbols; //Persist symbols in the output string
//Set the doubled character
@@ -482,6 +483,7 @@ public class Playfair{
addCharactersToCleanString(output.toString());
}
//Constructor
public Playfair() throws InvalidCharacterException{
reset();
preserveCapitals = false;
@@ -509,6 +511,7 @@ public class Playfair{
setReplacer(replacer);
setDoubled(doubled);
}
//Sets the keyword and inputString and encodes the message
public String encode(String keyword, String input) throws InvalidCharacterException, InvalidInputException{
reset();
@@ -535,7 +538,7 @@ public class Playfair{
outputString = "";
keyword = "";
}
//Gets
//Getters
public char getReplaced(){
return replaced;
}

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/PolybiusSquare.java
//Mattrixwv
// Created: 01-04-22
//Modified: 04-29-23
//Modified: 05-04-23
package com.mattrixwv.cipherstream.polysubstitution;
@@ -16,7 +16,7 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class PolybiusSquare{
protected static Logger logger = LoggerFactory.getLogger(PolybiusSquare.class);
private static final Logger logger = LoggerFactory.getLogger(PolybiusSquare.class);
//A class representing the location of a character in the grid
protected class CharLocation{
@@ -41,8 +41,9 @@ public class PolybiusSquare{
protected char[][] grid; //The grid used to encode/decode the message
protected char replaced; //The letter that will need to be replaced in the grid and any input string or keyword
protected char replacer; //The letter that replaces replaced in the input string or keyword
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
protected boolean preserveSymbols; //Whether to respect symbols in the output string
//Settings
protected boolean preserveWhitespace; //Persist whitespace in the output string
protected boolean preserveSymbols; //Persist symbols in the output string
//Setting the character to be replaced
@@ -362,6 +363,7 @@ public class PolybiusSquare{
addCharactersToCleanStringDecode(output.toString());
}
//Constructors
public PolybiusSquare() throws InvalidCharacterException{
reset();
setReplaced('J');
@@ -383,6 +385,7 @@ public class PolybiusSquare{
this.preserveWhitespace = preserveWhitespace;
this.preserveSymbols = preserveSymbols;
}
//Sets the keyword and inputString and encodes the message
public String encode(String inputString) throws InvalidCharacterException, InvalidInputException{
return encode("", inputString);
@@ -415,7 +418,8 @@ public class PolybiusSquare{
outputString = "";
keyword = "";
}
//Gets
//Getters
public char getReplaced(){
return replaced;
}

View File

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

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Trifid.java
//Mattrixwv
// Created: 03-03-22
//Modified: 03-03-22
//Modified: 05-04-23
package com.mattrixwv.cipherstream.polysubstitution;
@@ -21,10 +21,10 @@ 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;
private int y;
private int z;
protected class CharLocation{
protected int x;
protected int y;
protected int z;
public CharLocation(int x, int y, int z){
this.x = x;
this.y = y;
@@ -45,18 +45,19 @@ public class Trifid{
}
//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
private int groupSize; //The size of the groups used to break up the input
private char[][][] grid; //The grid used to encode/decode the message
private char fillIn; //The character added to the alphabet to meet the 27 character requirement
private boolean preserveCapitals; //Persist capitals in the output string
private boolean preserveWhitespace; //Persist whitespace in the output string
private boolean preserveSymbols; //Persist symbols in the output string
protected String inputString; //The message that needs to be encoded/decoded
protected String outputString; //The encoded/decoded message
protected String keyword; //The keyword used to create the grid
protected int groupSize; //The size of the groups used to break up the input
protected char[][][] grid; //The grid used to encode/decode the message
protected char fillIn; //The character added to the alphabet to meet the 27 character requirement
//Settings
protected boolean preserveCapitals; //Persist capitals in the output string
protected boolean preserveWhitespace; //Persist whitespace in the output string
protected boolean preserveSymbols; //Persist symbols in the output string
//Makes sure the fillIn is a valid character
private void setFillIn(char fillIn) throws InvalidCharacterException{
protected void setFillIn(char fillIn) throws InvalidCharacterException{
//Make sure the character is a printing character
if((fillIn < ' ') || (fillIn > '~')){
throw new InvalidCharacterException("Fill in character must be a printing character");
@@ -72,7 +73,7 @@ public class Trifid{
this.fillIn = fillIn;
}
//Strips invalid characters from the keyword and creates the grid
private void setKeyword(String keyword) throws InvalidKeywordException{
protected void setKeyword(String keyword) throws InvalidKeywordException{
//Ensure the keyword isn't null
if(keyword == null){
throw new InvalidKeywordException("Keyword cannot be null");
@@ -93,7 +94,7 @@ public class Trifid{
keyword += "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + fillIn;
//Remove all duplicate characters
logger.debug("Removing duplicated characters");
logger.debug("Removing duplicate characters");
StringBuilder uniqueKey = new StringBuilder();
keyword.chars().distinct().forEach(c -> uniqueKey.append((char)c));
this.keyword = uniqueKey.toString();
@@ -104,7 +105,7 @@ public class Trifid{
createGrid();
}
//Creates the grid from the keyword
private void createGrid(){
protected void createGrid(){
logger.debug("Creating grid from keyword");
for(int layerCnt = 0;layerCnt < grid.length;++layerCnt){
@@ -120,7 +121,7 @@ public class Trifid{
logger.debug("Completed grid\n{}", getGrid());
}
//Ensures groupSize constraints
private void setGroupSize(int groupSize) throws InvalidBaseException{
protected void setGroupSize(int groupSize) throws InvalidBaseException{
if(groupSize <= 0){
throw new InvalidBaseException("Group size must be > 0");
}
@@ -130,7 +131,7 @@ public class Trifid{
this.groupSize = groupSize;
}
//Ensures inputString constraints
private void setInputString(String inputString) throws InvalidInputException{
protected void setInputString(String inputString) throws InvalidInputException{
//Ensure the input string isn't null
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
@@ -165,12 +166,12 @@ public class Trifid{
}
}
//Returns the inputString with only letters
private String getCleanInputString(){
protected 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{
protected CharLocation findChar(char letter) throws InvalidCharacterException{
logger.debug("Finding character {} in grid", letter);
for(int layer = 0;layer < grid.length;++layer){
@@ -189,7 +190,7 @@ public class Trifid{
throw new InvalidCharacterException("The character '" + letter + "' was not found in the grid");
}
//Return the character from the location provided
private char getChar(CharLocation location) throws InvalidCharacterException{
protected char getChar(CharLocation location) throws InvalidCharacterException{
if(location.getX() > 2){
throw new InvalidCharacterException("x cannot be larget than 2");
}
@@ -205,7 +206,7 @@ public class Trifid{
return grid[location.getZ()][location.getX()][location.getY()];
}
//Adds all non-letter characters back to the output string
private void formatOutput(String outputString){
protected void formatOutput(String outputString){
logger.debug("Formatting output");
//Keep track of where you are in the output
@@ -222,6 +223,15 @@ public class Trifid{
logger.debug("Formatting lowercase");
output.append(Character.toLowerCase(outputString.charAt(outputCnt++)));
}
else if(ch == fillIn){
logger.debug("Adding fillIn");
if(preserveCapitals){
output.append(Character.toLowerCase(outputString.charAt(outputCnt++)));
}
else{
output.append(Character.toUpperCase(outputString.charAt(outputCnt++)));
}
}
else{
logger.debug("Appending symbol");
output.append(ch);
@@ -229,15 +239,15 @@ public class Trifid{
}
//Save the output
logger.debug("Formatted output '{}'", output);
this.outputString = output.toString();
logger.debug("Formatted output '{}'", this.outputString);
}
//Encodes inputString using a polybius square and stores the result in outputString
private void encode() throws InvalidCharacterException{
protected void encode() throws InvalidCharacterException{
logger.debug("Encoding");
//Step through every element in the sanitized inputString encoding the letters
logger.debug("Conveting letters to coordinates");
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
@@ -247,10 +257,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;
}
int numGroups = (int)Math.ceil((double)inputString.length() / (double)groupSize);
ArrayList<ArrayList<CharLocation>> groups = new ArrayList<>(numGroups);
for(int cnt = 0;cnt < numGroups;++cnt){
groups.add(new ArrayList<>());
@@ -302,7 +309,7 @@ public class Trifid{
formatOutput(output.toString());
}
//Decodes inputString using a polybius square and stores the result in outputString
private void decode() throws InvalidCharacterException{
protected void decode() throws InvalidCharacterException{
logger.debug("Decoding");
//Step through every element in the sanitized inputString encoding the letters
@@ -316,10 +323,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;
}
int numGroups = (int)Math.ceil((double)inputString.length() / (double)groupSize);
ArrayList<ArrayList<CharLocation>> groups = new ArrayList<>(numGroups);
for(int cnt = 0;cnt < numGroups;++cnt){
groups.add(new ArrayList<>());
@@ -382,18 +386,21 @@ public class Trifid{
preserveWhitespace = false;
preserveSymbols = false;
setFillIn('+');
reset();
}
public Trifid(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
this.preserveCapitals = preserveCapitals;
this.preserveWhitespace = preserveWhitespace;
this.preserveSymbols = preserveSymbols;
setFillIn('+');
reset();
}
public Trifid(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, char fillIn) throws InvalidCharacterException{
this.preserveCapitals = preserveCapitals;
this.preserveWhitespace = preserveWhitespace;
this.preserveSymbols = preserveSymbols;
setFillIn(fillIn);
reset();
}
//Encodes inputString using keyword and groupSize and returns the result
@@ -437,7 +444,8 @@ public class Trifid{
groupSize = Integer.MAX_VALUE;
grid = new char[3][3][3];
}
//Gets
//Getters
public String getInputString(){
return inputString;
}