Added Baconian cipher

This commit is contained in:
2022-01-12 19:44:28 -05:00
parent 0271fbe23c
commit 79d064f0b7
2 changed files with 234 additions and 0 deletions

View File

@@ -0,0 +1,134 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/Baconian.java
//Mattrixwv
// Created: 01-12-22
//Modified: 01-12-22
package com.mattrixwv.CipherStreamJava.monoSubstitution;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringJoiner;
import com.mattrixwv.CipherStreamJava.exceptions.InvalidCharacterException;
public class Baconian{
private static final ArrayList<String> code = new ArrayList<String>(Arrays.asList(
"aaaaa", "aaaab", "aaaba", "aaabb", "aabaa", "aabab", "aabba","aabbb", "abaaa", "abaaa", "abaab", "ababa", "ababb", //A-M
"abbaa", "abbab", "abbba", "abbbb", "baaaa", "baaab", "baaba", "baabb", "baabb", "babaa", "babab", "babba", "babbb" //N-Z
));
private String inputString; //The string that needs encoded/decoded
private String outputString; //The encoded/decoded string
private boolean leaveCapitals; //Whether to respect capitals in the output string
//Sets the input string
private void setInputStringEncode(String inputString){
//Remove all whitespace and symbols
inputString = inputString.replaceAll("[^A-Za-z]", "");
if(!leaveCapitals){
inputString = inputString.toLowerCase();
}
this.inputString = inputString;
}
private void setInputStringDecode(String inputString) throws InvalidCharacterException{
if(!leaveCapitals){
inputString = inputString.toLowerCase();
}
//Check each Baconian Cipher letter for validity
for(String str : inputString.split(" ")){
//Make sure each letter contains 5 characters
if(str.length() != 5){
throw new InvalidCharacterException("All Baconian letters contain exactly 5 characters: " + str);
}
//Make sure the letter contains only a's and b's
String temp = str.replaceAll("[^abAB]", "");
if(!temp.equals(str)){
throw new InvalidCharacterException("Baconian letters contain only a's and b's: " + str);
}
}
this.inputString = inputString;
}
//Encodes the inputString and stores the result in outputString
private String encode(){
StringJoiner output = new StringJoiner(" ");
//Go through every character in the inputString and encode it
for(char ch : inputString.toCharArray()){
//Convert the character to a binary string with A = 0
String binary = null;
if(Character.isUpperCase(ch)){
binary = code.get(ch - 'A').toUpperCase();
}
else{
binary = code.get(ch - 'a').toLowerCase();
}
//Add the encoded character to the output
output.add(binary);
}
//Save and return the output
outputString = output.toString();
return outputString;
}
//Decodes the inputString and stores the result in outputString
private String decode(){
StringBuilder output = new StringBuilder();
//Go through every Baconian Cipher character in the inputString and decode it
for(String baconianCharacter : inputString.split(" ")){
//Get the location of the Baconian character in the array
int location = code.indexOf(baconianCharacter.toLowerCase());
//Convert the Baconian character to an ASCII character
char ch = '\0';
if(Character.isUpperCase(baconianCharacter.charAt(0))){
ch = (char)(location + 'A');
}
else{
ch = (char)(location + 'a');
}
//Add the decoded character to the output
output.append(ch);
}
//Save and return the output
outputString = output.toString();
return outputString;
}
//Constructor
public Baconian(){
reset();
leaveCapitals = false;
}
public Baconian(boolean leaveCapitals){
reset();
this.leaveCapitals = leaveCapitals;
}
//Returns the outputString
public String getOutputString(){
return outputString;
}
//Returns the inputString
public String getInputString(){
return inputString;
}
//Sets the inputString and encodes the message
public String encode(String inputString){
reset();
setInputStringEncode(inputString);
return encode();
}
//Sets the inputString and decodes the message
public String decode(String inputString) throws InvalidCharacterException{
reset();
setInputStringDecode(inputString);
return decode();
}
//Makes sure all of the variables are empty
public void reset(){
inputString = "";
outputString = "";
}
}