//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Atbash.java //Mattrixwv // Created: 07-25-21 //Modified: 05-04-23 package com.mattrixwv.cipherstream.monosubstitution; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.mattrixwv.cipherstream.exceptions.InvalidInputException; public class Atbash{ private static final Logger logger = LoggerFactory.getLogger(Atbash.class); //Fields protected String inputString; //Holds the string that needs encoded or decoded protected String outputString; //The encoded/decoded string //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 //Encodes inputString and stores in outputString protected void encode(){ logger.debug("Encoding"); StringBuilder output = new StringBuilder(); //Step through every element in the inputString and shift it the correct amount for(int cnt = 0;cnt < inputString.length();++cnt){ char currentChar = inputString.charAt(cnt); logger.debug("Encoding char {}", currentChar); //Decode if the letter is alphabetic if(Character.isAlphabetic(currentChar)){ //Use either uppercase or lowercase for the base //(letterbase + 25 - (currentChar - letterBase)) if(Character.isUpperCase(currentChar)){ logger.debug("Encoding uppercase"); output.append((char)(155 - currentChar)); } else{ logger.debug("Encoding lowercase"); output.append((char)(219 - currentChar)); } } //Keep any punctuatio/whitespace the way it is else{ logger.debug("Appending symbol"); output.append(currentChar); } } //Return the output outputString = output.toString(); logger.debug("Saving output string '{}'", outputString); } //Removes all invalid characters and 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"); //Convert all letters to lowercase inputString = inputString.toUpperCase(); } if(!preserveWhitespace){ logger.debug("Removing whitespace"); //Remove all characters except capital letters inputString = inputString.replaceAll("\\s", ""); } if(!preserveSymbols){ logger.debug("Removing symbols"); //Remove all non-alpha numeric and whitespace symbols inputString = inputString.replaceAll("[^a-zA-Z\\s]", ""); } //Save the string this.inputString = inputString; logger.debug("Cleaned input string '{}'", inputString); if(this.inputString.isBlank()){ throw new InvalidInputException("Input must contain at least 1 character"); } } //Constructor public Atbash(){ reset(); preserveCapitals = false; preserveWhitespace = false; preserveSymbols = false; } public Atbash(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){ reset(); this.preserveCapitals = preserveCapitals; this.preserveWhitespace = preserveWhitespace; this.preserveSymbols = preserveSymbols; } //Encodes inputString and returns the result public String encode(String inputString) throws InvalidInputException{ //Make sure everything is empty before you begin reset(); setInputString(inputString); encode(); return outputString; } //Decodes inputString and returns the result public String decode(String inputString) throws InvalidInputException{ return encode(inputString); } //Getters public String getInputString(){ return inputString; } public String getOutputString(){ return outputString; } //Makes sure all variables are empty public void reset(){ logger.debug("Resetting fields"); inputString = ""; outputString = ""; } }