diff --git a/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGVX.java b/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGVX.java new file mode 100644 index 0000000..4b1a7d6 --- /dev/null +++ b/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGVX.java @@ -0,0 +1,296 @@ +//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGVX.java +//Mattrixwv +// Created: 01-26-22 +//Modified: 01-26-22 +package com.mattrixwv.CipherStreamJava.combination; + + +import java.util.StringJoiner; + +import com.mattrixwv.CipherStreamJava.exceptions.InvalidCharacterException; +import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException; +import com.mattrixwv.CipherStreamJava.exceptions.InvalidKeywordException; +import com.mattrixwv.CipherStreamJava.polySubstitution.Columnar; +import com.mattrixwv.CipherStreamJava.polySubstitution.PolybiusSquare; + + +public class ADFGVX{ + private class LargePolybiusSquare extends PolybiusSquare{ + @Override + protected void createGrid(){ + 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"); + } + + //Change to upper case + inputString = inputString.toUpperCase(); + + //Remove any whitespace if selected + if(!preserveWhitespace){ + inputString = inputString.replaceAll("\\s+", ""); + } + + //Remove any symbols if selected + if(!preserveSymbols){ + 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; + + if(this.inputString.isBlank() || getPreparedInputStringEncoding().isBlank()){ + throw new InvalidInputException("Input must contain at least 1 letter"); + } + } + @Override + protected String getPreparedInputStringEncoding(){ + String cleanString = inputString.toUpperCase(); + cleanString = cleanString.replaceAll("[^A-Z0-9]", ""); + return cleanString; + } + @Override + protected void setKeyword(String keyword){ + if(keyword == null){ + throw new NullPointerException("Keyword cannot be null"); + } + //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(); + + //Create the grid from the sanitized keyword + createGrid(); + } + @Override + protected void addCharactersToCleanStringEncode(String cleanString){ + int outputCnt = 0; + StringBuilder fullOutput = new StringBuilder(); + for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){ + //Add both numbers of any letters to the output + if(Character.isAlphabetic(inputString.charAt(inputCnt)) || Character.isDigit(inputString.charAt(inputCnt))){ + fullOutput.append(cleanString.charAt(outputCnt++)); + fullOutput.append(cleanString.charAt(outputCnt++)); + } + //Add any other characters that appear to the output + else{ + fullOutput.append(inputString.charAt(inputCnt)); + } + } + outputString = fullOutput.toString(); + } + @Override + protected void addCharactersToCleanStringDecode(String cleanString){ + int outputCnt = 0; + StringBuilder fullOutput = new StringBuilder(); + for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){ + //Add the letter to the output and skip the second number + if(Character.isDigit(inputString.charAt(inputCnt)) || Character.isAlphabetic(inputString.charAt(inputCnt))){ + fullOutput.append(cleanString.charAt(outputCnt++)); + ++inputCnt; + } + //Add any other characters that appear to the output + else{ + fullOutput.append(inputString.charAt(inputCnt)); + } + } + outputString = fullOutput.toString(); + } + @Override + public void reset(){ + grid = new char[6][6]; + inputString = ""; + outputString = ""; + keyword = ""; + } + + public LargePolybiusSquare(boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{ + super(preserveWhitespace, preserveSymbols); + } + } + private boolean preserveCapitals; + private boolean preserveWhitespace; + private boolean preserveSymbols; + private String inputString; + private String outputString; + private String squareKeyword; + private String keyword; + private LargePolybiusSquare largePolybiusSquare; + private Columnar columnar; + + private void setSquareKeyword(String squareKeyword) throws InvalidKeywordException{ + if(squareKeyword == null){ + throw new InvalidKeywordException("Square Keyword cannot be null"); + } + + this.squareKeyword = squareKeyword; + } + private void setKeyword(String keyword) throws InvalidKeywordException{ + if(keyword == null){ + throw new InvalidKeywordException("Keyword cannot be null"); + } + + this.keyword = keyword; + } + private void setInputString(String inputString) throws InvalidInputException{ + if(inputString == null){ + throw new InvalidInputException("Input cannot be null"); + } + + if(!preserveCapitals){ + inputString = inputString.toUpperCase(); + } + if(!preserveWhitespace){ + inputString = inputString.replaceAll("\\s+", ""); + } + if(!preserveSymbols){ + inputString = inputString.replaceAll("[^a-zA-Z\\s]", ""); + } + + this.inputString = inputString; + + if(this.inputString.isBlank()){ + throw new InvalidInputException("Input cannot be blank"); + } + } + private void formatOutputStringEncode(){ + StringBuilder output = new StringBuilder(); + int outputLocation = 0; + for(char ch : inputString.toCharArray()){ + if(Character.isUpperCase(ch)){ + output.append(Character.toUpperCase(outputString.charAt(outputLocation++))); + output.append(Character.toUpperCase(outputString.charAt(outputLocation++))); + } + else if(Character.isLowerCase(ch)){ + output.append(Character.toLowerCase(outputString.charAt(outputLocation++))); + output.append(Character.toLowerCase(outputString.charAt(outputLocation++))); + } + else{ + output.append(ch); + } + } + + outputString = output.toString(); + } + private void formatOutputStringDecode(){ + StringBuilder output = new StringBuilder(); + int outputLocation = 0; + for(int inputLocation = 0;inputLocation < inputString.length();++inputLocation){ + char ch = inputString.charAt(inputLocation); + if(Character.isUpperCase(ch)){ + output.append(Character.toUpperCase(outputString.charAt(outputLocation++))); + ++inputLocation; + } + else if(Character.isLowerCase(ch)){ + output.append(Character.toLowerCase(outputString.charAt(outputLocation++))); + ++inputLocation; + } + else{ + output.append(ch); + } + } + outputString = output.toString(); + } + private String encode() throws InvalidCharacterException, InvalidInputException, InvalidKeywordException{ + //Encode the input with polybius + String polybiusOutput = largePolybiusSquare.encode(squareKeyword, inputString); + //Change polybius to use the correct symbols + polybiusOutput = polybiusOutput.replaceAll("1", "A").replaceAll("2", "D").replaceAll("3", "F").replaceAll("4", "G").replaceAll("5", "V").replaceAll("6", "X"); + + //Encode polybius's output with columnar + String columnarOutput = columnar.encode(keyword, polybiusOutput); + outputString = columnarOutput; + + //Add whatever is needed to the output string + formatOutputStringEncode(); + + return outputString; + } + private String decode() throws InvalidKeywordException, InvalidCharacterException, InvalidInputException{ + //Decode the input with columnar + String columnarOutput = columnar.decode(keyword, inputString); + + //Change the symbols to the correct ones for polybius + columnarOutput = columnarOutput.replaceAll("A", "1").replaceAll("D", "2").replaceAll("F", "3").replaceAll("G", "4").replaceAll("V", "5").replaceAll("X", "6"); + //Decode with polybius + String polybiusOutput = largePolybiusSquare.decode(squareKeyword, columnarOutput); + outputString = polybiusOutput; + + //Add whatever is needed to the output string + formatOutputStringDecode(); + + return outputString; + } + + public ADFGVX() throws InvalidCharacterException{ + preserveCapitals = false; + preserveWhitespace = false; + preserveSymbols = false; + reset(); + } + public ADFGVX(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{ + this.preserveCapitals = preserveCapitals; + this.preserveWhitespace = preserveWhitespace; + this.preserveSymbols = preserveSymbols; + reset(); + } + + public String encode(String squareKeyword, String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{ + setSquareKeyword(squareKeyword); + setKeyword(keyword); + setInputString(inputString); + return encode(); + } + public String decode(String squareKeyword, String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{ + setSquareKeyword(squareKeyword); + setKeyword(keyword); + setInputString(inputString); + return decode(); + } + public String getInputString(){ + return inputString; + } + public String getOutputString(){ + return outputString; + } + public String getSquareKeyword(){ + return squareKeyword; + } + public String getKeyword(){ + return keyword; + } + public void reset() throws InvalidCharacterException{ + largePolybiusSquare = new LargePolybiusSquare(false, false); + columnar = new Columnar(false, false, false, true, 'B'); + inputString = ""; + outputString = ""; + squareKeyword = ""; + keyword = ""; + } +}