Update unit test coverage

This commit is contained in:
2023-04-22 10:52:16 -04:00
parent 494293c311
commit 59885b8df6
21 changed files with 2784 additions and 2005 deletions

View File

@@ -1,12 +1,10 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGVX.java
//Mattrixwv
// Created: 01-26-22
//Modified: 04-14-23
//Modified: 04-21-23
package com.mattrixwv.cipherstream.combination;
import java.util.StringJoiner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -14,181 +12,19 @@ import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
import com.mattrixwv.cipherstream.polysubstitution.Columnar;
import com.mattrixwv.cipherstream.polysubstitution.PolybiusSquare;
import com.mattrixwv.cipherstream.polysubstitution.LargePolybiusSquare;
public class ADFGVX{
protected static Logger logger = LoggerFactory.getLogger(ADFGVX.class);
//Internal classes
protected class LargePolybiusSquare extends PolybiusSquare{
protected static final Logger logger = LoggerFactory.getLogger(LargePolybiusSquare.class);
@Override
protected void createGrid(){
logger.debug("Creating grid");
for(int row = 0;row < 6;++row){
for(int col = 0;col < 6;++col){
char letter = keyword.charAt((6 * row) + col);
grid[row][col] = letter;
}
}
}
@Override
protected void setInputStringEncoding(String inputString) throws InvalidCharacterException, InvalidInputException{
if(inputString == null){
throw new NullPointerException("Input cannot be null");
}
logger.debug("Original input string '{}'", inputString);
//Change to upper case
inputString = inputString.toUpperCase();
//Remove any whitespace if selected
if(!preserveWhitespace){
logger.debug("Removing whitespace");
inputString = inputString.replaceAll("\\s", "");
}
//Remove any symbols if selected
if(!preserveSymbols){
logger.debug("Removing symbols");
inputString = inputString.replaceAll("[^a-zA-Z0-9\\s]", "");
}
if(!preserveWhitespace && !preserveSymbols){
//Add whitespace after every character for the default look
StringJoiner spacedString = new StringJoiner(" ");
for(int cnt = 0;cnt < inputString.length();++cnt){
spacedString.add(Character.toString(inputString.charAt(cnt)));
}
inputString = spacedString.toString();
}
//Save the string
this.inputString = inputString;
logger.debug("Cleaned input string '{}'", inputString);
if(this.inputString.isBlank() || getPreparedInputStringEncoding().isBlank()){
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
@Override
protected String getPreparedInputStringEncoding(){
logger.debug("Preparing input string for encoding");
String cleanString = inputString.toUpperCase();
cleanString = cleanString.replaceAll("[^A-Z0-9]", "");
logger.debug("Cleaned input string '{}'", cleanString);
return cleanString;
}
@Override
protected void setKeyword(String keyword){
if(keyword == null){
throw new NullPointerException("Keyword cannot be null");
}
logger.debug("Original keyword '{}'", keyword);
//Change everything to uppercase
keyword = keyword.toUpperCase();
//Remove everything except capital letters and numbers
keyword = keyword.replaceAll("[^A-Z0-9]", "");
//Add all letters in the alphabet to the key
keyword += "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
//Remove all duplicate characters
StringBuilder uniqueKey = new StringBuilder();
keyword.chars().distinct().forEach(c -> uniqueKey.append((char)c));
this.keyword = uniqueKey.toString();
logger.debug("Cleaned keyword '{}'", keyword);
//Create the grid from the sanitized keyword
createGrid();
}
@Override
protected void addCharactersToCleanStringEncode(String cleanString){
logger.debug("Formatting output string");
int outputCnt = 0;
StringBuilder fullOutput = new StringBuilder();
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
logger.debug("Current character {}", inputString.charAt(inputCnt));
//Add both numbers of any letters to the output
if(Character.isAlphabetic(inputString.charAt(inputCnt)) || Character.isDigit(inputString.charAt(inputCnt))){
logger.debug("Appending character");
fullOutput.append(cleanString.charAt(outputCnt++));
fullOutput.append(cleanString.charAt(outputCnt++));
}
//Add any other characters that appear to the output
else{
logger.debug("Appending symbol");
fullOutput.append(inputString.charAt(inputCnt));
}
}
outputString = fullOutput.toString();
}
@Override
protected void addCharactersToCleanStringDecode(String cleanString){
logger.debug("Formatting output string");
int outputCnt = 0;
StringBuilder fullOutput = new StringBuilder();
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
logger.debug("Current character {}", inputString.charAt(inputCnt));
//Add the letter to the output and skip the second number
if(Character.isDigit(inputString.charAt(inputCnt)) || Character.isAlphabetic(inputString.charAt(inputCnt))){
logger.debug("Appending character");
fullOutput.append(cleanString.charAt(outputCnt++));
++inputCnt;
}
//Add any other characters that appear to the output
else{
logger.debug("Appending symbol");
fullOutput.append(inputString.charAt(inputCnt));
}
}
outputString = fullOutput.toString();
}
@Override
public void reset(){
logger.debug("reseting");
grid = new char[6][6];
inputString = "";
outputString = "";
keyword = "";
}
public LargePolybiusSquare(boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
super(preserveWhitespace, preserveSymbols);
}
}
//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 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
//Internal ciphers
protected LargePolybiusSquare largePolybiusSquare; //The first step in encoding

View File

@@ -13,7 +13,7 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class OneTimePad extends Vigenere{
private static final Logger logger = LoggerFactory.getLogger(OneTimePad.class);
protected static Logger logger = LoggerFactory.getLogger(OneTimePad.class);
//?Add some kind of entropy calculator?
//?Add some kind of "book passage includer"?

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Porta.java
//Mattrixwv
// Created: 02-28-22
//Modified: 07-09-22
//Modified: 04-17-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -13,7 +13,7 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Porta{
private static final Logger logger = LoggerFactory.getLogger(Porta.class);
protected static Logger logger = LoggerFactory.getLogger(Porta.class);
private static final String[] tableau = {
"NOPQRSTUVWXYZABCDEFGHIJKLM", //A-B
@@ -32,15 +32,15 @@ public class Porta{
};
//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
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
//Ensure all keyword constraints are followed
private void setKeyword(String keyword) throws InvalidKeywordException{
protected void setKeyword(String keyword) throws InvalidKeywordException{
//Make sure the keyword isn't null
if(keyword == null){
throw new InvalidKeywordException("Keyword cannot be null");
@@ -66,7 +66,7 @@ public class Porta{
}
}
//Ensure all input constraints are followed
private void setInputString(String inputString) throws InvalidInputException{
protected void setInputString(String inputString) throws InvalidInputException{
//Ensure the input isn't null
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
@@ -86,7 +86,7 @@ public class Porta{
inputString = inputString.replaceAll("\\s", "");
}
if(!preserveSymbols){
logger.debug("Removig symbols");
logger.debug("Removing symbols");
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
}
@@ -101,7 +101,7 @@ public class Porta{
}
}
//Returns the letter that replaces the passed in letter
private char getReplacer(int keywordCnt, char letter){
protected char getReplacer(int keywordCnt, char letter){
logger.debug("Getting letter that replaces {} at {}", letter, keywordCnt);
char keyLetter = keyword.charAt(keywordCnt % keyword.length());
@@ -129,9 +129,21 @@ public class Porta{
return replacer;
}
//Encodes the inputString and stores the result in outputString
private void encode(){
protected void encode(){
logger.debug("Encoding");
//Encoding is the same as decoding
code();
}
//Decodes the inputString and stores the result in outputString
protected void decode(){
logger.debug("Decoding");
//Decoding is the same as encoding
code();
}
//Codes the inputString and stores the result in outputString
protected void code(){
StringBuilder output = new StringBuilder();
//Step through every character in the inputString and advance it the correct amount according to the keyword and tableau
@@ -156,15 +168,8 @@ public class Porta{
}
//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();
logger.debug("Saving output string '{}'", outputString);
}

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Substitution.java
//Mattrixwv
// Created: 02-22-22
//Modified: 07-09-22
//Modified: 04-18-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -13,18 +13,18 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Substitution{
private static final Logger logger = LoggerFactory.getLogger(Substitution.class);
protected static 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
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
//Ensures key constraints are followed
private void setKey(String key) throws InvalidKeywordException{
protected void setKeyword(String key) throws InvalidKeywordException{
if(key == null){
throw new InvalidKeywordException("Key cannot be null");
}
@@ -37,8 +37,9 @@ public class Substitution{
//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)){
StringBuilder uniqueKey = new StringBuilder();
key.chars().distinct().forEach(c -> uniqueKey.append((char)c));
if(!key.equals(uniqueKey.toString())){
throw new InvalidKeywordException("The key cannot contain duplicate mappings");
}
@@ -47,16 +48,16 @@ public class Substitution{
logger.debug("Ensuring there are only letters in the key");
//Make sure the key contains all valid characters
tempKey = key.replaceAll("[^A-Z]", "");
String tempKey = key.replaceAll("[^A-Z]", "");
if(!tempKey.equals(key)){
throw new InvalidKeywordException("The key must contain all letters");
}
}
else if(key.length() == 36){
logger.debug("Ensure there are only alpha-numeric characters in the key");
logger.debug("Ensuring there are only alpha-numeric characters in the key");
//Make sure the key contains all valid characters
tempKey = key.replaceAll("[^A-Z0-9]", "");
String tempKey = key.replaceAll("[^A-Z0-9]", "");
if(!tempKey.equals(key)){
throw new InvalidKeywordException("The key must contain all letters and can contain all numbers");
}
@@ -67,10 +68,10 @@ public class Substitution{
//Save the key
logger.debug("Cleaned key '{}'", key);
this.key = key;
this.keyword = key;
}
//Ensure intput constraints are followed
private void setInputString(String inputString) throws InvalidInputException{
protected void setInputString(String inputString) throws InvalidInputException{
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
}
@@ -104,7 +105,7 @@ public class Substitution{
}
}
//Encodes the inputString and stores the result in outputString
private void encode(){
protected void encode(){
logger.debug("Encoding");
StringBuilder output = new StringBuilder();
@@ -115,15 +116,15 @@ public class Substitution{
if(Character.isUpperCase(ch)){
logger.debug("Encoding uppercase");
output.append(Character.toUpperCase(key.charAt(ch - 'A')));
output.append(Character.toUpperCase(keyword.charAt(ch - 'A')));
}
else if(Character.isLowerCase(ch)){
logger.debug("Encoding lowercase");
output.append(Character.toLowerCase(key.charAt(ch - 'a')));
output.append(Character.toLowerCase(keyword.charAt(ch - 'a')));
}
else if(Character.isDigit(ch) && (key.length() == 36)){
else if(Character.isDigit(ch) && (keyword.length() == 36)){
logger.debug("Encoding digit");
output.append(key.charAt('Z' - 'A' + Integer.valueOf(Character.toString(ch)) + 1));
output.append(keyword.charAt('Z' - 'A' + Integer.valueOf(Character.toString(ch)) + 1));
}
else{
logger.debug("Passing symbol through");
@@ -132,11 +133,11 @@ public class Substitution{
}
//Save the output
logger.debug("Encoded message '{}'", output);
this.outputString = output.toString();
logger.debug("Encoded message '{}'", outputString);
}
//Decodes the inputString and stores the result in outputString
private void decode(){
protected void decode(){
logger.debug("Decoding");
StringBuilder output = new StringBuilder();
@@ -148,27 +149,27 @@ public class Substitution{
if(Character.isUpperCase(ch)){
logger.debug("Encoding uppercase");
output.append((char)('A' + key.indexOf(Character.toUpperCase(ch))));
output.append((char)('A' + keyword.indexOf(Character.toUpperCase(ch))));
}
else if(Character.isLowerCase(ch)){
logger.debug("Encoding lowercase");
output.append((char)('a' + key.indexOf(Character.toUpperCase(ch))));
output.append((char)('a' + keyword.indexOf(Character.toUpperCase(ch))));
}
else if(Character.isDigit(ch) && (key.length() == 36)){
else if(Character.isDigit(ch) && (keyword.length() == 36)){
logger.debug("Encoding digit");
output.append((char)('0' + (key.indexOf(Character.toUpperCase(ch)) - 26)));
output.append((char)('0' + (keyword.indexOf(Character.toUpperCase(ch)) - 26)));
}
else{
logger.debug("Passing through symbol");
logger.debug("Passing symbol through");
output.append(ch);
}
}
//Save the output
logger.debug("Encoded message '{}'", output);
this.outputString = output.toString();
logger.debug("Decoded message '{}'", outputString);
}
public Substitution(){
@@ -190,16 +191,16 @@ public class Substitution{
return outputString;
}
public String getKeyword(){
return key;
return keyword;
}
public String encode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{
setKey(key);
setKeyword(key);
setInputString(inputString);
encode();
return outputString;
}
public String decode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{
setKey(key);
setKeyword(key);
setInputString(inputString);
decode();
return outputString;
@@ -209,6 +210,6 @@ public class Substitution{
inputString = "";
outputString = "";
key = "";
keyword = "";
}
}

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Vigenere.java
//Matthew Ellison
// Created: 07-25-21
//Modified: 07-09-22
//Modified: 04-18-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -45,7 +45,7 @@ public class Vigenere{
//Sets inputString
protected void setInputString(String inputString) throws InvalidInputException{
if(inputString == null){
throw new NullPointerException("Input cannot be null");
throw new InvalidInputException("Input cannot be null");
}
logger.debug("Original input string '{}'", inputString);
@@ -76,7 +76,7 @@ public class Vigenere{
//Sets keyword
protected void setKeyword(String keyword) throws InvalidKeywordException{
if(keyword == null){
throw new NullPointerException("Keyword cannot be null");
throw new InvalidKeywordException("Keyword cannot be null");
}
logger.debug("Original keyword '{}'", keyword);
@@ -119,12 +119,7 @@ public class Vigenere{
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'){
if(letter > 'Z'){
logger.debug("Wrapping around to A");
letter -= 26;
@@ -136,12 +131,7 @@ public class Vigenere{
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'){
if(letter > 'z'){
logger.debug("Wrapping around to a");
letter -= 26;
@@ -153,8 +143,8 @@ public class Vigenere{
}
//Save output
logger.debug("Encoded message '{}'", output);
outputString = output.toString();
logger.debug("Encoded message '{}'", outputString);
return outputString;
}
//Decodes inputString and stores the result in outputString
@@ -180,11 +170,6 @@ public class Vigenere{
letter += 26;
}
else if(letter > 'Z'){
logger.debug("Wrapping around to A");
letter -= 26;
}
}
else if(Character.isLowerCase(letter)){
logger.debug("Decoding lowercase");
@@ -196,21 +181,16 @@ public class Vigenere{
letter += 26;
}
else if(letter > 'z'){
logger.debug("Wrapping around to a");
letter -= 26;
}
}
//Add letter to output
logger.debug("Encoded letter {}", letter);
logger.debug("Decoded character {}", letter);
output.append(letter);
}
//Save output
logger.debug("Encoded message '{}'", output);
outputString = output.toString();
logger.debug("Decoded message '{}'", outputString);
return outputString;
}

View File

@@ -18,27 +18,27 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Columnar{
private static final Logger logger = LoggerFactory.getLogger(Columnar.class);
protected static final Logger logger = LoggerFactory.getLogger(Columnar.class);
//Fields
private String inputString; //The message that needs to be encoded/decoded
private String outputString; //The encoded/decoded message
private String keyword; //The keyword used to create the grid
private char characterToAdd; //The character that is added to the end of a string to bring it to the correct length
private int charsAdded; //The number of characters that were added to the end of the message
private ArrayList<ArrayList<Character>> grid; //The grid used to encode/decode the message
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
private boolean removePadding; //Remove the padding letters added to the cipher
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 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 ArrayList<ArrayList<Character>> grid; //The grid used to encode/decode the message
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
//Strip the inputString of all non-letter characters and change them to capitals
private String getCleanInputString(){
protected String getCleanInputString(){
logger.debug("Cleaning input string");
return inputString.toUpperCase().replaceAll("[^A-Z]", "");
}
//Create the grid from the keyword
private void createGridEncode(){
protected void createGridEncode(){
logger.debug("Creating grid for encoding");
//Add the keyword to the first row in the array
@@ -57,7 +57,7 @@ public class Columnar{
)));
}
}
private void createGridDecode(){
protected void createGridDecode(){
logger.debug("Creating grid for decoding");
//Add the keyword to the first row in the array
@@ -86,7 +86,7 @@ public class Columnar{
}
}
//Strips invalid characters from the string that needs encoded/decoded
private void setInputStringEncode(String inputString) throws InvalidInputException{
protected void setInputStringEncode(String inputString) throws InvalidInputException{
logger.debug("Setting input string for encoding");
//Ensure the input isn't null
@@ -139,7 +139,7 @@ public class Columnar{
throw new InvalidInputException("Input cannot be blank");
}
}
private void setInputStringDecode(String inputString) throws InvalidInputException{
protected void setInputStringDecode(String inputString) throws InvalidInputException{
logger.debug("Setting input string for decoding");
//Ensure the input isn't null
@@ -225,7 +225,7 @@ public class Columnar{
}
}
//Creates the output string from the grid
private void createOutputStringFromColumns(){
protected void createOutputStringFromColumns(){
logger.debug("Creating output string for encoding");
//Get the current rows of any characters that you added
@@ -274,7 +274,7 @@ public class Columnar{
logger.debug("Output string '{}'", output);
outputString = output.toString();
}
private void createOutputStringFromRows(){
protected void createOutputStringFromRows(){
logger.debug("Creating output string for decoding");
//Turn the grid into a string
@@ -338,7 +338,7 @@ public class Columnar{
outputString = output.toString();
}
//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 NullPointerException("Keyword cannot be null");
@@ -357,7 +357,7 @@ public class Columnar{
}
}
//Set the character that is added to the end of the string
private void setCharacterToAdd(char characterToAdd) throws InvalidCharacterException{
protected void setCharacterToAdd(char characterToAdd) throws InvalidCharacterException{
if(!Character.isAlphabetic(characterToAdd)){
throw new InvalidCharacterException("Character to add must be a letter");
}
@@ -374,7 +374,7 @@ public class Columnar{
logger.debug("Character to add for padding {}", characterToAdd);
}
//Returns a list of integers that represents the location of the characters of the keyword in alphabetic order
private ArrayList<Integer> getKeywordAlphaLocations(){
protected ArrayList<Integer> getKeywordAlphaLocations(){
logger.debug("Creating an array of keyword letter locations");
ArrayList<Integer> orderedLocations = new ArrayList<>();
@@ -392,7 +392,7 @@ public class Columnar{
logger.debug("Array of keyword letters {}", orderedLocations);
return orderedLocations;
}
private ArrayList<Integer> getKeywordOriginalLocations(){
protected ArrayList<Integer> getKeywordOriginalLocations(){
logger.debug("Creating array of original keyword locations");
//Figure out the order the columns are in
@@ -413,7 +413,7 @@ public class Columnar{
return originalOrder;
}
//Rearanges the grid based on the list of numbers given
private void rearangeGrid(ArrayList<Integer> listOrder){
protected void rearangeGrid(ArrayList<Integer> listOrder){
logger.debug("Rearanging grid");
//Create a new grid and make sure it is the same size as the original grid
@@ -435,7 +435,7 @@ public class Columnar{
grid = newGrid;
}
//Encodes inputString using the Columnar cipher and stores the result in outputString
private void encode(){
protected void encode(){
logger.debug("Encoding");
//Create the grid
@@ -448,7 +448,7 @@ public class Columnar{
createOutputStringFromColumns();
}
//Decodes inputString using the Columnar cipher and stores the result in outputString
private void decode(){
protected void decode(){
logger.debug("Decoding");
//Create the grid

View File

@@ -0,0 +1,185 @@
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/polysubstitution/LargePolybiusSquare.java
//Mattrixwv
// Created: 04-21-23
// Modified: 04-21-23
package com.mattrixwv.cipherstream.polysubstitution;
import java.util.StringJoiner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class LargePolybiusSquare extends PolybiusSquare{
protected static Logger logger = LoggerFactory.getLogger(LargePolybiusSquare.class);
@Override
protected void createGrid(){
logger.debug("Creating grid");
for(int row = 0;row < 6;++row){
for(int col = 0;col < 6;++col){
char letter = keyword.charAt((6 * row) + col);
grid[row][col] = letter;
}
}
}
@Override
protected void setInputStringEncoding(String inputString) throws InvalidCharacterException, InvalidInputException{
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
}
logger.debug("Original input string '{}'", inputString);
//Change to upper case
inputString = inputString.toUpperCase();
//Remove any whitespace if selected
if(!preserveWhitespace){
logger.debug("Removing whitespace");
inputString = inputString.replaceAll("\\s", "");
}
//Remove any symbols if selected
if(!preserveSymbols){
logger.debug("Removing symbols");
inputString = inputString.replaceAll("[^a-zA-Z0-9\\s]", "");
}
if(!preserveWhitespace && !preserveSymbols){
//Add whitespace after every character for the default look
StringJoiner spacedString = new StringJoiner(" ");
for(int cnt = 0;cnt < inputString.length();++cnt){
spacedString.add(Character.toString(inputString.charAt(cnt)));
}
inputString = spacedString.toString();
}
//Save the string
this.inputString = inputString;
logger.debug("Cleaned input string '{}'", inputString);
if(this.inputString.isBlank() || getPreparedInputStringEncoding().isBlank()){
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
@Override
protected String getPreparedInputStringEncoding(){
logger.debug("Preparing input string for encoding");
String cleanString = inputString.toUpperCase();
cleanString = cleanString.replaceAll("[^A-Z0-9]", "");
logger.debug("Prepared input string '{}'", cleanString);
return cleanString;
}
@Override
protected void setKeyword(String keyword) throws InvalidKeywordException{
if(keyword == null){
throw new InvalidKeywordException("Keyword cannot be null");
}
logger.debug("Original keyword '{}'", keyword);
//Change everything to uppercase
keyword = keyword.toUpperCase();
//Remove everything except capital letters and numbers
keyword = keyword.replaceAll("[^A-Z0-9]", "");
//Add all letters in the alphabet to the key
keyword += "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
//Remove all duplicate characters
StringBuilder uniqueKey = new StringBuilder();
keyword.chars().distinct().forEach(c -> uniqueKey.append((char)c));
keyword = uniqueKey.toString();
logger.debug("Cleaned keyword '{}'", keyword);
this.keyword = keyword;
//Create the grid from the sanitized keyword
createGrid();
}
@Override
protected void addCharactersToCleanStringEncode(String cleanString){
logger.debug("Formatting output string");
int outputCnt = 0;
StringBuilder fullOutput = new StringBuilder();
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
logger.debug("Current character {}", inputString.charAt(inputCnt));
//Add both numbers of any letters to the output
if(Character.isAlphabetic(inputString.charAt(inputCnt)) || Character.isDigit(inputString.charAt(inputCnt))){
logger.debug("Appending character");
fullOutput.append(cleanString.charAt(outputCnt++));
fullOutput.append(cleanString.charAt(outputCnt++));
}
//Add any other characters that appear to the output
else{
logger.debug("Appending symbol");
fullOutput.append(inputString.charAt(inputCnt));
}
}
outputString = fullOutput.toString();
logger.debug("Saving output string {}", outputString);
}
@Override
protected void addCharactersToCleanStringDecode(String cleanString){
logger.debug("Formatting output string");
int outputCnt = 0;
StringBuilder fullOutput = new StringBuilder();
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
logger.debug("Current character {}", inputString.charAt(inputCnt));
//Add the letter to the output and skip the second number
if(Character.isDigit(inputString.charAt(inputCnt)) || Character.isAlphabetic(inputString.charAt(inputCnt))){
logger.debug("Appending character");
fullOutput.append(cleanString.charAt(outputCnt++));
++inputCnt;
}
//Add any other characters that appear to the output
else{
logger.debug("Appending symbol");
fullOutput.append(inputString.charAt(inputCnt));
}
}
outputString = fullOutput.toString();
logger.debug("Saving output string {}", outputString);
}
@Override
public void reset(){
logger.debug("Resetting");
grid = new char[6][6];
inputString = "";
outputString = "";
keyword = "";
}
public LargePolybiusSquare() throws InvalidCharacterException{
super();
}
public LargePolybiusSquare(boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
super(preserveWhitespace, preserveSymbols);
}
}