Created Beaufort cipher
This commit is contained in:
@@ -0,0 +1,145 @@
|
|||||||
|
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Beaufort.java
|
||||||
|
//Mattrixwv
|
||||||
|
// Created: 02-23-22
|
||||||
|
//Modified: 02-23-22
|
||||||
|
package com.mattrixwv.CipherStreamJava.monoSubstitution;
|
||||||
|
|
||||||
|
|
||||||
|
import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException;
|
||||||
|
import com.mattrixwv.CipherStreamJava.exceptions.InvalidKeywordException;
|
||||||
|
|
||||||
|
|
||||||
|
public class Beaufort{
|
||||||
|
private String inputString; //This is the string that needs encoded/decoded
|
||||||
|
private String outputString; //This is the string that is output after encoding/decoding
|
||||||
|
private String keyword; //This is the keyword that is responsible for determining the offsets that you change each character by
|
||||||
|
private boolean preserveCapitals; //Whether to respect capitals 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 Atbash atbash; //The first step in encoding/decoding the cipher
|
||||||
|
private Caesar caesar; //The second step in encoding/decoding the cipher
|
||||||
|
private Vigenere vigenere; //The third step in encoding/decoding the cipher
|
||||||
|
|
||||||
|
//Ensures inputString constraints
|
||||||
|
public void setInputString(String inputString) throws InvalidInputException{
|
||||||
|
//Make sure the input 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;
|
||||||
|
|
||||||
|
//Make sure the string isn't blank
|
||||||
|
if(this.inputString.isBlank()){
|
||||||
|
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Ensures keyword constraints
|
||||||
|
public void setKeyword(String keyword) throws InvalidKeywordException{
|
||||||
|
//Make sure the keyword isn't null
|
||||||
|
if(keyword == null){
|
||||||
|
throw new InvalidKeywordException("Keyword cannot be null");
|
||||||
|
}
|
||||||
|
|
||||||
|
//Convert all letters to uppercase
|
||||||
|
keyword = keyword.toUpperCase();
|
||||||
|
//Remove all characters except capital letters
|
||||||
|
keyword = keyword.replaceAll("[^A-Z]", "");
|
||||||
|
//Save the string
|
||||||
|
this.keyword = keyword;
|
||||||
|
|
||||||
|
//If after all the elimination of unusable characters the keyword is empty throw an exception
|
||||||
|
if(this.keyword.isBlank() || (this.keyword.length() < 2)){
|
||||||
|
throw new InvalidKeywordException("Keyword must contain at least 2 letters");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Encodes the inputString and stores the result in outputString
|
||||||
|
public void encode() throws InvalidKeywordException, InvalidInputException{
|
||||||
|
//Reverse the string
|
||||||
|
String atbashString = atbash.encode(inputString);
|
||||||
|
//Shift the reversal by 1
|
||||||
|
//?Not quite sure why this is needed. Need to look into this cipher a bit more closely
|
||||||
|
String caesarString = caesar.encode(1, atbashString);
|
||||||
|
//Shift each letter according to the key
|
||||||
|
String vigenereString = vigenere.encode(keyword, caesarString);
|
||||||
|
|
||||||
|
//Save the output
|
||||||
|
this.outputString = vigenereString;
|
||||||
|
}
|
||||||
|
//Decodes the inputString and stores the result in outputString
|
||||||
|
public void decode() throws InvalidKeywordException, InvalidInputException{
|
||||||
|
//Decoding is just encoding again
|
||||||
|
encode();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Constructor
|
||||||
|
public Beaufort(){
|
||||||
|
preserveCapitals = false;
|
||||||
|
preserveWhitespace = false;
|
||||||
|
preserveSymbols = false;
|
||||||
|
atbash = new Atbash(false, false, false);
|
||||||
|
caesar = new Caesar(false, false, false);
|
||||||
|
vigenere = new Vigenere(false, false, false);
|
||||||
|
reset();
|
||||||
|
}
|
||||||
|
public Beaufort(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
||||||
|
this.preserveCapitals = preserveCapitals;
|
||||||
|
this.preserveWhitespace = preserveWhitespace;
|
||||||
|
this.preserveSymbols = preserveSymbols;
|
||||||
|
atbash = new Atbash(preserveCapitals, preserveWhitespace, preserveSymbols);
|
||||||
|
caesar = new Caesar(preserveCapitals, preserveWhitespace, preserveSymbols);
|
||||||
|
vigenere = new Vigenere(preserveCapitals, preserveWhitespace, preserveSymbols);
|
||||||
|
reset();
|
||||||
|
}
|
||||||
|
//Returns the current inputString
|
||||||
|
public String getInputString(){
|
||||||
|
return inputString;
|
||||||
|
}
|
||||||
|
//Returns the current outputString
|
||||||
|
public String getOutputString(){
|
||||||
|
return outputString;
|
||||||
|
}
|
||||||
|
//Returns the current keyword
|
||||||
|
public String getKeyword(){
|
||||||
|
return keyword;
|
||||||
|
}
|
||||||
|
//Encodes inputString using keyword and returns the result
|
||||||
|
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||||
|
//Set the parameters
|
||||||
|
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{
|
||||||
|
//Set the parameters
|
||||||
|
setKeyword(keyword);
|
||||||
|
setInputString(inputString);
|
||||||
|
|
||||||
|
//Decode and return the message
|
||||||
|
decode();
|
||||||
|
return outputString;
|
||||||
|
}
|
||||||
|
//Makes sure all of the variables are empty
|
||||||
|
public void reset(){
|
||||||
|
inputString = "";
|
||||||
|
outputString = "";
|
||||||
|
keyword = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user