Fixed sonarqube findings
This commit is contained in:
@@ -0,0 +1,191 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Affine.java
|
||||
//Mattrixwv
|
||||
// Created: 01-26-22
|
||||
//Modified: 02-17-22
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
import mattrixwv.NumberAlgorithms;
|
||||
|
||||
|
||||
public class Affine{
|
||||
private boolean preserveCapitals;
|
||||
private boolean preserveSymbols;
|
||||
private boolean preserveWhitespace;
|
||||
private String inputString;
|
||||
private String outputString;
|
||||
private int key1; //Key1 must be relatively prime to 26
|
||||
private int key2;
|
||||
|
||||
private void setKey1(int key1) throws InvalidKeywordException{
|
||||
//If the key is negative change it to possitive
|
||||
if(key1 < 0){
|
||||
key1 %= 26;
|
||||
key1 += 26;
|
||||
}
|
||||
|
||||
//Make sure the key is relatively prime to 26 (The number of letters)
|
||||
if(NumberAlgorithms.gcd(key1, 26) != 1){
|
||||
throw new InvalidKeywordException("Key 1 must be relatively prime to 26");
|
||||
}
|
||||
|
||||
//Save the key
|
||||
this.key1 = key1;
|
||||
}
|
||||
private void setKey2(int key2){
|
||||
//If the key is negative change it to possitive
|
||||
if(key2 < 0){
|
||||
key2 %= 26;
|
||||
key2 += 26;
|
||||
}
|
||||
|
||||
//Save the key
|
||||
this.key2 = key2;
|
||||
}
|
||||
private void setInputString(String inputString) throws InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input must not 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 cannot be blank");
|
||||
}
|
||||
}
|
||||
private String encode(){
|
||||
//Step through every character in the input and encode it if needed
|
||||
StringBuilder output = new StringBuilder();
|
||||
for(char ch : inputString.toCharArray()){
|
||||
//Encode all letters
|
||||
if(Character.isUpperCase(ch)){
|
||||
//Change the character to a number
|
||||
int letter = ch - 65;
|
||||
//Encode the number
|
||||
letter = ((key1 * letter) + key2) % 26;
|
||||
//Change the new number back to a character and append it to the output
|
||||
char newChar = (char)(letter + 65);
|
||||
output.append(newChar);
|
||||
}
|
||||
else if(Character.isLowerCase(ch)){
|
||||
//Change the character to a number
|
||||
int letter = ch - 97;
|
||||
//Encode the number
|
||||
letter = ((key1 * letter) + key2) % 26;
|
||||
//Change the new number back to a character and append it to the output
|
||||
char newChar = (char)(letter + 97);
|
||||
output.append(newChar);
|
||||
}
|
||||
//Pass all other characters through
|
||||
else{
|
||||
output.append(ch);
|
||||
}
|
||||
}
|
||||
|
||||
//Save and return the output
|
||||
outputString = output.toString();
|
||||
return outputString;
|
||||
}
|
||||
private String decode(){
|
||||
//Find the multiplicative inverse of key1
|
||||
int key1I = 1;
|
||||
while(((key1 * key1I) % 26) != 1){
|
||||
++key1I;
|
||||
}
|
||||
|
||||
//Step through every character in the input and decode it if needed
|
||||
StringBuilder output = new StringBuilder();
|
||||
for(char ch : inputString.toCharArray()){
|
||||
//Encode all letters
|
||||
if(Character.isUpperCase(ch)){
|
||||
//Change the letter to a number
|
||||
int letter = ch - 65;
|
||||
//Encode the number
|
||||
letter = (key1I * (letter - key2)) % 26;
|
||||
if(letter < 0){
|
||||
letter += 26;
|
||||
}
|
||||
//Change the new number back to a character and append it to the output
|
||||
char newChar = (char)(letter + 65);
|
||||
output.append(newChar);
|
||||
}
|
||||
else if(Character.isLowerCase(ch)){
|
||||
//Change the letter to a number
|
||||
int letter = ch - 97;
|
||||
//Encode the number
|
||||
letter = (key1I * (letter - key2)) % 26;
|
||||
if(letter < 0){
|
||||
letter += 26;
|
||||
}
|
||||
//Change the new number back to a character and append it to the output
|
||||
char newChar = (char)(letter + 97);
|
||||
output.append(newChar);
|
||||
}
|
||||
//Pass all other characters through
|
||||
else{
|
||||
output.append(ch);
|
||||
}
|
||||
}
|
||||
|
||||
//Save and return the output
|
||||
outputString = output.toString();
|
||||
return outputString;
|
||||
}
|
||||
|
||||
public Affine(){
|
||||
preserveCapitals = false;
|
||||
preserveSymbols = false;
|
||||
preserveWhitespace = false;
|
||||
reset();
|
||||
}
|
||||
public Affine(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
reset();
|
||||
}
|
||||
public String encode(int key1, int key2, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
setKey1(key1);
|
||||
setKey2(key2);
|
||||
setInputString(inputString);
|
||||
return encode();
|
||||
}
|
||||
public String decode(int key1, int key2, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
setKey1(key1);
|
||||
setKey2(key2);
|
||||
setInputString(inputString);
|
||||
return decode();
|
||||
}
|
||||
|
||||
public void reset(){
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
key1 = 0;
|
||||
key2 = 0;
|
||||
}
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
public int getKey1(){
|
||||
return key1;
|
||||
}
|
||||
public int getKey2(){
|
||||
return key2;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Atbash.java
|
||||
//Mattrixwv
|
||||
// Created: 07-25-21
|
||||
//Modified: 02-22-22
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
|
||||
|
||||
public class Atbash{
|
||||
private String inputString; //Holds the string that needs encoded or decoded
|
||||
private String outputString; //The encoded/decoded string
|
||||
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
|
||||
//Encodes inputString and stores in outputString
|
||||
private String encode(){
|
||||
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);
|
||||
//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)){
|
||||
output.append((char)(155 - currentChar));
|
||||
}
|
||||
else{
|
||||
output.append((char)(219 - currentChar));
|
||||
}
|
||||
}
|
||||
//Keep any punctuatio/whitespace the way it is
|
||||
else{
|
||||
output.append(currentChar);
|
||||
}
|
||||
}
|
||||
|
||||
outputString = output.toString();
|
||||
return outputString;
|
||||
}
|
||||
//Removes all invalid characters and sets inputString
|
||||
private void setInputString(String inputString) throws InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new NullPointerException("Input cannot be null");
|
||||
}
|
||||
|
||||
if(!preserveCapitals){
|
||||
//Convert all letters to lowercase
|
||||
inputString = inputString.toUpperCase();
|
||||
}
|
||||
if(!preserveWhitespace){
|
||||
//Remove all characters except capital letters
|
||||
inputString = inputString.replaceAll("\\s", "");
|
||||
}
|
||||
if(!preserveSymbols){
|
||||
//Remove all non-alpha numeric and whitespace symbols
|
||||
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
|
||||
}
|
||||
|
||||
//Save the string
|
||||
this.inputString = inputString;
|
||||
|
||||
if(this.inputString.isBlank()){
|
||||
throw new InvalidInputException("Input must contain at least 1 character");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
public String encode(String inputString) throws InvalidInputException{
|
||||
//Make sure everything is empty before you begin
|
||||
reset();
|
||||
setInputString(inputString);
|
||||
return encode();
|
||||
}
|
||||
public String decode(String inputString) throws InvalidInputException{
|
||||
return encode(inputString);
|
||||
}
|
||||
public void reset(){
|
||||
inputString = outputString = "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Autokey.java
|
||||
//Mattrixwv
|
||||
// Created: 07-25-21
|
||||
//Modified: 07-03-22
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
public class Autokey extends Vigenere{
|
||||
//Special rules for setting the strings for encoding
|
||||
private void encodeSet(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
//Set the input
|
||||
setInputString(inputString);
|
||||
|
||||
StringBuilder newKey = new StringBuilder();
|
||||
//Remove all unneccessary elements from the key
|
||||
setKeyword(keyword);
|
||||
newKey.append(keyword);
|
||||
//Remove all unneccessary elements from the input
|
||||
setKeyword(inputString);
|
||||
newKey.append(getKeyword());
|
||||
|
||||
//Make sure the key is not any longer than the input
|
||||
keyword = newKey.substring(0, getKeyword().length());
|
||||
|
||||
//Set the new keyword
|
||||
setKeyword(keyword);
|
||||
//Make sure to update the offset
|
||||
offset.clear();
|
||||
setOffset();
|
||||
}
|
||||
//Setting the strings for decoding
|
||||
private void decodeSet(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
//Remove all unneccessary elements from the key
|
||||
setKeyword(keyword);
|
||||
//Remove all unneccessary elements from the input
|
||||
setInputString(inputString);
|
||||
}
|
||||
//Decodes the inputString
|
||||
@Override
|
||||
protected String decode(){
|
||||
//Decode what the key will allow, add that to the key and continue
|
||||
StringBuilder currentOutput = new StringBuilder();
|
||||
StringBuilder fullOutput = 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){
|
||||
//If we have reached the end of the keyword add what we have to it and continue
|
||||
if(offsetCnt == keyword.length()){
|
||||
setKeyword(keyword + currentOutput.toString());
|
||||
fullOutput.append(currentOutput);
|
||||
currentOutput = new StringBuilder();
|
||||
}
|
||||
|
||||
char letter = inputString.charAt(letterCnt);
|
||||
if(Character.isUpperCase(letter)){
|
||||
letter -= offset.get((offsetCnt++) % offset.size());
|
||||
if(letter < 'A'){
|
||||
letter += 26;
|
||||
}
|
||||
else if(letter > 'Z'){
|
||||
letter -= 26;
|
||||
}
|
||||
}
|
||||
else if(Character.isLowerCase(letter)){
|
||||
letter -= offset.get((offsetCnt++) % offset.size());
|
||||
if(letter < 'a'){
|
||||
letter += 26;
|
||||
}
|
||||
else if(letter > 'z'){
|
||||
letter -= 26;
|
||||
}
|
||||
}
|
||||
currentOutput.append(letter);
|
||||
}
|
||||
//Empty the last character that were decoded
|
||||
fullOutput.append(currentOutput);
|
||||
|
||||
//Save and return the results
|
||||
outputString = fullOutput.toString();
|
||||
return outputString;
|
||||
}
|
||||
|
||||
|
||||
//Constructor
|
||||
public Autokey(){
|
||||
super();
|
||||
}
|
||||
public Autokey(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
||||
super(preserveCapitals, preserveWhitespace, preserveSymbols);
|
||||
}
|
||||
//Encodes inputString using the Autokey cipher
|
||||
@Override
|
||||
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
reset();
|
||||
encodeSet(keyword, inputString);
|
||||
return encode();
|
||||
}
|
||||
//Decodes inputString using the Autokey cipher
|
||||
@Override
|
||||
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
reset();
|
||||
decodeSet(keyword, inputString);
|
||||
return decode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/Baconian.java
|
||||
//Mattrixwv
|
||||
// Created: 01-12-22
|
||||
//Modified: 01-16-22
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
|
||||
public class Baconian{
|
||||
private static final ArrayList<String> code = new ArrayList<>(Arrays.asList(
|
||||
"aaaaa", "aaaab", "aaaba", "aaabb", "aabaa", "aabab", "aabba","aabbb", "abaaa", "abaaa", "abaab", "ababa", "ababb", //A-M
|
||||
"abbaa", "abbab", "abbba", "abbbb", "baaaa", "baaab", "baaba", "baabb", "baabb", "babaa", "babab", "babba", "babbb" //N-Z
|
||||
));
|
||||
private String inputString; //The string that needs encoded/decoded
|
||||
private String outputString; //The encoded/decoded string
|
||||
private boolean preserveCapitals; //Whether to respect capitals in the output string
|
||||
|
||||
//Sets the input string
|
||||
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){
|
||||
inputString = inputString.toLowerCase();
|
||||
}
|
||||
|
||||
this.inputString = inputString;
|
||||
|
||||
if(this.inputString.isBlank()){
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
private void setInputStringDecode(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new NullPointerException("Input cannot be null");
|
||||
}
|
||||
|
||||
if(!preserveCapitals){
|
||||
inputString = inputString.toLowerCase();
|
||||
}
|
||||
|
||||
//Check each Baconian Cipher letter for validity
|
||||
for(String str : inputString.split(" ")){
|
||||
//Make sure each letter contains 5 characters
|
||||
if(str.length() != 5){
|
||||
throw new InvalidCharacterException("All Baconian letters contain exactly 5 characters: " + str);
|
||||
}
|
||||
//Make sure the letter contains only a's and b's
|
||||
String temp = str.replaceAll("[^abAB]", "");
|
||||
if(!temp.equals(str)){
|
||||
throw new InvalidCharacterException("Baconian letters contain only a's and b's: " + str);
|
||||
}
|
||||
}
|
||||
|
||||
this.inputString = inputString;
|
||||
|
||||
if(this.inputString.isBlank()){
|
||||
throw new InvalidInputException("Input cannot be empty");
|
||||
}
|
||||
}
|
||||
//Encodes the inputString and stores the result in outputString
|
||||
private String encode(){
|
||||
StringJoiner output = new StringJoiner(" ");
|
||||
//Go through every character in the inputString and encode it
|
||||
for(char ch : inputString.toCharArray()){
|
||||
//Convert the character to a binary string with A = 0
|
||||
String binary = null;
|
||||
if(Character.isUpperCase(ch)){
|
||||
binary = code.get(ch - 'A').toUpperCase();
|
||||
}
|
||||
else{
|
||||
binary = code.get(ch - 'a').toLowerCase();
|
||||
}
|
||||
|
||||
//Add the encoded character to the output
|
||||
output.add(binary);
|
||||
}
|
||||
|
||||
//Save and return the output
|
||||
outputString = output.toString();
|
||||
return outputString;
|
||||
}
|
||||
//Decodes the inputString and stores the result in outputString
|
||||
private String decode(){
|
||||
StringBuilder output = new StringBuilder();
|
||||
//Go through every Baconian Cipher character in the inputString and decode it
|
||||
for(String baconianCharacter : inputString.split(" ")){
|
||||
//Get the location of the Baconian character in the array
|
||||
int location = code.indexOf(baconianCharacter.toLowerCase());
|
||||
|
||||
//Convert the Baconian character to an ASCII character
|
||||
char ch;
|
||||
if(Character.isUpperCase(baconianCharacter.charAt(0))){
|
||||
ch = (char)(location + 'A');
|
||||
}
|
||||
else{
|
||||
ch = (char)(location + 'a');
|
||||
}
|
||||
|
||||
//Add the decoded character to the output
|
||||
output.append(ch);
|
||||
}
|
||||
|
||||
//Save and return the output
|
||||
outputString = output.toString();
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Constructor
|
||||
public Baconian(){
|
||||
reset();
|
||||
preserveCapitals = false;
|
||||
}
|
||||
public Baconian(boolean preserveCapitals){
|
||||
reset();
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
}
|
||||
//Returns the outputString
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
//Returns the inputString
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
//Sets the inputString and encodes the message
|
||||
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, InvalidInputException{
|
||||
reset();
|
||||
setInputStringDecode(inputString);
|
||||
return decode();
|
||||
}
|
||||
//Makes sure all of the variables are empty
|
||||
public void reset(){
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/BaseX.java
|
||||
//Mattrixwv
|
||||
// Created: 01-08-22
|
||||
//Modified: 01-09-22
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidBaseException;
|
||||
|
||||
|
||||
public class BaseX{
|
||||
private String inputString; //The string that needs encoded/decoded
|
||||
private String outputString; //The encoded/decoded string
|
||||
private int base; //The base that the number will be encoded at
|
||||
|
||||
//Sets the input string
|
||||
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, 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());
|
||||
}
|
||||
this.inputString = inputString.replaceAll("[^" + validNumbers.toString() + "\\s]", "");
|
||||
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{
|
||||
if(base <= 0){
|
||||
throw new InvalidBaseException("Base cannot be a negative number");
|
||||
}
|
||||
|
||||
this.base = base;
|
||||
}
|
||||
//Encode inputString, store it in outputString, and return it
|
||||
private String encode(){
|
||||
//Encode every character in inputString
|
||||
StringJoiner output = new StringJoiner(" ");
|
||||
for(int cnt = 0;cnt < inputString.length();++cnt){
|
||||
//Get the next character
|
||||
char ch = inputString.charAt(cnt);
|
||||
//Encode the character to binary and add it to the output
|
||||
output.add(Integer.toString(ch, base));
|
||||
}
|
||||
|
||||
//Save the output
|
||||
outputString = output.toString().toUpperCase();
|
||||
|
||||
//Return the output
|
||||
return outputString;
|
||||
}
|
||||
//Decode inputString, store it in outputString, and return it
|
||||
private String decode() throws InvalidCharacterException{
|
||||
//Decode every binary number in the string
|
||||
StringBuilder output = new StringBuilder();
|
||||
for(String baseXString : inputString.split(" ")){
|
||||
//Decode the current binary number
|
||||
int num = Integer.valueOf(baseXString, base);
|
||||
//Make sure it is in a valid range
|
||||
if((num < 0) && (num > 255)){
|
||||
throw new InvalidCharacterException("The base" + base + " string '" + baseXString + "' is not a valid character");
|
||||
}
|
||||
|
||||
//Convert the int to a char and save it
|
||||
output.append((char)num);
|
||||
}
|
||||
|
||||
//Save the output
|
||||
outputString = output.toString();
|
||||
|
||||
//Return the output
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Constructor
|
||||
public BaseX() throws InvalidBaseException{
|
||||
reset();
|
||||
setBase(2);
|
||||
}
|
||||
public BaseX(int base) throws InvalidBaseException{
|
||||
reset();
|
||||
setBase(base);
|
||||
}
|
||||
//Returns the inputString
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
//Returns the outputString
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
//Returns the base
|
||||
public int getBase(){
|
||||
return base;
|
||||
}
|
||||
//Sets the inputString and encodes the message
|
||||
public String encode(String inputString) throws InvalidInputException{
|
||||
reset();
|
||||
setInputStringEncode(inputString);
|
||||
return encode();
|
||||
}
|
||||
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, InvalidInputException{
|
||||
reset();
|
||||
setInputStringDecode(inputString);
|
||||
return decode();
|
||||
}
|
||||
public String decode(int base, String inputString) throws InvalidBaseException, InvalidCharacterException, InvalidInputException{
|
||||
reset();
|
||||
setBase(base);
|
||||
setInputStringDecode(inputString);
|
||||
return decode();
|
||||
}
|
||||
//Makes sure all of the variables are empty
|
||||
public void reset(){
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Beaufort.java
|
||||
//Mattrixwv
|
||||
// Created: 02-23-22
|
||||
//Modified: 02-23-22
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
public class Beaufort{
|
||||
private String inputString; //This is the string that needs encoded/decoded
|
||||
private String outputString; //This is the string that is output after encoding/decoding
|
||||
private String keyword; //This is the keyword that is responsible for determining the offsets that you change each character by
|
||||
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
|
||||
private Atbash atbash; //The first step in encoding/decoding the cipher
|
||||
private Caesar caesar; //The second step in encoding/decoding the cipher
|
||||
private Vigenere vigenere; //The third step in encoding/decoding the cipher
|
||||
|
||||
//Ensures inputString constraints
|
||||
public void setInputString(String inputString) throws InvalidInputException{
|
||||
//Make sure the input isn't null
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
}
|
||||
|
||||
//Apply removal options
|
||||
if(!preserveCapitals){
|
||||
inputString = inputString.toUpperCase();
|
||||
}
|
||||
if(!preserveWhitespace){
|
||||
inputString = inputString.replaceAll("\\s", "");
|
||||
}
|
||||
if(!preserveSymbols){
|
||||
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
|
||||
}
|
||||
|
||||
//Save the string
|
||||
this.inputString = inputString;
|
||||
|
||||
//Make sure the string isn't blank
|
||||
if(this.inputString.isBlank()){
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
//Ensures keyword constraints
|
||||
public void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
//Make sure the keyword isn't null
|
||||
if(keyword == null){
|
||||
throw new InvalidKeywordException("Keyword cannot be null");
|
||||
}
|
||||
|
||||
//Convert all letters to uppercase
|
||||
keyword = keyword.toUpperCase();
|
||||
//Remove all characters except capital letters
|
||||
keyword = keyword.replaceAll("[^A-Z]", "");
|
||||
//Save the string
|
||||
this.keyword = keyword;
|
||||
|
||||
//If after all the elimination of unusable characters the keyword is empty throw an exception
|
||||
if(this.keyword.isBlank() || (this.keyword.length() < 2)){
|
||||
throw new InvalidKeywordException("Keyword must contain at least 2 letters");
|
||||
}
|
||||
}
|
||||
//Encodes the inputString and stores the result in outputString
|
||||
public void encode() throws InvalidKeywordException, InvalidInputException{
|
||||
//Reverse the string
|
||||
String atbashString = atbash.encode(inputString);
|
||||
//Shift the reversal by 1
|
||||
//?Not quite sure why this is needed. Need to look into this cipher a bit more closely
|
||||
String caesarString = caesar.encode(1, atbashString);
|
||||
//Shift each letter according to the key
|
||||
String vigenereString = vigenere.encode(keyword, caesarString);
|
||||
|
||||
//Save the output
|
||||
this.outputString = vigenereString;
|
||||
}
|
||||
//Decodes the inputString and stores the result in outputString
|
||||
public void decode() throws InvalidKeywordException, InvalidInputException{
|
||||
//Decoding is just encoding again
|
||||
encode();
|
||||
}
|
||||
|
||||
|
||||
//Constructor
|
||||
public Beaufort(){
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
atbash = new Atbash(false, false, false);
|
||||
caesar = new Caesar(false, false, false);
|
||||
vigenere = new Vigenere(false, false, false);
|
||||
reset();
|
||||
}
|
||||
public Beaufort(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
atbash = new Atbash(preserveCapitals, preserveWhitespace, preserveSymbols);
|
||||
caesar = new Caesar(preserveCapitals, preserveWhitespace, preserveSymbols);
|
||||
vigenere = new Vigenere(preserveCapitals, preserveWhitespace, preserveSymbols);
|
||||
reset();
|
||||
}
|
||||
//Returns the current inputString
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
//Returns the current outputString
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
//Returns the current keyword
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
//Encodes inputString using keyword and returns the result
|
||||
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
//Set the parameters
|
||||
setKeyword(keyword);
|
||||
setInputString(inputString);
|
||||
|
||||
//Encode and return the message
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString using keyword and returns the result
|
||||
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
//Set the parameters
|
||||
setKeyword(keyword);
|
||||
setInputString(inputString);
|
||||
|
||||
//Decode and return the message
|
||||
decode();
|
||||
return outputString;
|
||||
}
|
||||
//Makes sure all of the variables are empty
|
||||
public void reset(){
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
keyword = "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
//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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/OneTimePad.java
|
||||
//Mattrixwv
|
||||
// Created: 02-23-22
|
||||
//Modified: 02-23-22
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
public class OneTimePad extends Vigenere{
|
||||
//?Add some kind of entropy calculator?
|
||||
//?Add some kind of "book passage includer"?
|
||||
|
||||
|
||||
//Constructor
|
||||
public OneTimePad(){
|
||||
super();
|
||||
}
|
||||
public OneTimePad(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
||||
super(preserveCapitals, preserveWhitespace, preserveSymbols);
|
||||
}
|
||||
|
||||
//Encodes input using key and returns the result
|
||||
@Override
|
||||
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
if(keyword.length() < inputString.length()){
|
||||
throw new InvalidKeywordException("Key must be at least as long as the input");
|
||||
}
|
||||
return super.encode(keyword, inputString);
|
||||
}
|
||||
//Decodes input using key and returns the result
|
||||
@Override
|
||||
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
if(keyword.length() < inputString.length()){
|
||||
throw new InvalidKeywordException("Key must be at least as long as the input");
|
||||
}
|
||||
return super.decode(keyword, inputString);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Porta.java
|
||||
//Mattrixwv
|
||||
// Created: 02-28-22
|
||||
//Modified: 02-28-22
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
public class Porta{
|
||||
private static final String[] tableau = {
|
||||
"NOPQRSTUVWXYZABCDEFGHIJKLM", //A-B
|
||||
"OPQRSTUVWXYZNMABCDEFGHIJKL", //C-D
|
||||
"PQRSTUVWXYZNOLMABCDEFGHIJK", //E-F
|
||||
"QRSTUVWXYZNOPKLMABCDEFGHIJ", //G-H
|
||||
"RSTUVWXYZNOPQJKLMABCDEFGHI", //I-J
|
||||
"STUVWXYZNOPQRIJKLMABCDEFGH", //K-L
|
||||
"TUVWXYZNOPQRSHIJKLMABCDEFG", //M-N
|
||||
"UVWXYZNOPQRSTGHIJKLMABCDEF", //O-P
|
||||
"VWXYZNOPQRSTUFGHIJKLMABCDE", //Q-R
|
||||
"WXYZNOPQRSTUVEFGHIJKLMABCD", //S-T
|
||||
"XYZNOPQRSTUVWDEFGHIJKLMABC", //U-V
|
||||
"YZNOPQRSTUVWXCDEFGHIJKLMAB", //W-X
|
||||
"ZNOPQRSTUVWXYBCDEFGHIJKLMA" //Y-Z
|
||||
};
|
||||
|
||||
private String inputString;
|
||||
private String outputString;
|
||||
private String keyword;
|
||||
private boolean preserveCapitals;
|
||||
private boolean preserveWhitespace;
|
||||
private boolean preserveSymbols;
|
||||
|
||||
private void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
//Make sure the keyword isn't null
|
||||
if(keyword == null){
|
||||
throw new InvalidKeywordException("Keyword cannot be null");
|
||||
}
|
||||
|
||||
//Convert all letters to uppercase
|
||||
keyword = keyword.toUpperCase();
|
||||
//Remove all characters except capital letters and save the string
|
||||
keyword = keyword.replaceAll("[^A-Z]", "");
|
||||
this.keyword = keyword;
|
||||
|
||||
//If after eliminating all ususable characters the keyword is empty throw an exception
|
||||
if(this.keyword.isBlank() || (this.keyword.length() < 2)){
|
||||
throw new InvalidKeywordException("Keyword must contain at least 2 letters");
|
||||
}
|
||||
}
|
||||
private void setInputString(String inputString) throws InvalidInputException{
|
||||
//Ensure the input isn't null
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
}
|
||||
|
||||
//Apply removal options
|
||||
if(!preserveCapitals){
|
||||
inputString = inputString.toUpperCase();
|
||||
}
|
||||
if(!preserveWhitespace){
|
||||
inputString = inputString.replaceAll("\\s", "");
|
||||
}
|
||||
if(!preserveSymbols){
|
||||
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
|
||||
}
|
||||
|
||||
//Save the string
|
||||
this.inputString = inputString;
|
||||
|
||||
//Ensure the string isn't blank
|
||||
if(this.inputString.isBlank()){
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
private char getReplacer(int keywordCnt, char letter){
|
||||
char keyLetter = keyword.charAt(keywordCnt % keyword.length());
|
||||
int tableauColumn = (Character.toUpperCase(letter) - 'A');
|
||||
char replacer;
|
||||
|
||||
switch(keyLetter){
|
||||
case 'A':
|
||||
case 'B':
|
||||
replacer = tableau[0].charAt(tableauColumn); break;
|
||||
case 'C':
|
||||
case 'D':
|
||||
replacer = tableau[1].charAt(tableauColumn); break;
|
||||
case 'E':
|
||||
case 'F':
|
||||
replacer = tableau[2].charAt(tableauColumn); break;
|
||||
case 'G':
|
||||
case 'H':
|
||||
replacer = tableau[3].charAt(tableauColumn); break;
|
||||
case 'I':
|
||||
case 'J':
|
||||
replacer = tableau[4].charAt(tableauColumn); break;
|
||||
case 'K':
|
||||
case 'L':
|
||||
replacer = tableau[5].charAt(tableauColumn); break;
|
||||
case 'M':
|
||||
case 'N':
|
||||
replacer = tableau[6].charAt(tableauColumn); break;
|
||||
case 'O':
|
||||
case 'P':
|
||||
replacer = tableau[7].charAt(tableauColumn); break;
|
||||
case 'Q':
|
||||
case 'R':
|
||||
replacer = tableau[8].charAt(tableauColumn); break;
|
||||
case 'S':
|
||||
case 'T':
|
||||
replacer = tableau[9].charAt(tableauColumn); break;
|
||||
case 'U':
|
||||
case 'V':
|
||||
replacer = tableau[10].charAt(tableauColumn); break;
|
||||
case 'W':
|
||||
case 'X':
|
||||
replacer = tableau[11].charAt(tableauColumn); break;
|
||||
case 'Y':
|
||||
case 'Z':
|
||||
replacer = tableau[12].charAt(tableauColumn); break;
|
||||
default:
|
||||
replacer = letter;
|
||||
}
|
||||
|
||||
return replacer;
|
||||
}
|
||||
private void encode(){
|
||||
StringBuilder output = new StringBuilder();
|
||||
|
||||
//Step through every character in the inputString and advance it the correct amount according to the keyword and tableau
|
||||
int keywordCnt = 0;
|
||||
for(char letter : inputString.toCharArray()){
|
||||
//If the character is a letter replace with the corresponding character from the tableau
|
||||
if(Character.isUpperCase(letter)){
|
||||
letter = Character.toUpperCase(getReplacer(keywordCnt, letter));
|
||||
++keywordCnt;
|
||||
}
|
||||
else if(Character.isLowerCase(letter)){
|
||||
letter = Character.toLowerCase(getReplacer(keywordCnt, letter));
|
||||
++keywordCnt;
|
||||
}
|
||||
|
||||
//Add the current character to the output
|
||||
output.append(letter);
|
||||
}
|
||||
|
||||
//Save the output
|
||||
outputString = output.toString();
|
||||
}
|
||||
private void decode(){
|
||||
//Decoding is the same as encoding
|
||||
encode();
|
||||
}
|
||||
|
||||
|
||||
//Constructor
|
||||
public Porta(){
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
reset();
|
||||
}
|
||||
public Porta(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
reset();
|
||||
}
|
||||
|
||||
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
//Set the parameters
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setInputString(inputString);
|
||||
|
||||
//Encode and return the message
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
//Set the parameters
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setInputString(inputString);
|
||||
|
||||
//Decode and return the message
|
||||
decode();
|
||||
return outputString;
|
||||
}
|
||||
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
public void reset(){
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
keyword = "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Substitution.java
|
||||
//Mattrixwv
|
||||
// Created: 02-22-22
|
||||
//Modified: 02-22-22
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
public class Substitution{
|
||||
private String inputString;
|
||||
private String outputString;
|
||||
private String key;
|
||||
private boolean preserveCapitals;
|
||||
private boolean preserveWhitespace;
|
||||
private boolean preserveSymbols;
|
||||
|
||||
private void setKey(String key) throws InvalidKeywordException{
|
||||
if(key == null){
|
||||
throw new InvalidKeywordException("Key cannot be null");
|
||||
}
|
||||
|
||||
//Transform all letters to uppercase
|
||||
key = key.toUpperCase();
|
||||
|
||||
//Make sure the key contains no duplicate mappings
|
||||
String tempKey = key.replaceAll("(.)\\1{2}", "");
|
||||
if(!tempKey.equals(key)){
|
||||
throw new InvalidKeywordException("The key cannot contain duplicate mappings");
|
||||
}
|
||||
|
||||
//Make sure the key is a valid length
|
||||
if(key.length() == 26){
|
||||
//Make sure the key contains all valid characters
|
||||
tempKey = key.replaceAll("[^A-Z]", "");
|
||||
if(!tempKey.equals(key)){
|
||||
throw new InvalidKeywordException("The key must contain all letters");
|
||||
}
|
||||
}
|
||||
else if(key.length() == 36){
|
||||
//Make sure the key contains all valid characters
|
||||
tempKey = key.replaceAll("[^A-Z0-9]", "");
|
||||
if(!tempKey.equals(key)){
|
||||
throw new InvalidKeywordException("The key must contain all letters and can contain all numbers");
|
||||
}
|
||||
}
|
||||
else{
|
||||
throw new InvalidKeywordException("The key must contain all letters and can contain all numbers");
|
||||
}
|
||||
|
||||
//Save the key
|
||||
this.key = key;
|
||||
}
|
||||
private void setInputString(String inputString) throws InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
}
|
||||
|
||||
//Remove any data that should not be preserved
|
||||
if(!preserveCapitals){
|
||||
inputString = inputString.toUpperCase();
|
||||
}
|
||||
if(!preserveWhitespace){
|
||||
inputString = inputString.replaceAll("\\s", "");
|
||||
}
|
||||
if(!preserveSymbols){
|
||||
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
|
||||
}
|
||||
|
||||
//Save the inputString
|
||||
this.inputString = inputString;
|
||||
|
||||
//Make sure there is still input
|
||||
if(this.inputString.isBlank()){
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
private void encode(){
|
||||
StringBuilder output = new StringBuilder();
|
||||
|
||||
//Step through every character in the inputString and convert it
|
||||
for(char ch : inputString.toCharArray()){
|
||||
if(Character.isUpperCase(ch)){
|
||||
output.append(Character.toUpperCase(key.charAt(ch - 'A')));
|
||||
}
|
||||
else if(Character.isLowerCase(ch)){
|
||||
output.append(Character.toLowerCase(key.charAt(ch - 'a')));
|
||||
}
|
||||
else if(Character.isDigit(ch) && (key.length() == 36)){
|
||||
output.append(key.charAt('Z' - 'A' + Integer.valueOf(Character.toString(ch)) + 1));
|
||||
}
|
||||
else{
|
||||
output.append(ch);
|
||||
}
|
||||
}
|
||||
|
||||
this.outputString = output.toString();
|
||||
}
|
||||
private void decode(){
|
||||
StringBuilder output = new StringBuilder();
|
||||
|
||||
//Step through every character in the inputString and convert it
|
||||
for(char ch : inputString.toCharArray()){
|
||||
if(Character.isUpperCase(ch)){
|
||||
output.append((char)('A' + key.indexOf(Character.toUpperCase(ch))));
|
||||
}
|
||||
else if(Character.isLowerCase(ch)){
|
||||
output.append((char)('a' + key.indexOf(Character.toUpperCase(ch))));
|
||||
}
|
||||
else if(Character.isDigit(ch) && (key.length() == 36)){
|
||||
output.append((char)('0' + (key.indexOf(Character.toUpperCase(ch)) - 26)));
|
||||
}
|
||||
else{
|
||||
output.append(ch);
|
||||
}
|
||||
}
|
||||
|
||||
this.outputString = output.toString();
|
||||
}
|
||||
|
||||
public Substitution(){
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
reset();
|
||||
}
|
||||
public Substitution(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
reset();
|
||||
}
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
public String getKeyword(){
|
||||
return key;
|
||||
}
|
||||
public String encode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
setKey(key);
|
||||
setInputString(inputString);
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
public String decode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
setKey(key);
|
||||
setInputString(inputString);
|
||||
decode();
|
||||
return outputString;
|
||||
}
|
||||
public void reset(){
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
key = "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Vigenere.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-25-21
|
||||
//Modified: 02-22-22
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
public class Vigenere{
|
||||
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
|
||||
protected boolean preserveCapitals; //Whether to respect capitals in the output string
|
||||
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
|
||||
protected boolean preserveSymbols; //Whether to respect symbols in the output string
|
||||
|
||||
//Uses keyword to calculate the offset for the Caesar cipher for each character
|
||||
protected void setOffset(){
|
||||
//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);
|
||||
}
|
||||
}
|
||||
//Sets inputString
|
||||
protected void setInputString(String inputString) throws InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new NullPointerException("Input cannot be null");
|
||||
}
|
||||
|
||||
if(!preserveCapitals){
|
||||
inputString = inputString.toUpperCase();
|
||||
}
|
||||
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");
|
||||
}
|
||||
}
|
||||
//Sets keyword
|
||||
protected void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
if(keyword == null){
|
||||
throw new NullPointerException("Keyword cannot be null");
|
||||
}
|
||||
|
||||
//Convert all letters to uppercase
|
||||
keyword = keyword.toUpperCase();
|
||||
//Remove all characters except capital letters
|
||||
keyword = keyword.replaceAll("[^A-Z]", "");
|
||||
//Save the string
|
||||
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.isBlank()){
|
||||
throw new InvalidKeywordException("Keyword must contain at least 2 letters");
|
||||
}
|
||||
}
|
||||
//Encodes inputString and stores the result in outputString
|
||||
protected String encode(){
|
||||
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);
|
||||
if(Character.isUpperCase(letter)){
|
||||
letter += offset.get((offsetCnt++) % offset.size());
|
||||
//Make sure the character is still a letter, if not, wrap around
|
||||
if(letter < 'A'){
|
||||
letter += 26;
|
||||
}
|
||||
else if(letter > 'Z'){
|
||||
letter -= 26;
|
||||
}
|
||||
}
|
||||
else if(Character.isLowerCase(letter)){
|
||||
letter += offset.get((offsetCnt++) % offset.size());
|
||||
//Make sure the character is still a letter, if not, wrap around
|
||||
if(letter < 'a'){
|
||||
letter += 26;
|
||||
}
|
||||
else if(letter > 'z'){
|
||||
letter -= 26;
|
||||
}
|
||||
}
|
||||
output.append(letter);
|
||||
}
|
||||
|
||||
outputString = output.toString();
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString and stores the result in outputString
|
||||
protected String decode(){
|
||||
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);
|
||||
if(Character.isUpperCase(letter)){
|
||||
letter -= offset.get((offsetCnt++) % offset.size());
|
||||
if(letter < 'A'){
|
||||
letter += 26;
|
||||
}
|
||||
else if(letter > 'Z'){
|
||||
letter -= 26;
|
||||
}
|
||||
}
|
||||
else if(Character.isLowerCase(letter)){
|
||||
letter -= offset.get((offsetCnt++) % offset.size());
|
||||
if(letter < 'a'){
|
||||
letter += 26;
|
||||
}
|
||||
else if(letter > 'z'){
|
||||
letter -= 26;
|
||||
}
|
||||
}
|
||||
output.append(letter);
|
||||
}
|
||||
|
||||
outputString = output.toString();
|
||||
return 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;
|
||||
}
|
||||
//Returns the current inputString
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
//Returns the current outputString
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
//Returns the current keyword
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
//Returns the current offsets (Used mostly in bug fixing)
|
||||
public List<Integer> getOffsets(){
|
||||
return offset;
|
||||
}
|
||||
//Encodes input using key and returns the result
|
||||
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setInputString(inputString);
|
||||
return encode();
|
||||
}
|
||||
//Decodes input using key and returns the result
|
||||
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setInputString(inputString);
|
||||
return decode();
|
||||
}
|
||||
//Makes sure all of the variables are empty
|
||||
public void reset(){
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
keyword = "";
|
||||
offset.clear();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user