Added exceptions

This commit is contained in:
2022-01-25 23:25:38 -05:00
parent c65947a630
commit 87c5eb093e
15 changed files with 1715 additions and 1619 deletions

View File

@@ -4,11 +4,12 @@
//Modified: 01-16-22
package com.mattrixwv.CipherStreamJava.monoSubstitution;
import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException;
import com.mattrixwv.CipherStreamJava.exceptions.InvalidKeywordException;
public class Autokey extends Vigenere{
//Special rules for setting the strings for encoding
private void encodeSet(String key, String input) throws InvalidKeywordException{
private void encodeSet(String key, String input) throws InvalidKeywordException, InvalidInputException{
//Set the input
setInputString(input);
@@ -30,11 +31,10 @@ public class Autokey extends Vigenere{
setOffset();
}
//Setting the strings for decoding
private void decodeSet(String key, String input) throws InvalidKeywordException{
private void decodeSet(String key, String input) throws InvalidKeywordException, InvalidInputException{
//Remove all unneccessary elements from the key
setKeyword(key);
//Remove all unneccessary elements from the input
inputString = "";
setInputString(input);
}
//Decodes the inputString
@@ -91,15 +91,14 @@ public class Autokey extends Vigenere{
super(preserveCapitals, preserveWhitespace, preserveSymbols);
}
//Encodes inputString using the Autokey cipher
public String encode(String key, String input) throws InvalidKeywordException{
public String encode(String key, String input) throws InvalidKeywordException, InvalidInputException{
reset();
encodeSet(key, input);
return encode();
}
//Decodes inputString using the Autokey cipher
public String decode(String key, String input) throws InvalidKeywordException{
public String decode(String key, String input) throws InvalidKeywordException, InvalidInputException{
reset();
setInputString(input);
decodeSet(key, input);
return decode();
}

View File

@@ -9,6 +9,7 @@ import java.util.Arrays;
import java.util.StringJoiner;
import com.mattrixwv.CipherStreamJava.exceptions.InvalidCharacterException;
import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException;
public class Baconian{
private static final ArrayList<String> code = new ArrayList<String>(Arrays.asList(
@@ -20,7 +21,11 @@ public class Baconian{
private boolean preserveCapitals; //Whether to respect capitals in the output string
//Sets the input string
private void setInputStringEncode(String inputString){
private void setInputStringEncode(String inputString) throws InvalidInputException{
if(inputString == null){
throw new NullPointerException("Input cannot be null");
}
//Remove all whitespace and symbols
inputString = inputString.replaceAll("[^A-Za-z]", "");
if(!preserveCapitals){
@@ -28,8 +33,16 @@ public class Baconian{
}
this.inputString = inputString;
if(this.inputString.isBlank()){
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
private void setInputStringDecode(String inputString) throws InvalidCharacterException{
private void setInputStringDecode(String inputString) throws InvalidCharacterException, InvalidInputException{
if(inputString == null){
throw new NullPointerException("Input cannot be null");
}
if(!preserveCapitals){
inputString = inputString.toLowerCase();
}
@@ -48,6 +61,10 @@ public class Baconian{
}
this.inputString = inputString;
if(this.inputString.isBlank()){
throw new InvalidInputException("Input cannot be null");
}
}
//Encodes the inputString and stores the result in outputString
private String encode(){
@@ -115,13 +132,13 @@ public class Baconian{
return inputString;
}
//Sets the inputString and encodes the message
public String encode(String inputString){
public String encode(String inputString) throws InvalidInputException{
reset();
setInputStringEncode(inputString);
return encode();
}
//Sets the inputString and decodes the message
public String decode(String inputString) throws InvalidCharacterException{
public String decode(String inputString) throws InvalidCharacterException, InvalidInputException{
reset();
setInputStringDecode(inputString);
return decode();

View File

@@ -9,6 +9,7 @@ import java.util.StringJoiner;
import com.mattrixwv.CipherStreamJava.exceptions.InvalidBaseException;
import com.mattrixwv.CipherStreamJava.exceptions.InvalidCharacterException;
import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException;
public class BaseX{
@@ -17,10 +18,22 @@ public class BaseX{
private int base; //The base that the number will be encoded at
//Sets the input string
private void setInputStringEncode(String inputString){
private void setInputStringEncode(String inputString) throws InvalidInputException{
if(inputString == null){
throw new NullPointerException("Input cannot be null");
}
this.inputString = inputString;
if(this.inputString.isBlank()){
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
private void setInputStringDecode(String inputString) throws InvalidCharacterException{
private void setInputStringDecode(String inputString) throws InvalidCharacterException, InvalidInputException{
if(inputString == null){
throw new NullPointerException("Input cannot be null");
}
StringBuilder validNumbers = new StringBuilder();
for(int cnt = 0;cnt < base;++cnt){
validNumbers.append(Integer.toString(cnt, base).toUpperCase());
@@ -29,6 +42,10 @@ public class BaseX{
if(!this.inputString.equals(inputString)){
throw new InvalidCharacterException("inputString cannot contain anything except numbers 0-" + Integer.toString(base - 1, base) + ", and whitespace");
}
if(this.inputString.isBlank()){
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
//Sets the numeric base
private void setBase(int base) throws InvalidBaseException{
@@ -100,24 +117,24 @@ public class BaseX{
return base;
}
//Sets the inputString and encodes the message
public String encode(String inputString){
public String encode(String inputString) throws InvalidInputException{
reset();
setInputStringEncode(inputString);
return encode();
}
public String encode(int base, String inputString) throws InvalidBaseException{
public String encode(int base, String inputString) throws InvalidBaseException, InvalidInputException{
reset();
setBase(base);
setInputStringEncode(inputString);
return encode();
}
//Sets the inputString and decodes the message
public String decode(String inputString) throws InvalidCharacterException{
public String decode(String inputString) throws InvalidCharacterException, InvalidInputException{
reset();
setInputStringDecode(inputString);
return decode();
}
public String decode(int base, String inputString) throws InvalidBaseException, InvalidCharacterException{
public String decode(int base, String inputString) throws InvalidBaseException, InvalidCharacterException, InvalidInputException{
reset();
setBase(base);
setInputStringDecode(inputString);

View File

@@ -5,6 +5,9 @@
package com.mattrixwv.CipherStreamJava.monoSubstitution;
import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException;
public class Caesar{
private String inputString; //The string that needs encoded/decoded
private String outputString; //The encoded/decoded string
@@ -18,7 +21,11 @@ public class Caesar{
shift = shiftAmount % 26;
}
//Sets the input string
private void setInputString(String inputString){
private void setInputString(String inputString) throws InvalidInputException{
if(inputString == null){
throw new NullPointerException("Input cannot be null");
}
if(!preserveCapitals){
inputString = inputString.toLowerCase();
}
@@ -26,9 +33,14 @@ public class Caesar{
inputString = inputString.replaceAll("\\s+", "");
}
if(!preserveSymbols){
inputString = inputString.replaceAll("[^a-zA-Z0-9\\s]", "");
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(){
@@ -127,14 +139,14 @@ public class Caesar{
return outputString;
}
//Sets the shift and inputString and encodes the message
public String encode(int shiftAmount, String inputString){
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){
public String decode(int shiftAmount, String inputString) throws InvalidInputException{
reset();
setShift(shiftAmount);
setInputString(inputString);

View File

@@ -7,6 +7,7 @@ package com.mattrixwv.CipherStreamJava.monoSubstitution;
import java.util.ArrayList;
import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException;
import com.mattrixwv.CipherStreamJava.exceptions.InvalidKeywordException;
@@ -31,7 +32,11 @@ public class Vigenere{
}
}
//Sets inputString
protected void setInputString(String inputString){
protected void setInputString(String inputString) throws InvalidInputException{
if(inputString == null){
throw new NullPointerException("Input cannot be null");
}
if(!preserveCapitals){
inputString = inputString.toLowerCase();
}
@@ -39,25 +44,35 @@ public class Vigenere{
inputString = inputString.replaceAll("\\s+", "");
}
if(!preserveSymbols){
inputString = inputString.replaceAll("[^a-zA-Z0-9\\s]", "");
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
}
this.inputString = inputString;
if(this.inputString.isBlank()){
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
//Sets keyword
protected void setKeyword(String key) throws InvalidKeywordException{
protected void setKeyword(String keyword) throws InvalidKeywordException{
if(keyword == null){
throw new NullPointerException("Keyword cannot be null");
}
//Convert all letters to uppercase
key = key.toUpperCase();
keyword = keyword.toUpperCase();
//Remove all characters except capital letters
key = key.replaceAll("[^A-Z]", "");
keyword = keyword.replaceAll("[^A-Z]", "");
//Save the string
keyword = key;
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(keyword == ""){
throw new InvalidKeywordException("The keyword cannot be empty");
if(this.keyword.isBlank()){
throw new InvalidKeywordException("Keyword must contain at least 2 letters");
}
}
//Encodes inputString and stores the result in outputString
@@ -160,14 +175,14 @@ public class Vigenere{
return offset;
}
//Encodes input using key and returns the result
public String encode(String key, String input) throws InvalidKeywordException{
public String encode(String key, String input) throws InvalidKeywordException, InvalidInputException{
reset();
setKeyword(key);
setInputString(input);
return encode();
}
//Decodes input using key and returns the result
public String decode(String key, String input) throws InvalidKeywordException{
public String decode(String key, String input) throws InvalidKeywordException, InvalidInputException{
reset();
setKeyword(key);
setInputString(input);

View File

@@ -47,10 +47,10 @@ public class Playfair{
}
}
//Strips invalid characters from the string that needs encoded/decoded
private void setInputString(String inputString, boolean encoding) throws InvalidCharacterException{
private void setInputString(String inputString, boolean encoding) throws InvalidCharacterException, InvalidInputException{
//Make sure the input string is not null
if(inputString == null){
throw new InvalidCharacterException("The input string cannot be null");
throw new NullPointerException("The input string cannot be null");
}
//Set the options
@@ -61,7 +61,7 @@ public class Playfair{
inputString = inputString.replaceAll("\\s+", "");
}
if(!preserveSymbols){
inputString = inputString.replaceAll("[^a-zA-Z0-9\\s]", "");
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
}
//Make replace all of the replacers with replaced
@@ -146,6 +146,10 @@ public class Playfair{
this.inputString = inputString;
}
if(this.inputString.isBlank() || getPreparedInputString().isBlank()){
throw new InvalidInputException("Input must have at least 1 letter");
}
}
//Returns the input string ready for encoding
private String getPreparedInputString(){
@@ -154,23 +158,27 @@ public class Playfair{
return cleanString;
}
//Strips invalid characters from the keyword and creates the grid
private void setKeyword(String key){
private void setKeyword(String keyword){
if(keyword == null){
throw new NullPointerException("Keyword cannot be null");
}
//Change everything to uppercase
key = key.toUpperCase();
keyword = keyword.toUpperCase();
//Removing everything except capital letters
key = key.replaceAll("[^A-Z]", "");
keyword = keyword.replaceAll("[^A-Z]", "");
//Add all letters in the alphabet to the key
key += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
keyword += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//Replace all replaced characters
key = key.replaceAll(Character.toString(replaced), Character.toString(replacer));
keyword = keyword.replaceAll(Character.toString(replaced), Character.toString(replacer));
//Remove all duplicate chatacters
StringBuilder uniqueKey = new StringBuilder();
key.chars().distinct().forEach(c -> uniqueKey.append((char)c));
keyword = uniqueKey.toString();
keyword.chars().distinct().forEach(c -> uniqueKey.append((char)c));
this.keyword = uniqueKey.toString();
//Create the grid from the sanitized keyword
createGrid();

View File

@@ -35,8 +35,8 @@ public class PolybiusSquare{
private char[][] grid; //The grid used to encode/decode the message
private char replaced; //The letter that will need to be replaced in the grid and any input string or keyword
private char replacer; //The letter that replaces replaced in the input string or keyword
private boolean leaveWhitespace; //Whether to respect whitespace in the output string
private boolean leaveSymbols; //Whether to respect symbols 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
//Create the grid from the keyword
public void createGrid(){
@@ -48,7 +48,11 @@ public class PolybiusSquare{
}
}
//Strips invalid characters from the string that needs encoded/decoded
public void setInputStringEncoding(String inputString) throws InvalidCharacterException{
public void setInputStringEncoding(String inputString) throws InvalidCharacterException, InvalidInputException{
if(inputString == null){
throw new NullPointerException("Input cannot be null");
}
//Make sure the string doesn't contain any numbers
for(char ch = '0';ch <= '9';++ch){
if(inputString.contains(Character.toString(ch))){
@@ -60,16 +64,16 @@ public class PolybiusSquare{
inputString = inputString.toUpperCase();
//Remove any whitespace if selected
if(!leaveWhitespace){
if(!preserveWhitespace){
inputString = inputString.replaceAll("\\s+", "");
}
//Remove any symbols if selected
if(!leaveSymbols){
inputString = inputString.replaceAll("[^a-zA-Z0-9\\s]", "");
if(!preserveSymbols){
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
}
if(!leaveWhitespace && !leaveSymbols){
if(!preserveWhitespace && !preserveSymbols){
//Add whitespace after every character for the default look
StringJoiner spacedString = new StringJoiner(" ");
for(int cnt = 0;cnt < inputString.length();++cnt){
@@ -83,8 +87,15 @@ public class PolybiusSquare{
//Save the string
this.inputString = inputString;
if(this.inputString.isBlank() || getPreparedInputStringEncoding().isBlank()){
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
public void setInputStringDecoding(String inputString) throws InvalidCharacterException{
public void setInputStringDecoding(String inputString) throws InvalidCharacterException, InvalidInputException{
if(inputString == null){
throw new NullPointerException("Input cannot be null");
}
//Make sure the string contains an even number of digits and no letters
int numberOfDigits = 0;
for(int cnt = 0;cnt < inputString.length();++cnt){
@@ -101,17 +112,21 @@ public class PolybiusSquare{
}
//Remove any whitespace if selected
if(!leaveWhitespace){
if(!preserveWhitespace){
inputString = inputString.replaceAll("\\s+", "");
}
//Remove any symbols if selected
if(!leaveSymbols){
if(!preserveSymbols){
inputString = inputString.replaceAll("[^0-9\\s]", "");
}
//Save the string
this.inputString = inputString;
if(this.inputString.isBlank() || getPreparedInputStringDecoding().isBlank()){
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
//Returns the input string ready for encoding
public String getPreparedInputStringEncoding(){
@@ -124,23 +139,26 @@ public class PolybiusSquare{
return cleanString;
}
//Strips invalid characters from the keyword and creates the grid
public void setKeyword(String key){
public void setKeyword(String keyword){
if(keyword == null){
throw new NullPointerException("Keyword cannot be null");
}
//Change everything to uppercase
key = key.toUpperCase();
keyword = keyword.toUpperCase();
//Remove everything except capital letters
key = key.replaceAll("[^A-Z]", "");
keyword = keyword.replaceAll("[^A-Z]", "");
//Add all letters in the alphabet to the key
key += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
keyword += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//Replace all replaced characters
key = key.replaceAll(Character.toString(replaced), Character.toString(replacer));
keyword = keyword.replaceAll(Character.toString(replaced), Character.toString(replacer));
//Remove all duplicate characters
StringBuilder uniqueKey = new StringBuilder();
key.chars().distinct().forEach(c -> uniqueKey.append((char)c));
keyword = uniqueKey.toString();
keyword.chars().distinct().forEach(c -> uniqueKey.append((char)c));
this.keyword = uniqueKey.toString();
//Create the grid from the sanitized keyword
createGrid();
@@ -239,22 +257,22 @@ public class PolybiusSquare{
reset();
setReplaced('J');
setReplacer('I');
leaveWhitespace = false;
leaveSymbols = false;
preserveWhitespace = false;
preserveSymbols = false;
}
public PolybiusSquare(boolean leaveWhitespace, boolean leaveSymbols) throws InvalidCharacterException{
public PolybiusSquare(boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
reset();
setReplaced('J');
setReplacer('I');
this.leaveWhitespace = leaveWhitespace;
this.leaveSymbols = leaveSymbols;
this.preserveWhitespace = preserveWhitespace;
this.preserveSymbols = preserveSymbols;
}
public PolybiusSquare(boolean leaveWhitespace, boolean leaveSymbols, char replaced, char replacer) throws InvalidCharacterException{
public PolybiusSquare(boolean preserveWhitespace, boolean preserveSymbols, char replaced, char replacer) throws InvalidCharacterException{
reset();
setReplaced(replaced);
setReplacer(replacer);
this.leaveWhitespace = leaveWhitespace;
this.leaveSymbols = leaveSymbols;
this.preserveWhitespace = preserveWhitespace;
this.preserveSymbols = preserveSymbols;
}
//Sets the keyword and inputString and encodes the message
public String encode(String inputString) throws InvalidCharacterException, InvalidInputException{
@@ -267,10 +285,10 @@ public class PolybiusSquare{
return encode();
}
//Sets the keyword and inputString and decodes the message
public String decode(String inputString) throws InvalidCharacterException{
public String decode(String inputString) throws InvalidCharacterException, InvalidInputException{
return decode("", inputString);
}
public String decode(String keyword, String inputString) throws InvalidCharacterException{
public String decode(String keyword, String inputString) throws InvalidCharacterException, InvalidInputException{
reset();
setKeyword(keyword);
setInputStringDecoding(inputString);