254 lines
7.0 KiB
Java
254 lines
7.0 KiB
Java
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Vigenere.java
|
|
//Matthew Ellison
|
|
// Created: 07-25-21
|
|
//Modified: 05-04-23
|
|
package com.mattrixwv.cipherstream.monosubstitution;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
|
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
|
|
|
|
|
public class Vigenere{
|
|
private static final Logger logger = LoggerFactory.getLogger(Vigenere.class);
|
|
//Fields
|
|
protected String inputString; //This is the string that needs encoded/decoded
|
|
protected String outputString; //This is the string that is output after encoding/decoding
|
|
protected String keyword; //This is the keyword that is resposible for determining the offsets that you change each character by
|
|
protected ArrayList<Integer> offset; //Holds the offsets coputed from each character in the keyword
|
|
//Settings
|
|
protected boolean preserveCapitals; //Persist capitals in the output string
|
|
protected boolean preserveWhitespace; //Persist whitespace in the output string
|
|
protected boolean preserveSymbols; //Persist symbols in the output string
|
|
|
|
|
|
//Uses keyword to calculate the offset for the Caesar cipher for each character
|
|
protected void setOffset(){
|
|
logger.debug("Setting offset array from keyword");
|
|
|
|
//Reserve the correct size to increase speed later
|
|
offset.ensureCapacity(keyword.length());
|
|
|
|
//Loop through every letter in keyword and get the offset from A
|
|
for(int cnt = 0;cnt < keyword.length();++cnt){
|
|
char letter = keyword.charAt(cnt);
|
|
offset.add((letter - 'A') % 26);
|
|
}
|
|
|
|
logger.debug("Offset {}", offset);
|
|
}
|
|
//Sets inputString
|
|
protected void setInputString(String inputString) throws InvalidInputException{
|
|
if(inputString == null){
|
|
throw new InvalidInputException("Input cannot be null");
|
|
}
|
|
|
|
logger.debug("Original input string '{}'", inputString);
|
|
|
|
if(!preserveCapitals){
|
|
logger.debug("Removing case");
|
|
|
|
inputString = inputString.toUpperCase();
|
|
}
|
|
if(!preserveWhitespace){
|
|
logger.debug("Removing whitespace");
|
|
|
|
inputString = inputString.replaceAll("\\s", "");
|
|
}
|
|
if(!preserveSymbols){
|
|
logger.debug("Removing symbols");
|
|
|
|
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
|
|
}
|
|
|
|
logger.debug("Cleaned input string '{}'", inputString);
|
|
this.inputString = inputString;
|
|
|
|
if(this.inputString.isBlank()){
|
|
throw new InvalidInputException("Input must contain at least 1 letter");
|
|
}
|
|
}
|
|
//Sets keyword
|
|
protected void setKeyword(String keyword) throws InvalidKeywordException{
|
|
if(keyword == null){
|
|
throw new InvalidKeywordException("Keyword cannot be null");
|
|
}
|
|
|
|
logger.debug("Original keyword '{}'", keyword);
|
|
|
|
//Convert all letters to uppercase
|
|
logger.debug("Removing case");
|
|
keyword = keyword.toUpperCase();
|
|
//Remove all characters except capital letters
|
|
logger.debug("Removing all non-letter characters");
|
|
keyword = keyword.replaceAll("[^A-Z]", "");
|
|
|
|
//Save the string
|
|
logger.debug("Clean keyword '{}'", keyword);
|
|
this.keyword = keyword;
|
|
|
|
//Make sure offset is empty before adding to it
|
|
offset.clear();
|
|
setOffset();
|
|
|
|
//If after all the eliminating of unusable characters the keyword is empty throw an exception
|
|
if(this.keyword.length() < 2){
|
|
throw new InvalidKeywordException("Keyword must contain at least 2 letters");
|
|
}
|
|
}
|
|
//Encodes inputString and stores the result in outputString
|
|
protected void encode(){
|
|
logger.debug("Encoding");
|
|
|
|
StringBuilder output = new StringBuilder();
|
|
|
|
//Step through every character in the inputString and advance it the correct amount, according to offset
|
|
int offsetCnt = 0;
|
|
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
|
|
char letter = inputString.charAt(inputCnt);
|
|
logger.debug("Working character {}", letter);
|
|
|
|
if(Character.isUpperCase(letter)){
|
|
logger.debug("Encoding uppercase");
|
|
|
|
letter += offset.get((offsetCnt++) % offset.size());
|
|
|
|
//Make sure the character is still a letter, if not, wrap around
|
|
if(letter > 'Z'){
|
|
logger.debug("Wrapping around to A");
|
|
|
|
letter -= 26;
|
|
}
|
|
}
|
|
else if(Character.isLowerCase(letter)){
|
|
logger.debug("Encoding lowercase");
|
|
|
|
letter += offset.get((offsetCnt++) % offset.size());
|
|
|
|
//Make sure the character is still a letter, if not, wrap around
|
|
if(letter > 'z'){
|
|
logger.debug("Wrapping around to a");
|
|
|
|
letter -= 26;
|
|
}
|
|
}
|
|
|
|
logger.debug("Encoded character {}", letter);
|
|
output.append(letter);
|
|
}
|
|
|
|
//Save output
|
|
outputString = output.toString();
|
|
logger.debug("Encoded message '{}'", outputString);
|
|
}
|
|
//Decodes inputString and stores the result in outputString
|
|
protected void decode(){
|
|
logger.debug("Decoding");
|
|
|
|
StringBuilder output = new StringBuilder();
|
|
|
|
//Step through every character in the inputString and advance it the correct amount, according to offset
|
|
int offsetCnt = 0;
|
|
for(int letterCnt = 0;letterCnt < inputString.length();++letterCnt){
|
|
char letter = inputString.charAt(letterCnt);
|
|
|
|
logger.debug("Working character {}", letter);
|
|
|
|
if(Character.isUpperCase(letter)){
|
|
logger.debug("Decoding uppercase");
|
|
|
|
letter -= offset.get((offsetCnt++) % offset.size());
|
|
|
|
if(letter < 'A'){
|
|
logger.debug("Wrapping around to Z");
|
|
|
|
letter += 26;
|
|
}
|
|
}
|
|
else if(Character.isLowerCase(letter)){
|
|
logger.debug("Decoding lowercase");
|
|
|
|
letter -= offset.get((offsetCnt++) % offset.size());
|
|
|
|
if(letter < 'a'){
|
|
logger.debug("Wrapping around to z");
|
|
|
|
letter += 26;
|
|
}
|
|
}
|
|
|
|
//Add letter to output
|
|
logger.debug("Decoded character {}", letter);
|
|
output.append(letter);
|
|
}
|
|
|
|
//Save output
|
|
outputString = output.toString();
|
|
logger.debug("Decoded message '{}'", outputString);
|
|
}
|
|
|
|
|
|
//Constructor
|
|
public Vigenere(){
|
|
offset = new ArrayList<>();
|
|
reset();
|
|
preserveCapitals = false;
|
|
preserveWhitespace = false;
|
|
preserveSymbols = false;
|
|
}
|
|
public Vigenere(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
|
offset = new ArrayList<>();
|
|
reset();
|
|
this.preserveCapitals = preserveCapitals;
|
|
this.preserveWhitespace = preserveWhitespace;
|
|
this.preserveSymbols = preserveSymbols;
|
|
}
|
|
|
|
//Encodes input using key and returns the result
|
|
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
|
reset();
|
|
setKeyword(keyword);
|
|
setInputString(inputString);
|
|
encode();
|
|
return outputString;
|
|
}
|
|
//Decodes input using key and returns the result
|
|
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
|
reset();
|
|
setKeyword(keyword);
|
|
setInputString(inputString);
|
|
decode();
|
|
return outputString;
|
|
}
|
|
|
|
//Getters
|
|
public String getInputString(){
|
|
return inputString;
|
|
}
|
|
public String getOutputString(){
|
|
return outputString;
|
|
}
|
|
public String getKeyword(){
|
|
return keyword;
|
|
}
|
|
public List<Integer> getOffsets(){
|
|
return offset;
|
|
}
|
|
|
|
//Makes sure all variables are empty
|
|
public void reset(){
|
|
logger.debug("Resetting fields");
|
|
|
|
inputString = "";
|
|
outputString = "";
|
|
keyword = "";
|
|
offset.clear();
|
|
}
|
|
}
|