Updated more tests
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Playfair.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-30-21
|
||||
//Modified: 07-09-22
|
||||
//Modified: 04-28-23
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
@@ -12,15 +12,16 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
public class Playfair{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Playfair.class);
|
||||
protected static Logger logger = LoggerFactory.getLogger(Playfair.class);
|
||||
|
||||
//A class representing the location of a character in the grid
|
||||
private class CharLocation{
|
||||
private int x;
|
||||
private int y;
|
||||
protected class CharLocation{
|
||||
protected int x;
|
||||
protected int y;
|
||||
public CharLocation(int x, int y){
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
@@ -34,23 +35,122 @@ public class Playfair{
|
||||
}
|
||||
|
||||
//Fields
|
||||
private boolean preserveCapitals; //Whether to respect captials in the output string
|
||||
private boolean preserveWhitespace; //Whether to respect whitespace in the output string
|
||||
private boolean preserveSymbols; //Whether to respect symbols in the output string
|
||||
private char replaced; //The letter that will need to be replaced in the grid and any input string or keyword
|
||||
private char replacer; //The letter that replaced replaced in the input string or keyword
|
||||
private char doubled; //The letter that will be placed between double letters in the input string if necessary or to make the string length even
|
||||
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[][] grid; //The grid used to encode/decode the message
|
||||
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
|
||||
|
||||
|
||||
//Set the doubled character
|
||||
protected void setDoubled(char doubled) throws InvalidCharacterException{
|
||||
logger.debug("Setting doubled");
|
||||
logger.debug("Original character {}", doubled);
|
||||
|
||||
//Make sure the character is a letter
|
||||
logger.debug("Checking letter");
|
||||
if(!Character.isAlphabetic(doubled)){
|
||||
throw new InvalidCharacterException("The double letter replacement must be a letter");
|
||||
}
|
||||
|
||||
//Make sure the 2 replacers are the same
|
||||
logger.debug("Checking same as replacer");
|
||||
if(doubled == replacer){
|
||||
throw new InvalidCharacterException("The double letter replacement cannot be the same as the regular replacer");
|
||||
}
|
||||
|
||||
//Make sure the double letter replacement isn't the same as the letter being replaced
|
||||
logger.debug("Checking same as replaced");
|
||||
if(doubled == replaced){
|
||||
throw new InvalidCharacterException("The double letter replacement cannot be the same as the letter replaced");
|
||||
}
|
||||
|
||||
if(!preserveCapitals){
|
||||
logger.debug("Removing capitals");
|
||||
|
||||
doubled = Character.toUpperCase(doubled);
|
||||
}
|
||||
|
||||
logger.debug("Setting doubled to {}", doubled);
|
||||
this.doubled = doubled;
|
||||
}
|
||||
//Set the replacer character
|
||||
protected void setReplacer(char replacer) throws InvalidCharacterException{
|
||||
logger.debug("Setting replacer");
|
||||
logger.debug("Original character {}", replacer);
|
||||
|
||||
//Make sure the character is a letter
|
||||
logger.debug("Checking letter");
|
||||
if(!Character.isAlphabetic(replacer)){
|
||||
throw new InvalidCharacterException("The replacer must be a letter");
|
||||
}
|
||||
|
||||
//Make sure the character isn't the same as what it is supposed to replace
|
||||
logger.debug("Checking same as replaced");
|
||||
if(replacer == replaced){
|
||||
throw new InvalidCharacterException("The replacer cannot be the same letter as what it is replacing");
|
||||
}
|
||||
|
||||
//Make sure the replacer isn't the same as the double letter replacer
|
||||
logger.debug("Checking same as doubled");
|
||||
if(replacer == doubled){
|
||||
throw new InvalidCharacterException("The replacer cannot be the same as the double letter replacer");
|
||||
}
|
||||
|
||||
if(!preserveCapitals){
|
||||
logger.debug("Removing capitals");
|
||||
|
||||
replacer = Character.toUpperCase(replacer);
|
||||
}
|
||||
|
||||
logger.debug("Setting replacer to {}", replacer);
|
||||
this.replacer = replacer;
|
||||
}
|
||||
//Set the replaced character
|
||||
protected void setReplaced(char replaced) throws InvalidCharacterException{
|
||||
logger.debug("Setting replaced");
|
||||
logger.debug("Original character {}", replaced);
|
||||
|
||||
//Make sure the character is a letter
|
||||
logger.debug("Checking letter");
|
||||
if(!Character.isAlphabetic(replaced)){
|
||||
throw new InvalidCharacterException("The replaced character must be a letter");
|
||||
}
|
||||
|
||||
//Make sure the character isn't the same as what is replacing it
|
||||
logger.debug("Checking same as replacer");
|
||||
if(replaced == replacer){
|
||||
throw new InvalidCharacterException("The replaced letter cannot be the same as the replacing letter");
|
||||
}
|
||||
|
||||
//Make sure the replacer isn't the same as the double letter replacer
|
||||
logger.debug("Checking same as doubled");
|
||||
if(replaced == doubled){
|
||||
throw new InvalidCharacterException("The replaced letter cannot be the same as the replacing letter");
|
||||
}
|
||||
|
||||
if(!preserveCapitals){
|
||||
logger.debug("Removing capitals");
|
||||
|
||||
replaced = Character.toUpperCase(replaced);
|
||||
}
|
||||
|
||||
logger.debug("Setting replaced to {}", replaced);
|
||||
this.replaced = replaced;
|
||||
}
|
||||
//Create the grid from the keyword
|
||||
private void createGrid(){
|
||||
protected void createGrid(){
|
||||
logger.debug("Creating grid from keyword");
|
||||
|
||||
for(int row = 0;row < 5;++row){
|
||||
for(int col = 0;col < 5;++col){
|
||||
char letter = keyword.charAt((5 * row) + col);
|
||||
logger.debug("Letter {} going to position [{}] [{}]", letter, row, col);
|
||||
grid[row][col] = letter;
|
||||
}
|
||||
}
|
||||
@@ -58,10 +158,12 @@ public class Playfair{
|
||||
logger.debug("Grid\n{}", getGrid());
|
||||
}
|
||||
//Strips invalid characters from the string that needs encoded/decoded
|
||||
private void setInputString(String inputString, boolean encoding) throws InvalidCharacterException, InvalidInputException{
|
||||
protected void setInputString(String inputString, boolean encoding) throws InvalidCharacterException, InvalidInputException{
|
||||
logger.debug("Setting input string");
|
||||
|
||||
//Make sure the input string is not null
|
||||
if(inputString == null){
|
||||
throw new NullPointerException("The input string cannot be null");
|
||||
throw new InvalidInputException("The input string cannot be null");
|
||||
}
|
||||
|
||||
logger.debug("Original input string {}", inputString);
|
||||
@@ -83,18 +185,14 @@ public class Playfair{
|
||||
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
|
||||
}
|
||||
|
||||
//Make replace all of the replacers with replaced
|
||||
logger.debug("Replacing all {} with {}", replaced, replacer);
|
||||
inputString = inputString.replace(Character.toString(replaced), Character.toString(replacer));
|
||||
|
||||
//If there is nothing in the input string throw an exception
|
||||
if(inputString.isBlank()){
|
||||
throw new InvalidCharacterException("The input string cannot be blank");
|
||||
throw new InvalidInputException("The input string cannot be blank");
|
||||
}
|
||||
|
||||
//If this is encoding parse it and clean up any problems
|
||||
if(encoding){
|
||||
setEncodingInputString(inputString);
|
||||
setInputStringEncode(inputString);
|
||||
}
|
||||
//If this is decoding just add it without parsing it
|
||||
else{
|
||||
@@ -105,20 +203,28 @@ public class Playfair{
|
||||
|
||||
logger.debug("Clean input string '{}'", inputString);
|
||||
this.inputString = inputString;
|
||||
|
||||
if((getPreparedInputString().length() % 2) == 1){
|
||||
throw new InvalidInputException("Input length must be even");
|
||||
}
|
||||
}
|
||||
|
||||
if(this.inputString.isBlank() || getPreparedInputString().isBlank()){
|
||||
if(getPreparedInputString().isBlank()){
|
||||
throw new InvalidInputException("Input must have at least 1 letter");
|
||||
}
|
||||
}
|
||||
private void setEncodingInputString(String inputString){
|
||||
protected void setInputStringEncode(String inputString){
|
||||
logger.debug("Cleaning up input string for encoding");
|
||||
|
||||
//Replace characters that need replaced
|
||||
logger.debug("Replacing all {} with {}", replaced, replacer);
|
||||
inputString = inputString.replace(Character.toString(replaced), Character.toString(replacer));
|
||||
|
||||
//Check if there are any doubled characters
|
||||
StringBuilder cleanInput = new StringBuilder();
|
||||
int letterCount = 0;
|
||||
for(int cnt = 0;cnt < inputString.length();){
|
||||
logger.debug("Starting at character {}", cnt);
|
||||
//Advance until you find a letter, saving the input for inclusion
|
||||
StringBuilder prepend = new StringBuilder();
|
||||
while((cnt < inputString.length()) && !Character.isAlphabetic(inputString.charAt(cnt))){
|
||||
@@ -151,6 +257,7 @@ public class Playfair{
|
||||
}
|
||||
|
||||
//Add all of the gathered input to the cleaned up input
|
||||
logger.debug("Adding to clean input: {} {} {} {}", prepend, firstLetter, middle, secondLetter);
|
||||
cleanInput.append(prepend);
|
||||
cleanInput.append(firstLetter);
|
||||
cleanInput.append(middle);
|
||||
@@ -160,7 +267,9 @@ public class Playfair{
|
||||
}
|
||||
|
||||
//Check if there are an odd number of characters
|
||||
logger.debug("Checking odd characters");
|
||||
if((letterCount % 2) == 1){
|
||||
logger.debug("Adding final character to make even");
|
||||
int lastLetterLocation = cleanInput.length() - 1;
|
||||
while(!Character.isAlphabetic(cleanInput.charAt(lastLetterLocation))){
|
||||
--lastLetterLocation;
|
||||
@@ -173,11 +282,11 @@ public class Playfair{
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug("Cleaned input string '{}'", cleanInput);
|
||||
this.inputString = cleanInput.toString();
|
||||
logger.debug("Cleaned input string '{}'", this.inputString);
|
||||
}
|
||||
//Returns the input string ready for encoding
|
||||
private String getPreparedInputString(){
|
||||
protected String getPreparedInputString(){
|
||||
logger.debug("Getting input string ready for encoding");
|
||||
|
||||
String cleanString = inputString.toUpperCase();
|
||||
@@ -188,9 +297,11 @@ public class Playfair{
|
||||
return cleanString;
|
||||
}
|
||||
//Strips invalid characters from the keyword and creates the grid
|
||||
private void setKeyword(String keyword){
|
||||
protected void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
logger.debug("Setting keyword");
|
||||
|
||||
if(keyword == null){
|
||||
throw new NullPointerException("Keyword cannot be null");
|
||||
throw new InvalidKeywordException("Keyword cannot be null");
|
||||
}
|
||||
|
||||
logger.debug("Original keyword {}", keyword);
|
||||
@@ -209,19 +320,20 @@ public class Playfair{
|
||||
|
||||
//Replace all replaced characters
|
||||
logger.debug("Replacing {} with {}", replaced, replacer);
|
||||
keyword = keyword.replaceAll(Character.toString(replaced), Character.toString(replacer));
|
||||
keyword = keyword.replaceAll(Character.toString(Character.toUpperCase(replaced)), Character.toString(Character.toUpperCase(replacer)));
|
||||
|
||||
//Remove all duplicate chatacters
|
||||
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();
|
||||
logger.debug("Cleaned keyword {}", this.keyword);
|
||||
|
||||
//Create the grid from the sanitized keyword
|
||||
createGrid();
|
||||
}
|
||||
//Returns the location of the given character in the grid
|
||||
private CharLocation findChar(char letter) throws InvalidInputException{
|
||||
protected CharLocation findChar(char letter) throws InvalidInputException{
|
||||
logger.debug("Finding character in grid {}", letter);
|
||||
|
||||
for(int row = 0;row < grid.length;++row){
|
||||
@@ -238,7 +350,7 @@ public class Playfair{
|
||||
throw new InvalidInputException("The character '" + letter + "' was not found in the grid");
|
||||
}
|
||||
//Returns the location in the grid of x and y, adjusting for out of bounds
|
||||
private char getGridChar(int x, int y){
|
||||
protected char getGridChar(int x, int y){
|
||||
logger.debug("Getting character from grid[{}][{}]", x, y);
|
||||
|
||||
if(x < 0){
|
||||
@@ -253,7 +365,7 @@ public class Playfair{
|
||||
return letter;
|
||||
}
|
||||
//Adds characters that aren't letters to the output
|
||||
private void addCharactersToCleanString(String cleanString){
|
||||
protected void addCharactersToCleanString(String cleanString){
|
||||
logger.debug("Formatting output string");
|
||||
|
||||
int outputCnt = 0;
|
||||
@@ -262,27 +374,27 @@ public class Playfair{
|
||||
logger.debug("Working character {}", inputString.charAt(inputCnt));
|
||||
|
||||
if(Character.isUpperCase(inputString.charAt(inputCnt))){
|
||||
logger.debug("Adjusting uppercase");
|
||||
logger.debug("Appending uppercase");
|
||||
|
||||
fullOutput.append(cleanString.charAt(outputCnt++));
|
||||
}
|
||||
else if(Character.isLowerCase(inputString.charAt(inputCnt))){
|
||||
logger.debug("Adjusting lowercase");
|
||||
logger.debug("Appending lowercase");
|
||||
|
||||
fullOutput.append(Character.toLowerCase(cleanString.charAt(outputCnt++)));
|
||||
}
|
||||
else{
|
||||
logger.debug("Inserting symbol");
|
||||
logger.debug("Appending symbol");
|
||||
|
||||
fullOutput.append(inputString.charAt(inputCnt));
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug("Formatted output '{}'", fullOutput);
|
||||
outputString = fullOutput.toString();
|
||||
logger.debug("Formatted output '{}'", outputString);
|
||||
}
|
||||
//Encodes inputString using the Playfair cipher and stores the result in outputString
|
||||
private String encode() throws InvalidInputException{
|
||||
protected void encode() throws InvalidInputException{
|
||||
logger.debug("Encoding");
|
||||
|
||||
StringBuilder output = new StringBuilder();
|
||||
@@ -324,12 +436,9 @@ public class Playfair{
|
||||
|
||||
//Add other characters to the output string
|
||||
addCharactersToCleanString(output.toString());
|
||||
|
||||
//Return the output string
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString using the Playfair cipher and stores the result in outputString
|
||||
private String decode() throws InvalidInputException{
|
||||
protected void decode() throws InvalidInputException{
|
||||
logger.debug("Decoding");
|
||||
|
||||
StringBuilder output = new StringBuilder();
|
||||
@@ -348,17 +457,17 @@ public class Playfair{
|
||||
|
||||
//Decode the letters
|
||||
if(firstLocation.getX() == secondLocation.getX()){
|
||||
logger.debug("Decoding row");
|
||||
logger.debug("Row decoding");
|
||||
firstLetter = getGridChar(firstLocation.getX(), firstLocation.getY() - 1);
|
||||
secondLetter = getGridChar(secondLocation.getX(), secondLocation.getY() - 1);
|
||||
}
|
||||
else if(firstLocation.getY() == secondLocation.getY()){
|
||||
logger.debug("Decoding col");
|
||||
logger.debug("Column decoding");
|
||||
firstLetter = getGridChar(firstLocation.getX() - 1, firstLocation.getY());
|
||||
secondLetter = getGridChar(secondLocation.getX() - 1, secondLocation.getY());
|
||||
}
|
||||
else{
|
||||
logger.debug("Decoding corners");
|
||||
logger.debug("Corner decoding");
|
||||
firstLetter = getGridChar(firstLocation.getX(), secondLocation.getY());
|
||||
secondLetter = getGridChar(secondLocation.getX(), firstLocation.getY());
|
||||
}
|
||||
@@ -371,9 +480,6 @@ public class Playfair{
|
||||
|
||||
//Add other characters to the output string
|
||||
addCharactersToCleanString(output.toString());
|
||||
|
||||
//Return the output string
|
||||
return outputString;
|
||||
}
|
||||
|
||||
public Playfair() throws InvalidCharacterException{
|
||||
@@ -381,18 +487,18 @@ public class Playfair{
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
setReplaced('J');
|
||||
setReplacer('I');
|
||||
setDoubled('X');
|
||||
setReplaced('j');
|
||||
setReplacer('i');
|
||||
setDoubled('x');
|
||||
}
|
||||
public Playfair(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
|
||||
reset();
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
setReplaced('J');
|
||||
setReplacer('I');
|
||||
setDoubled('X');
|
||||
setReplaced('j');
|
||||
setReplacer('i');
|
||||
setDoubled('x');
|
||||
}
|
||||
public Playfair(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, char replaced, char replacer, char doubled) throws InvalidCharacterException{
|
||||
reset();
|
||||
@@ -408,14 +514,16 @@ public class Playfair{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setInputString(input, true);
|
||||
return encode();
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Sets the keyword and inputString and decodes the message
|
||||
public String decode(String keyword, String input) throws InvalidCharacterException, InvalidInputException{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setInputString(input, false);
|
||||
return decode();
|
||||
decode();
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Makes sure all variables are empty
|
||||
@@ -431,64 +539,12 @@ public class Playfair{
|
||||
public char getReplaced(){
|
||||
return replaced;
|
||||
}
|
||||
public void setReplaced(char replaced) throws InvalidCharacterException{
|
||||
if(!Character.isAlphabetic(replaced)){
|
||||
throw new InvalidCharacterException("The replaced character must be a letter");
|
||||
}
|
||||
|
||||
if(replaced == replacer){
|
||||
throw new InvalidCharacterException("The replaced letter cannot be the same as the replacing letter");
|
||||
}
|
||||
|
||||
if(replaced == doubled){
|
||||
throw new InvalidCharacterException("The replaced letter cannot be the same as the replacing letter");
|
||||
}
|
||||
|
||||
this.replaced = Character.toUpperCase(replaced);
|
||||
}
|
||||
public char getReplacer(){
|
||||
return replacer;
|
||||
}
|
||||
public void setReplacer(char replacer) throws InvalidCharacterException{
|
||||
//Make sure the character is a letter
|
||||
if(!Character.isAlphabetic(replacer)){
|
||||
throw new InvalidCharacterException("The replacer must be a letter");
|
||||
|
||||
}
|
||||
|
||||
//Make sure the character isn't the same as what it is supposed to replace
|
||||
if(replacer == replaced){
|
||||
throw new InvalidCharacterException("The replacer cannot be the same letter as what it is replacing");
|
||||
}
|
||||
|
||||
//Make sure the replacer isn't the same as the double letter replacer
|
||||
if(replacer == doubled){
|
||||
throw new InvalidCharacterException("The replacer cannot be the same as the double letter replacer");
|
||||
}
|
||||
|
||||
this.replacer = Character.toUpperCase(replacer);
|
||||
}
|
||||
public char getDoubled(){
|
||||
return doubled;
|
||||
}
|
||||
public void setDoubled(char doubled) throws InvalidCharacterException{
|
||||
//Make sure the character is a letter
|
||||
if(!Character.isAlphabetic(doubled)){
|
||||
throw new InvalidCharacterException("The double letter replacement must be a letter");
|
||||
}
|
||||
|
||||
//Make sure the 2 replacers are the same
|
||||
if(doubled == replacer){
|
||||
throw new InvalidCharacterException("The double letter replacement cannot be the same as the regular replacer");
|
||||
}
|
||||
|
||||
//Make sure the double letter replacement isn't the same as the letter being replaced
|
||||
if(doubled == replaced){
|
||||
throw new InvalidCharacterException("The double letter replacement cannot be the same as the letter replaced");
|
||||
}
|
||||
|
||||
this.doubled = Character.toUpperCase(doubled);
|
||||
}
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user