Added ADFGX cipher

This commit is contained in:
2022-01-25 23:26:05 -05:00
parent 87c5eb093e
commit 57f5d350da
2 changed files with 623 additions and 0 deletions

View File

@@ -0,0 +1,173 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGX.java
//Mattrixwv
// Created: 01-25-22
//Modified: 01-25-22
package com.mattrixwv.CipherStreamJava.combination;
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 ADFGX{
private boolean preserveCapitals;
private boolean preserveWhitespace;
private boolean preserveSymbols;
private String inputString;
private String outputString;
private String squareKeyword;
private String keyword;
private PolybiusSquare polybiusSquare;
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 = polybiusSquare.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", "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("X", "5");
//Decode with polybius
String polybiusOutput = polybiusSquare.decode(squareKeyword, columnarOutput);
outputString = polybiusOutput;
//Add whatever is needed to the output string
formatOutputStringDecode();
return outputString;
}
public ADFGX() throws InvalidCharacterException{
preserveCapitals = false;
preserveWhitespace = false;
preserveSymbols = false;
reset();
}
public ADFGX(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{
polybiusSquare = new PolybiusSquare(false, false);
columnar = new Columnar(false, false, false, true, 'B');
inputString = "";
outputString = "";
squareKeyword = "";
keyword = "";
}
}