Created Bifid cipher
This commit is contained in:
@@ -0,0 +1,181 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Bifid.java
|
||||
//Mattrixwv
|
||||
// Created: 03-03-22
|
||||
//Modified: 03-03-22
|
||||
package com.mattrixwv.CipherStreamJava.polySubstitution;
|
||||
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidCharacterException;
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidKeywordException;
|
||||
|
||||
public class Bifid{
|
||||
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 PolybiusSquare polybiusSquare; //Used to encode the message to numbers then back into letters
|
||||
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
|
||||
|
||||
//Strips invalid characters from the keyword and creates the grid
|
||||
private void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
//Ensure the keyword isn't null
|
||||
if(keyword == null){
|
||||
throw new InvalidKeywordException("Keyword cannot be null");
|
||||
}
|
||||
|
||||
//Save the key for polybius to deal with
|
||||
this.keyword = keyword;
|
||||
}
|
||||
//Ensures inputString constraints
|
||||
private void setInputString(String inputString) throws InvalidInputException{
|
||||
//Ensure the input string isn't null
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
}
|
||||
|
||||
//Apply removal options
|
||||
if(!preserveCapitals){
|
||||
inputString = inputString.toUpperCase();
|
||||
}
|
||||
if(!preserveWhitespace){
|
||||
inputString = inputString.replaceAll("\\s", "");
|
||||
}
|
||||
if(!preserveSymbols){
|
||||
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
|
||||
}
|
||||
|
||||
//Save the string
|
||||
this.inputString = inputString;
|
||||
|
||||
//Ensure the string isn't blank
|
||||
if(this.inputString.isBlank()){
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
//Adds all non-letter characters back to the output string
|
||||
private void formatOutput(String outputString){
|
||||
//Keep track of where you are in the output
|
||||
int outputCnt = 0;
|
||||
StringBuilder output = new StringBuilder();
|
||||
//Check every character in the input and apply the correct rules to the output
|
||||
for(char ch : inputString.toCharArray()){
|
||||
if(Character.isUpperCase(ch)){
|
||||
output.append(Character.toUpperCase(outputString.charAt(outputCnt++)));
|
||||
}
|
||||
else if(Character.isLowerCase(ch)){
|
||||
output.append(Character.toLowerCase(outputString.charAt(outputCnt++)));
|
||||
}
|
||||
else{
|
||||
output.append(ch);
|
||||
}
|
||||
}
|
||||
|
||||
//Save the output
|
||||
this.outputString = output.toString();
|
||||
}
|
||||
//Encodes inputString using a polybius square and stores the result in outputString
|
||||
private void encode() throws InvalidCharacterException, InvalidInputException{
|
||||
//Get the encoded numbers from a polybius square
|
||||
String numberResult = polybiusSquare.encode(keyword, inputString).replaceAll("\\s", "");
|
||||
keyword = polybiusSquare.getKeyword(); //Save the cleaned keyword
|
||||
|
||||
//Split the numbers into 2 rows and rejoin the rows to create a new string
|
||||
StringBuilder row0 = new StringBuilder();
|
||||
StringBuilder row1 = new StringBuilder();
|
||||
boolean firstNum = true;
|
||||
for(char ch : numberResult.toCharArray()){
|
||||
if(firstNum){
|
||||
row0.append(ch);
|
||||
}
|
||||
else{
|
||||
row1.append(ch);
|
||||
}
|
||||
firstNum = !firstNum;
|
||||
}
|
||||
String shuffledResult = row0.toString() + row1.toString();
|
||||
|
||||
//Take the new string and decode the numbers using polybius
|
||||
String letterResult = polybiusSquare.decode(keyword, shuffledResult);
|
||||
|
||||
//Format the output
|
||||
formatOutput(letterResult);
|
||||
}
|
||||
//Decodes inputString using a polybius square and stores the result in outputString
|
||||
private void decode() throws InvalidCharacterException, InvalidInputException{
|
||||
//Get the decoded number from a polybius square
|
||||
String numberResult = polybiusSquare.encode(keyword, inputString).replaceAll("\\s", "");
|
||||
keyword = polybiusSquare.getKeyword();
|
||||
|
||||
//Split the numbers into to rows and rejoin the rows to create a new string
|
||||
String row0 = numberResult.substring(0, numberResult.length() / 2);
|
||||
String row1 = numberResult.substring(numberResult.length() / 2);
|
||||
StringBuilder unshuffledResult = new StringBuilder();
|
||||
for(int cnt = 0;cnt < row0.length();++cnt){
|
||||
unshuffledResult.append(row0.charAt(cnt));
|
||||
unshuffledResult.append(row1.charAt(cnt));
|
||||
}
|
||||
|
||||
//Take the new string and decode the numbers using polybius
|
||||
String letterResult = polybiusSquare.decode(keyword, unshuffledResult.toString());
|
||||
|
||||
//Format the outputString
|
||||
formatOutput(letterResult);
|
||||
}
|
||||
|
||||
|
||||
//Constructor
|
||||
public Bifid() throws InvalidCharacterException{
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
polybiusSquare = new PolybiusSquare(false, false);
|
||||
reset();
|
||||
}
|
||||
public Bifid(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
polybiusSquare = new PolybiusSquare(false, false);
|
||||
reset();
|
||||
}
|
||||
//Encodes inputString using keyword and returns the result
|
||||
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
//Set the parameters
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setInputString(inputString);
|
||||
|
||||
//Encode and return the message
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString using keyword and returns the result
|
||||
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
//Set the parameters
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setInputString(inputString);
|
||||
|
||||
//Decode and return the message
|
||||
decode();
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Makes sure all variables are empty
|
||||
public void reset(){
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
keyword = "";
|
||||
}
|
||||
//Gets
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user