Files
CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Caesar.java
2022-07-04 01:04:06 -04:00

161 lines
4.9 KiB
Java

//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Caesar.java
//Matthew Ellison
// Created: 07-25-21
//Modified: 02-17-22
package com.mattrixwv.cipherstream.monosubstitution;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
public class Caesar{
private String inputString; //The string that needs encoded/decoded
private String outputString; //The encoded/decoded string
private int shift; //The amount that you need to shift each letter
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
//Sets shift and makes sure it is within the propper bounds
private void setShift(int shiftAmount){
//If you shift more than 26 you will just be wrapping back around again
shift = shiftAmount % 26;
}
//Sets the input string
private void setInputString(String inputString) throws InvalidInputException{
if(inputString == null){
throw new NullPointerException("Input cannot be null");
}
if(!preserveCapitals){
inputString = inputString.toLowerCase();
}
if(!preserveWhitespace){
inputString = inputString.replaceAll("\\s", "");
}
if(!preserveSymbols){
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
}
this.inputString = inputString;
if(this.inputString.isBlank()){
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
//Encodes the inputString and stores the result in outputString
private String encode(){
StringBuilder output = new StringBuilder();
for(int cnt = 0;cnt < inputString.length();++cnt){
char currentChar = inputString.charAt(cnt); //A temperary holder for the current working character
//If it is an upper case letter shift it and wrap if necessary
if(Character.isUpperCase(currentChar)){
currentChar += shift;
//Wrap around if the letter is now out of bounds
if(currentChar < 'A'){
currentChar += 26;
}
else if(currentChar > 'Z'){
currentChar -= 26;
}
}
//If it is a lower case letter shift it and wrap if necessary
else if(Character.isLowerCase(currentChar)){
currentChar += shift;
//Wrap around if the letter is now out of bounds
if(currentChar < 'a'){
currentChar += 26;
}
else if(currentChar > 'z'){
currentChar -= 26;
}
}
//If it is whitespace, number, or punctuation just let it pass through
//Add it to the output string
output.append(currentChar);
}
outputString = output.toString();
return outputString;
}
//Decodes the inputString and stores the result in outputString
private String decode(){
StringBuilder output = new StringBuilder();
for(int cnt = 0;cnt < inputString.length();++cnt){
char currentChar = inputString.charAt(cnt); //A temperary holder for the current working character
//If it is an upper case letter shift it and wrap if necessary
if(Character.isUpperCase(currentChar)){
currentChar -= shift;
//Wrap around if the letter is now out of bounds
if(currentChar < 'A'){
currentChar += 26;
}
else if(currentChar > 'Z'){
currentChar -= 26;
}
}
//If it is a lower case letter shift it and wrap if necessary
else if(Character.isLowerCase(currentChar)){
currentChar -= shift;
//Wrap around if the letter is now out of bounds
if(currentChar < 'a'){
currentChar += 26;
}
else if(currentChar > 'z'){
currentChar -= 26;
}
}
//If it is whitespace, number, or punctuation just let it pass through
//Add it to the output string
output.append(currentChar);
}
outputString = output.toString();
return outputString;
}
//Constructor
public Caesar(){
reset();
preserveCapitals = false;
preserveWhitespace = false;
preserveSymbols = false;
}
public Caesar(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
reset();
this.preserveCapitals = preserveCapitals;
this.preserveWhitespace = preserveWhitespace;
this.preserveSymbols = preserveSymbols;
}
//Returns the inputString
public String getInputString(){
return inputString;
}
//Returns shift
public int getShift(){
return shift;
}
//Returns the outputString
public String getOutputString(){
return outputString;
}
//Sets the shift and inputString and encodes the message
public String encode(int shiftAmount, String inputString) throws InvalidInputException{
reset();
setShift(shiftAmount);
setInputString(inputString);
return encode();
}
//Sets the shift and inputString and decodes the message
public String decode(int shiftAmount, String inputString) throws InvalidInputException{
reset();
setShift(shiftAmount);
setInputString(inputString);
return decode();
}
//Makes sure all of the variables are empty
public void reset(){
inputString = outputString = "";
shift = 0;
}
}