Replaced generic exceptions with custom exceptions
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Exceptions/InvalidBaseException.java
|
||||
//Mattrixwv
|
||||
// Created: 01-09-22
|
||||
//Modified: 01-09-22
|
||||
package com.mattrixwv.CipherStreamJava.exceptions;
|
||||
|
||||
|
||||
public class InvalidBaseException extends Exception{
|
||||
public InvalidBaseException(){
|
||||
super();
|
||||
}
|
||||
public InvalidBaseException(String message){
|
||||
super(message);
|
||||
}
|
||||
public InvalidBaseException(Throwable error){
|
||||
super(error);
|
||||
}
|
||||
public InvalidBaseException(String message, Throwable error){
|
||||
super(message, error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Exceptions/InvalidInputException.java
|
||||
//Mattrixwv
|
||||
// Created: 01-09-22
|
||||
//Modified: 01-09-22
|
||||
package com.mattrixwv.CipherStreamJava.exceptions;
|
||||
|
||||
|
||||
public class InvalidInputException extends Exception{
|
||||
public InvalidInputException(){
|
||||
super();
|
||||
}
|
||||
public InvalidInputException(String message){
|
||||
super(message);
|
||||
}
|
||||
public InvalidInputException(Throwable error){
|
||||
super(error);
|
||||
}
|
||||
public InvalidInputException(String message, Throwable error){
|
||||
super(message, error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Exceptions/InvalidKeywordException.java
|
||||
//Mattrixwv
|
||||
// Created: 01-09-22
|
||||
//Modified: 01-09-22
|
||||
package com.mattrixwv.CipherStreamJava.exceptions;
|
||||
|
||||
|
||||
public class InvalidKeywordException extends Exception{
|
||||
public InvalidKeywordException(){
|
||||
super();
|
||||
}
|
||||
public InvalidKeywordException(String message){
|
||||
super(message);
|
||||
}
|
||||
public InvalidKeywordException(Throwable error){
|
||||
super(error);
|
||||
}
|
||||
public InvalidKeywordException(String message, Throwable error){
|
||||
super(message, error);
|
||||
}
|
||||
}
|
||||
@@ -49,6 +49,7 @@ public class Atbash{
|
||||
if(Character.isUpperCase(currentChar)){
|
||||
letterBase = 'A';
|
||||
}
|
||||
//TODO: Test and see if there is a more efficient way to do this
|
||||
output.append((char)(currentChar + 25 - (2 * (currentChar - letterBase))));
|
||||
}
|
||||
//Keep any punctuatio/whitespace the way it is
|
||||
|
||||
@@ -4,9 +4,11 @@
|
||||
//Modified: 01-04-22
|
||||
package com.mattrixwv.CipherStreamJava.monoSubstitution;
|
||||
|
||||
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 Exception{
|
||||
private void encodeSet(String key, String input) throws InvalidKeywordException{
|
||||
//Set the input
|
||||
setInputString(input);
|
||||
|
||||
@@ -28,7 +30,7 @@ public class Autokey extends Vigenere{
|
||||
setOffset();
|
||||
}
|
||||
//Setting the strings for decoding
|
||||
private void decodeSet(String key, String input) throws Exception{
|
||||
private void decodeSet(String key, String input) throws InvalidKeywordException{
|
||||
//Remove all unneccessary elements from the key
|
||||
setKeyword(key);
|
||||
//Remove all unneccessary elements from the input
|
||||
@@ -36,7 +38,7 @@ public class Autokey extends Vigenere{
|
||||
setInputString(input);
|
||||
}
|
||||
//Decodes the inputString
|
||||
protected String decode() throws Exception{
|
||||
protected String decode() throws InvalidKeywordException{
|
||||
//Decode what the key will allow, add that to the key and continue
|
||||
StringBuilder currentOutput = new StringBuilder();
|
||||
StringBuilder fullOutput = new StringBuilder();
|
||||
@@ -89,13 +91,13 @@ public class Autokey extends Vigenere{
|
||||
super(leaveCapitals, leaveWhitespace, leaveSymbols);
|
||||
}
|
||||
//Encodes inputString using the Autokey cipher
|
||||
public String encode(String key, String input) throws Exception{
|
||||
public String encode(String key, String input) throws InvalidKeywordException{
|
||||
reset();
|
||||
encodeSet(key, input);
|
||||
return encode();
|
||||
}
|
||||
//Decodes inputString using the Autokey cipher
|
||||
public String decode(String key, String input) throws Exception{
|
||||
public String decode(String key, String input) throws InvalidKeywordException{
|
||||
reset();
|
||||
setInputString(input);
|
||||
decodeSet(key, input);
|
||||
|
||||
@@ -7,6 +7,7 @@ package com.mattrixwv.CipherStreamJava.monoSubstitution;
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidBaseException;
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidCharacterException;
|
||||
|
||||
|
||||
@@ -30,10 +31,9 @@ public class BaseX{
|
||||
}
|
||||
}
|
||||
//Sets the numeric base
|
||||
private void setBase(int base) throws Exception{
|
||||
private void setBase(int base) throws InvalidBaseException{
|
||||
if(base <= 0){
|
||||
//TODO: Change this to a custom exception. InvalidParameter?
|
||||
throw new Exception("Base cannot be a negative number");
|
||||
throw new InvalidBaseException("Base cannot be a negative number");
|
||||
}
|
||||
|
||||
this.base = base;
|
||||
@@ -79,11 +79,11 @@ public class BaseX{
|
||||
}
|
||||
|
||||
//Constructor
|
||||
public BaseX() throws Exception{
|
||||
public BaseX() throws InvalidBaseException{
|
||||
reset();
|
||||
setBase(2);
|
||||
}
|
||||
public BaseX(int base) throws Exception{
|
||||
public BaseX(int base) throws InvalidBaseException{
|
||||
reset();
|
||||
setBase(base);
|
||||
}
|
||||
@@ -105,7 +105,7 @@ public class BaseX{
|
||||
setInputStringEncode(inputString);
|
||||
return encode();
|
||||
}
|
||||
public String encode(int base, String inputString) throws Exception{
|
||||
public String encode(int base, String inputString) throws InvalidBaseException{
|
||||
reset();
|
||||
setBase(base);
|
||||
setInputStringEncode(inputString);
|
||||
@@ -117,7 +117,7 @@ public class BaseX{
|
||||
setInputStringDecode(inputString);
|
||||
return decode();
|
||||
}
|
||||
public String decode(int base, String inputString) throws Exception{
|
||||
public String decode(int base, String inputString) throws InvalidBaseException, InvalidCharacterException{
|
||||
reset();
|
||||
setBase(base);
|
||||
setInputStringDecode(inputString);
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Vigenere.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-25-21
|
||||
//Modified: 01-04-22
|
||||
//Modified: 01-09-22
|
||||
package com.mattrixwv.CipherStreamJava.monoSubstitution;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
public class Vigenere{
|
||||
protected String inputString; //This is the string that needs encoded/decoded
|
||||
@@ -42,7 +44,7 @@ public class Vigenere{
|
||||
this.inputString = inputString;
|
||||
}
|
||||
//Sets keyword
|
||||
protected void setKeyword(String key) throws Exception{
|
||||
protected void setKeyword(String key) throws InvalidKeywordException{
|
||||
//Convert all letters to uppercase
|
||||
key = key.toUpperCase();
|
||||
//Remove all characters except capital letters
|
||||
@@ -55,7 +57,7 @@ public class Vigenere{
|
||||
setOffset();
|
||||
//If after all the eliminating of unusable characters the keyword is empty throw an exception
|
||||
if(keyword == ""){
|
||||
throw new Exception("The keyword cannot be empty");
|
||||
throw new InvalidKeywordException("The keyword cannot be empty");
|
||||
}
|
||||
}
|
||||
//Encodes inputString and stores the result in outputString
|
||||
@@ -93,7 +95,7 @@ public class Vigenere{
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString and stores the result in outputString
|
||||
protected String decode() throws Exception{
|
||||
protected String decode() throws InvalidKeywordException{
|
||||
StringBuilder output = new StringBuilder();
|
||||
|
||||
//Step through every character in the inputString and advance it the correct amount, according to offset
|
||||
@@ -158,14 +160,14 @@ public class Vigenere{
|
||||
return offset;
|
||||
}
|
||||
//Encodes input using key and returns the result
|
||||
public String encode(String key, String input) throws Exception{
|
||||
public String encode(String key, String input) throws InvalidKeywordException{
|
||||
reset();
|
||||
setKeyword(key);
|
||||
setInputString(input);
|
||||
return encode();
|
||||
}
|
||||
//Decodes input using key and returns the result
|
||||
public String decode(String key, String input) throws Exception{
|
||||
public String decode(String key, String input) throws InvalidKeywordException{
|
||||
reset();
|
||||
setKeyword(key);
|
||||
setInputString(input);
|
||||
@@ -173,7 +175,9 @@ public class Vigenere{
|
||||
}
|
||||
//Makes sure all of the variables are empty
|
||||
public void reset(){
|
||||
inputString = outputString = keyword = "";
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
keyword = "";
|
||||
offset.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Playfair.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-30-21
|
||||
//Modified: 01-04-22
|
||||
//Modified: 01-09-22
|
||||
package com.mattrixwv.CipherStreamJava.polySubstitution;
|
||||
|
||||
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidCharacterException;
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException;
|
||||
|
||||
|
||||
public class Playfair{
|
||||
@@ -175,7 +176,7 @@ public class Playfair{
|
||||
createGrid();
|
||||
}
|
||||
//Returns the location of the given character in the grid
|
||||
private CharLocation findChar(char letter) throws Exception{
|
||||
private CharLocation findChar(char letter) throws InvalidInputException{
|
||||
for(int row = 0;row < grid.length;++row){
|
||||
for(int col = 0;col < grid[row].length;++col){
|
||||
if(grid[row][col] == letter){
|
||||
@@ -184,7 +185,7 @@ public class Playfair{
|
||||
}
|
||||
}
|
||||
//If it was not found something went wrong
|
||||
throw new Exception("That character was not found in the grid. ERROR");
|
||||
throw new InvalidInputException("The character '" + letter + "' was not found in the grid");
|
||||
}
|
||||
//Returns the location in the grid of x and y, adjusting for out of bounds
|
||||
private char getGridChar(int x, int y){
|
||||
@@ -214,7 +215,7 @@ public class Playfair{
|
||||
outputString = fullOutput.toString();
|
||||
}
|
||||
//Encodes inputString using the Playfair cipher and stores the result in outputString
|
||||
private String encode() throws Exception{
|
||||
private String encode() throws InvalidInputException{
|
||||
StringBuilder output = new StringBuilder();
|
||||
int inputCnt = 0;
|
||||
String cleanString = getPreparedInputString();
|
||||
@@ -253,7 +254,7 @@ public class Playfair{
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString using the Playfair cipher and stores the result in outputString
|
||||
private String decode() throws Exception{
|
||||
private String decode() throws InvalidInputException{
|
||||
StringBuilder output = new StringBuilder();
|
||||
int inputCnt = 0;
|
||||
String cleanString = getPreparedInputString();
|
||||
@@ -320,14 +321,14 @@ public class Playfair{
|
||||
setDoubled(doubled);
|
||||
}
|
||||
//Sets the keyword and inputString and encodes the message
|
||||
public String encode(String keyword, String input) throws InvalidCharacterException, Exception{
|
||||
public String encode(String keyword, String input) throws InvalidCharacterException, InvalidInputException{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setInputString(input, true);
|
||||
return encode();
|
||||
}
|
||||
//Sets the keyword and inputString and decodes the message
|
||||
public String decode(String keyword, String input) throws InvalidCharacterException, Exception{
|
||||
public String decode(String keyword, String input) throws InvalidCharacterException, InvalidInputException{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setInputString(input, false);
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/PolybiusSquare.java
|
||||
//Mattrixwv
|
||||
// Created: 01-04-22
|
||||
//Modified: 01-04-22
|
||||
//Modified: 01-09-22
|
||||
package com.mattrixwv.CipherStreamJava.polySubstitution;
|
||||
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidCharacterException;
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException;
|
||||
|
||||
|
||||
public class PolybiusSquare{
|
||||
@@ -145,7 +146,7 @@ public class PolybiusSquare{
|
||||
createGrid();
|
||||
}
|
||||
//Returns the location of the given charcter in the grid
|
||||
public CharLocation findChar(char letter) throws Exception{
|
||||
public CharLocation findChar(char letter) throws InvalidInputException{
|
||||
for(int row = 0;row < grid.length;++row){
|
||||
for(int col = 0;col < grid[row].length;++col){
|
||||
if(grid[row][col] == letter){
|
||||
@@ -154,7 +155,7 @@ public class PolybiusSquare{
|
||||
}
|
||||
}
|
||||
//If it was not found something went wrong
|
||||
throw new Exception("That character was not found in the grid. ERROR");
|
||||
throw new InvalidInputException("The character '" + letter + "' was not found in the grid");
|
||||
}
|
||||
//Adds characters that aren't letters to the output
|
||||
public void addCharactersToCleanStringEncode(String cleanString){
|
||||
@@ -190,7 +191,7 @@ public class PolybiusSquare{
|
||||
outputString = fullOutput.toString();
|
||||
}
|
||||
//Encodes inputString using the Playfair cipher and stores the result in outputString
|
||||
private String encode() throws Exception{
|
||||
private String encode() throws InvalidInputException{
|
||||
StringBuilder output = new StringBuilder();
|
||||
String cleanString = getPreparedInputStringEncoding();
|
||||
for(int cnt = 0;cnt < cleanString.length();++cnt){
|
||||
@@ -256,10 +257,10 @@ public class PolybiusSquare{
|
||||
this.leaveSymbols = leaveSymbols;
|
||||
}
|
||||
//Sets the keyword and inputString and encodes the message
|
||||
public String encode(String inputString) throws InvalidCharacterException, Exception{
|
||||
public String encode(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
return encode("", inputString);
|
||||
}
|
||||
public String encode(String keyword, String inputString) throws InvalidCharacterException, Exception{
|
||||
public String encode(String keyword, String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setInputStringEncoding(inputString);
|
||||
|
||||
Reference in New Issue
Block a user