//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Playfair.java //Matthew Ellison // Created: 07-30-21 //Modified: 07-09-22 package com.mattrixwv.cipherstream.polysubstitution; import java.util.StringJoiner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException; import com.mattrixwv.cipherstream.exceptions.InvalidInputException; public class Playfair{ private static final Logger logger = LoggerFactory.getLogger(Playfair.class); //A class representing the location of a character in the grid private class CharLocation{ private int x; private int y; public CharLocation(int x, int y){ this.x = x; this.y = y; } public int getX(){ return x; } public int getY(){ return y; } } //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 //Create the grid from the keyword private void createGrid(){ logger.debug("Creating grid from keyword"); for(int row = 0;row < 5;++row){ for(int col = 0;col < 5;++col){ char letter = keyword.charAt((5 * row) + col); grid[row][col] = letter; } } logger.debug("Grid\n{}", getGrid()); } //Strips invalid characters from the string that needs encoded/decoded private void setInputString(String inputString, boolean encoding) throws InvalidCharacterException, InvalidInputException{ //Make sure the input string is not null if(inputString == null){ throw new NullPointerException("The input string cannot be null"); } logger.debug("Original input string {}", inputString); //Set the options if(!preserveCapitals){ logger.debug("Removing case"); inputString = inputString.toUpperCase(); } if(!preserveWhitespace){ logger.debug("Removing whitespace"); inputString = inputString.replaceAll("\\s", ""); } if(!preserveSymbols){ logger.debug("Removing symbols"); inputString = inputString.replaceAll("[^a-zA-Z\\s]", ""); } //Make replace all of the replacers with replaced logger.debug("Replacing all {} with {}", replaced, replacer); inputString = inputString.replace(Character.toString(replaced), Character.toString(replacer)); //If there is nothing in the input string throw an exception if(inputString.isBlank()){ throw new InvalidCharacterException("The input string cannot be blank"); } //If this is encoding parse it and clean up any problems if(encoding){ setEncodingInputString(inputString); } //If this is decoding just add it without parsing it else{ //Throw an exception if replaced is included if(inputString.contains(Character.toString(replaced))){ throw new InvalidCharacterException("An encoded message cannot contain a letter that needs replaced"); } logger.debug("Clean input string '{}'", inputString); this.inputString = inputString; } if(this.inputString.isBlank() || getPreparedInputString().isBlank()){ throw new InvalidInputException("Input must have at least 1 letter"); } } private void setEncodingInputString(String inputString){ //Replace characters that need replaced 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();){ //Advance until you find a letter, saving the input for inclusion StringBuilder prepend = new StringBuilder(); while((cnt < inputString.length()) && !Character.isAlphabetic(inputString.charAt(cnt))){ prepend.append(inputString.charAt(cnt++)); } //If we have reached the end of the string end the loop if(cnt == inputString.length()){ cleanInput.append(prepend); break; } //Get the next character char firstLetter = inputString.charAt(cnt++); ++letterCount; //Advance until you find a letter, saving the input for inclusion StringBuilder middle = new StringBuilder(); while((cnt < inputString.length()) && !Character.isAlphabetic(inputString.charAt(cnt))){ middle.append(inputString.charAt(cnt++)); } char secondLetter = '\0'; //If we have not reached the end of the string get the next character if(cnt != inputString.length()){ secondLetter = inputString.charAt(cnt++); ++letterCount; } //If the second character is the same as the first character set the pointer back and use the doubled character if(secondLetter == firstLetter){ --cnt; secondLetter = doubled; } //Add all of the gathered input to the cleaned up input cleanInput.append(prepend); cleanInput.append(firstLetter); cleanInput.append(middle); if(secondLetter != '\0'){ cleanInput.append(secondLetter); } } //Check if there are an odd number of characters if((letterCount % 2) == 1){ int lastLetterLocation = cleanInput.length() - 1; while(!Character.isAlphabetic(cleanInput.charAt(lastLetterLocation))){ --lastLetterLocation; } if(cleanInput.charAt(lastLetterLocation) == doubled){ cleanInput.append(replacer); } else{ cleanInput.append(doubled); } } logger.debug("Cleaned input string '{}'", cleanInput); this.inputString = cleanInput.toString(); } //Returns the input string ready for encoding private String getPreparedInputString(){ logger.debug("Getting input string ready for encoding"); String cleanString = inputString.toUpperCase(); cleanString = cleanString.replaceAll("[^A-Z]", ""); logger.debug("Prepared string '{}'", cleanString); return cleanString; } //Strips invalid characters from the keyword and creates the grid private void setKeyword(String keyword){ if(keyword == null){ throw new NullPointerException("Keyword cannot be null"); } logger.debug("Original keyword {}", keyword); //Change everything to uppercase logger.debug("Removing case"); keyword = keyword.toUpperCase(); //Removing everything except capital letters logger.debug("Removing all non-letter characters"); keyword = keyword.replaceAll("[^A-Z]", ""); //Add all letters in the alphabet to the key logger.debug("Appending the alphabet to the keyword"); keyword += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //Replace all replaced characters logger.debug("Replacing {} with {}", replaced, replacer); keyword = keyword.replaceAll(Character.toString(replaced), Character.toString(replacer)); //Remove all duplicate chatacters logger.debug("Removing duplicated characters"); StringBuilder uniqueKey = new StringBuilder(); keyword.chars().distinct().forEach(c -> uniqueKey.append((char)c)); this.keyword = uniqueKey.toString(); //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{ logger.debug("Finding character in grid {}", letter); for(int row = 0;row < grid.length;++row){ for(int col = 0;col < grid[row].length;++col){ if(grid[row][col] == letter){ logger.debug("Found at {}, {}", row, col); return new CharLocation(row, col); } } } //If it was not found something went wrong throw new InvalidInputException("The character '" + letter + "' was not found in the grid"); } //Returns the location in the grid of x and y, adjusting for out of bounds private char getGridChar(int x, int y){ logger.debug("Getting character from grid[{}][{}]", x, y); if(x < 0){ x += 5; } if(y < 0){ y += 5; } char letter = grid[x % 5][y % 5]; logger.debug("Character {}", letter); return letter; } //Adds characters that aren't letters to the output private void addCharactersToCleanString(String cleanString){ logger.debug("Formatting output string"); int outputCnt = 0; StringBuilder fullOutput = new StringBuilder(); for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){ logger.debug("Working character {}", inputString.charAt(inputCnt)); if(Character.isUpperCase(inputString.charAt(inputCnt))){ logger.debug("Adjusting uppercase"); fullOutput.append(cleanString.charAt(outputCnt++)); } else if(Character.isLowerCase(inputString.charAt(inputCnt))){ logger.debug("Adjusting lowercase"); fullOutput.append(Character.toLowerCase(cleanString.charAt(outputCnt++))); } else{ logger.debug("Inserting symbol"); fullOutput.append(inputString.charAt(inputCnt)); } } logger.debug("Formatted output '{}'", fullOutput); outputString = fullOutput.toString(); } //Encodes inputString using the Playfair cipher and stores the result in outputString private String encode() throws InvalidInputException{ logger.debug("Encoding"); StringBuilder output = new StringBuilder(); int inputCnt = 0; String cleanString = getPreparedInputString(); while(inputCnt < cleanString.length()){ //Get the next 2 letters to be encoded char firstLetter = cleanString.charAt(inputCnt++); char secondLetter = cleanString.charAt(inputCnt++); logger.debug("Letters {} {}", firstLetter, secondLetter); //Find the letters in the grid CharLocation firstLocation = findChar(firstLetter); CharLocation secondLocation = findChar(secondLetter); //Encode the letters if(firstLocation.getX() == secondLocation.getX()){ logger.debug("Row encoding"); firstLetter = getGridChar(firstLocation.getX(), firstLocation.getY() + 1); secondLetter = getGridChar(secondLocation.getX(), secondLocation.getY() + 1); } else if(firstLocation.getY() == secondLocation.getY()){ logger.debug("Column encoding"); firstLetter = getGridChar(firstLocation.getX() + 1, firstLocation.getY()); secondLetter = getGridChar(secondLocation.getX() + 1, secondLocation.getY()); } else{ logger.debug("Corner encoding"); firstLetter = getGridChar(firstLocation.getX(), secondLocation.getY()); secondLetter = getGridChar(secondLocation.getX(), firstLocation.getY()); } //Add the new letters to the output string logger.debug("Encoded letters {} {}", firstLetter, secondLetter); output.append(firstLetter); output.append(secondLetter); } //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{ logger.debug("Decoding"); StringBuilder output = new StringBuilder(); int inputCnt = 0; String cleanString = getPreparedInputString(); while(inputCnt < cleanString.length()){ //Get the next 2 letters to be encoded char firstLetter = cleanString.charAt(inputCnt++); char secondLetter = cleanString.charAt(inputCnt++); logger.debug("Letters {} {}", firstLetter, secondLetter); //Find the letters in the grid CharLocation firstLocation = findChar(firstLetter); CharLocation secondLocation = findChar(secondLetter); //Decode the letters if(firstLocation.getX() == secondLocation.getX()){ logger.debug("Decoding row"); firstLetter = getGridChar(firstLocation.getX(), firstLocation.getY() - 1); secondLetter = getGridChar(secondLocation.getX(), secondLocation.getY() - 1); } else if(firstLocation.getY() == secondLocation.getY()){ logger.debug("Decoding col"); firstLetter = getGridChar(firstLocation.getX() - 1, firstLocation.getY()); secondLetter = getGridChar(secondLocation.getX() - 1, secondLocation.getY()); } else{ logger.debug("Decoding corners"); firstLetter = getGridChar(firstLocation.getX(), secondLocation.getY()); secondLetter = getGridChar(secondLocation.getX(), firstLocation.getY()); } //Add the new letters to the output string logger.debug("Decoded letters {} {}", firstLetter, secondLetter); output.append(firstLetter); output.append(secondLetter); } //Add other characters to the output string addCharactersToCleanString(output.toString()); //Return the output string return outputString; } public Playfair() throws InvalidCharacterException{ reset(); preserveCapitals = false; preserveWhitespace = false; preserveSymbols = false; 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'); } public Playfair(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, char replaced, char replacer, char doubled) throws InvalidCharacterException{ reset(); this.preserveCapitals = preserveCapitals; this.preserveWhitespace = preserveWhitespace; this.preserveSymbols = preserveSymbols; setReplaced(replaced); setReplacer(replacer); setDoubled(doubled); } //Sets the keyword and inputString and encodes the message public String encode(String keyword, String input) throws InvalidCharacterException, InvalidInputException{ reset(); setKeyword(keyword); setInputString(input, true); return encode(); } //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(); } //Makes sure all variables are empty public void reset(){ logger.debug("Resetting fields"); grid = new char[5][5]; inputString = ""; outputString = ""; keyword = ""; } //Gets 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; } public String getInputString(){ return inputString; } public String getOutputString(){ return outputString; } public String getGrid(){ logger.debug("Creating string from grid"); StringJoiner gridString = new StringJoiner("\n"); for(char[] row : grid){ StringJoiner rowString = new StringJoiner(" ", "[", "]"); for(char col : row){ rowString.add(Character.toString(col)); } gridString.add(rowString.toString()); } return gridString.toString(); } }