Moved classes to website appropriate directory
This commit is contained in:
115
src/main/java/com/mattrixwv/CipherStreamJava/Atbash.java
Normal file
115
src/main/java/com/mattrixwv/CipherStreamJava/Atbash.java
Normal file
@@ -0,0 +1,115 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Atbash.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-25-21
|
||||
//Modified: 01-04-22
|
||||
package com.mattrixwv.CipherStreamJava;
|
||||
|
||||
|
||||
public class Atbash{
|
||||
private String inputString; //Holds the string that needs encoded or decoded
|
||||
private String outputString; //The encoded/decoded string
|
||||
private boolean leaveCapitals; //Whether to respect capitals in the output string
|
||||
private boolean leaveWhitespace; //Whether to respect whitespace in the output string
|
||||
private boolean leaveSymbols; //Whether to respect symbols in the output string
|
||||
//Decodes inputString and stores in outputString
|
||||
private String decode(){
|
||||
StringBuilder output = new StringBuilder();
|
||||
//Stop 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
|
||||
char letterBase = 'a';
|
||||
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 punctuation/whitespace the way it is
|
||||
else{
|
||||
output.append(currentChar);
|
||||
}
|
||||
}
|
||||
|
||||
outputString = output.toString();
|
||||
return outputString;
|
||||
}
|
||||
//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
|
||||
char letterBase = 'a';
|
||||
if(Character.isUpperCase(currentChar)){
|
||||
letterBase = 'A';
|
||||
}
|
||||
output.append((char)(currentChar + 25 - (2 * (currentChar - letterBase))));
|
||||
}
|
||||
//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 input){
|
||||
if(!leaveCapitals){
|
||||
//Convert all letters to lowercase
|
||||
input = input.toLowerCase();
|
||||
}
|
||||
if(!leaveWhitespace){
|
||||
//Remove all characters except capital letters
|
||||
input = input.replaceAll("\\s+", "");
|
||||
}
|
||||
if(!leaveSymbols){
|
||||
//Remove all non-alpha numeric and whitespace symbols
|
||||
input = input.replaceAll("[^a-zA-Z0-9\\s]", "");
|
||||
}
|
||||
//Save the string
|
||||
inputString = input;
|
||||
}
|
||||
|
||||
|
||||
public Atbash(){
|
||||
reset();
|
||||
leaveCapitals = false;
|
||||
leaveWhitespace = false;
|
||||
leaveSymbols = false;
|
||||
}
|
||||
public Atbash(boolean leaveCapitals, boolean leaveWhitespace, boolean leaveSymbols){
|
||||
reset();
|
||||
this.leaveCapitals = leaveCapitals;
|
||||
this.leaveWhitespace = leaveWhitespace;
|
||||
this.leaveSymbols = leaveSymbols;
|
||||
}
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
public String encode(String input){
|
||||
//Make sure everything is empty before you begin
|
||||
reset();
|
||||
setInputString(input);
|
||||
return encode();
|
||||
}
|
||||
public String decode(String input){
|
||||
//Make sure everything is empty before you begin
|
||||
reset();
|
||||
setInputString(input);
|
||||
return decode();
|
||||
}
|
||||
public void reset(){
|
||||
inputString = outputString = "";
|
||||
}
|
||||
}
|
||||
105
src/main/java/com/mattrixwv/CipherStreamJava/Autokey.java
Normal file
105
src/main/java/com/mattrixwv/CipherStreamJava/Autokey.java
Normal file
@@ -0,0 +1,105 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Autokey.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-25-21
|
||||
//Modified: 01-04-22
|
||||
package com.mattrixwv.CipherStreamJava;
|
||||
|
||||
|
||||
public class Autokey extends Vigenere{
|
||||
//Special rules for setting the strings for encoding
|
||||
private void encodeSet(String key, String input) throws Exception{
|
||||
//Set the input
|
||||
setInputString(input);
|
||||
|
||||
StringBuilder newKey = new StringBuilder();
|
||||
//Remove all unneccessary elements from the key
|
||||
setKeyword(key);
|
||||
newKey.append(keyword);
|
||||
//Remove all unneccessary elements from the input
|
||||
setKeyword(input);
|
||||
newKey.append(getKeyword());
|
||||
|
||||
//Make sure the key is not any longer than the input
|
||||
key = newKey.substring(0, getKeyword().length());
|
||||
|
||||
//Set the new keyword
|
||||
setKeyword(key);
|
||||
//Make sure to update the offset
|
||||
offset.clear();
|
||||
setOffset();
|
||||
}
|
||||
//Setting the strings for decoding
|
||||
private void decodeSet(String key, String input) throws Exception{
|
||||
//Remove all unneccessary elements from the key
|
||||
setKeyword(key);
|
||||
//Remove all unneccessary elements from the input
|
||||
inputString = "";
|
||||
setInputString(input);
|
||||
}
|
||||
//Decodes the inputString
|
||||
protected String decode() throws Exception{
|
||||
//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 leaveCapitals, boolean leaveWhitespace, boolean leaveSymbols){
|
||||
super(leaveCapitals, leaveWhitespace, leaveSymbols);
|
||||
}
|
||||
//Encodes inputString using the Autokey cipher
|
||||
public String encode(String key, String input) throws Exception{
|
||||
reset();
|
||||
encodeSet(key, input);
|
||||
return encode();
|
||||
}
|
||||
//Decodes inputString using the Autokey cipher
|
||||
public String decode(String key, String input) throws Exception{
|
||||
reset();
|
||||
setInputString(input);
|
||||
decodeSet(key, input);
|
||||
return decode();
|
||||
}
|
||||
}
|
||||
148
src/main/java/com/mattrixwv/CipherStreamJava/Caesar.java
Normal file
148
src/main/java/com/mattrixwv/CipherStreamJava/Caesar.java
Normal file
@@ -0,0 +1,148 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Caesar.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-25-21
|
||||
//Modified: 01-04-22
|
||||
package com.mattrixwv.CipherStreamJava;
|
||||
|
||||
|
||||
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 leaveCapitals; //Whether to respect capitals in the output string
|
||||
private boolean leaveWhitespace; //Whether to respect whitespace in the output string
|
||||
private boolean leaveSymbols; //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){
|
||||
if(!leaveCapitals){
|
||||
inputString = inputString.toLowerCase();
|
||||
}
|
||||
if(!leaveWhitespace){
|
||||
inputString = inputString.replaceAll("\\s+", "");
|
||||
}
|
||||
if(!leaveSymbols){
|
||||
inputString = inputString.replaceAll("[^a-zA-Z0-9\\s]", "");
|
||||
}
|
||||
this.inputString = inputString;
|
||||
}
|
||||
//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();
|
||||
leaveCapitals = false;
|
||||
leaveWhitespace = false;
|
||||
leaveSymbols = false;
|
||||
}
|
||||
public Caesar(boolean leaveCapitals, boolean leaveWhitespace, boolean leaveSymbols){
|
||||
reset();
|
||||
this.leaveCapitals = leaveCapitals;
|
||||
this.leaveWhitespace = leaveWhitespace;
|
||||
this.leaveSymbols = leaveSymbols;
|
||||
}
|
||||
//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){
|
||||
reset();
|
||||
setShift(shiftAmount);
|
||||
setInputString(inputString);
|
||||
return encode();
|
||||
}
|
||||
//Sets the shift and inputString and decodes the message
|
||||
public String decode(int shiftAmount, String inputString){
|
||||
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,21 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Exceptions/InvalidCharacterException.java
|
||||
//Mattrixwv
|
||||
// Created: 01-04-22
|
||||
//Modified: 01-04-22
|
||||
package com.mattrixwv.CipherStreamJava.Exceptions;
|
||||
|
||||
|
||||
public class InvalidCharacterException extends Exception{
|
||||
public InvalidCharacterException(){
|
||||
super();
|
||||
}
|
||||
public InvalidCharacterException(String message){
|
||||
super(message);
|
||||
}
|
||||
public InvalidCharacterException(Throwable error){
|
||||
super(error);
|
||||
}
|
||||
public InvalidCharacterException(String message, Throwable error){
|
||||
super(message, error);
|
||||
}
|
||||
}
|
||||
121
src/main/java/com/mattrixwv/CipherStreamJava/Morse.java
Normal file
121
src/main/java/com/mattrixwv/CipherStreamJava/Morse.java
Normal file
@@ -0,0 +1,121 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Morse.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-28-21
|
||||
//Modified: 01-04-22
|
||||
package com.mattrixwv.CipherStreamJava;
|
||||
|
||||
|
||||
public class Morse{
|
||||
//Holds the Morse representation of the alphanumeric characters
|
||||
private static final String[] code = {
|
||||
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", //A-L
|
||||
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", //M-Z
|
||||
"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----." //0-9
|
||||
};
|
||||
public static final String version = "1.0"; //The current library's version
|
||||
private String inputString; //The string that needs encoded/decoded
|
||||
private String outputString; //The encoded/decoded message
|
||||
|
||||
//Encodes inputString and stores the result in outputString
|
||||
private String encode(){
|
||||
StringBuilder output = new StringBuilder();
|
||||
//Loop through every element in the input string and see what type it is
|
||||
for(char letter : inputString.toCharArray()){
|
||||
//If the character is a letter get teh correct combination from code and add it to the output
|
||||
if(Character.isUpperCase(letter)){
|
||||
output.append(code[letter - 65]);
|
||||
output.append(' ');
|
||||
}
|
||||
//If it is a number get the correct combination from code and add it to the output
|
||||
else if(Character.isDigit(letter)){
|
||||
int tempNum = Integer.parseInt(Character.toString(letter));
|
||||
output.append(code[tempNum + 26]);
|
||||
output.append(' ');
|
||||
}
|
||||
}
|
||||
//Remove the final space from the output
|
||||
if(output.length() > 0){
|
||||
output.replace(output.length() - 1, output.length(), "");
|
||||
}
|
||||
|
||||
//Save and return the output
|
||||
outputString = output.toString();
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString and stores the result in outputString
|
||||
private String decode(){
|
||||
StringBuilder output = new StringBuilder();
|
||||
for(String current : inputString.split(" ")){
|
||||
boolean found = false;
|
||||
//Loop through the code and see if the current letter
|
||||
for(int cnt = 0;(cnt < 26) && (!found);++cnt){
|
||||
//See if current is the same as an element in code
|
||||
if(current.equals(code[cnt])){
|
||||
//Add 65 to cnt to get the correct capital letter
|
||||
output.append((char)(cnt + 'A'));
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
//Loop through code and see if current is a number
|
||||
for(int cnt = 26;(cnt < 36) && (!found);++cnt){
|
||||
if(current.equals(code[cnt])){
|
||||
//Remove 26 from cnt to get the correct number
|
||||
output.append(Integer.toString(cnt - 26));
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
//If it is neither print an error in the output
|
||||
if(!found){
|
||||
output.append("<Unknown symbol: " + current + ">");
|
||||
}
|
||||
}
|
||||
|
||||
//Save and return the output
|
||||
outputString = output.toString();
|
||||
return outputString;
|
||||
}
|
||||
//Encodes input and returns the result
|
||||
private void setEncodeInputString(String input){
|
||||
//Make sure the letters are all uppercase
|
||||
input = input.toUpperCase();
|
||||
//Remove all characters that are not capital letters
|
||||
input = input.replaceAll("[^A-Z0-9]", "");
|
||||
//Save the new input
|
||||
inputString = input;
|
||||
}
|
||||
//Decodes input and returns the result
|
||||
private void setDecodeInputString(String input){
|
||||
//Remove all characters except ., -, and ' '
|
||||
input = input.replaceAll("[^ \\.\\-]", "");
|
||||
//Save the new input
|
||||
inputString = input;
|
||||
}
|
||||
|
||||
|
||||
//Constructor
|
||||
public Morse(){
|
||||
reset();
|
||||
}
|
||||
//Returns inputString
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
//Returns outputString
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
//Encodes input and returns the result
|
||||
public String encode(String input){
|
||||
setEncodeInputString(input);
|
||||
return encode();
|
||||
}
|
||||
//Decoes input and returns the result
|
||||
public String decode(String input){
|
||||
setDecodeInputString(input);
|
||||
return decode();
|
||||
}
|
||||
//Makes sure all variables are empty
|
||||
public void reset(){
|
||||
inputString = outputString = "";
|
||||
}
|
||||
}
|
||||
425
src/main/java/com/mattrixwv/CipherStreamJava/Playfair.java
Normal file
425
src/main/java/com/mattrixwv/CipherStreamJava/Playfair.java
Normal file
@@ -0,0 +1,425 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Playfair.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-30-21
|
||||
//Modified: 01-04-22
|
||||
package com.mattrixwv.CipherStreamJava;
|
||||
|
||||
|
||||
import com.mattrixwv.CipherStreamJava.Exceptions.InvalidCharacterException;
|
||||
|
||||
|
||||
public class Playfair{
|
||||
//A class representing the location of a character in the grid
|
||||
private class CharLocation{
|
||||
private int x;
|
||||
private int y;
|
||||
public CharLocation(int x, int y){
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
public int getX(){
|
||||
return x;
|
||||
}
|
||||
public int getY(){
|
||||
return y;
|
||||
}
|
||||
}
|
||||
|
||||
//Variables
|
||||
private boolean leaveCapitals; //Whether to respect captials in the output string
|
||||
private boolean leaveWhitespace; //Whether to respect whitespace in the output string
|
||||
private boolean leaveSymbols; //Whether to respect symbols in the output string
|
||||
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 replaced replaced in the input string or keyword
|
||||
private char doubled; //The letter that will be placed between double letters in the input string if necessary or to make the string length even
|
||||
private String inputString; //The message that needs to be encoded/decoded
|
||||
private String outputString; //The encoded/decoded message
|
||||
private String keyword; //The keyword used to create the grid
|
||||
private char[][] grid; //The grid used to encode/decode the message
|
||||
//Create the grid from the keyword
|
||||
private void createGrid(){
|
||||
for(int row = 0;row < 5;++row){
|
||||
for(int col = 0;col < 5;++col){
|
||||
char letter = keyword.charAt((5 * row) + col);
|
||||
grid[row][col] = letter;
|
||||
}
|
||||
}
|
||||
}
|
||||
//Strips invalid characters from the string that needs encoded/decoded
|
||||
private void setInputString(String inputString, boolean encoding) throws InvalidCharacterException{
|
||||
//Make sure the input string is not null
|
||||
if(inputString == null){
|
||||
throw new InvalidCharacterException("The input string cannot be null");
|
||||
}
|
||||
|
||||
//Set the options
|
||||
if(!leaveCapitals){
|
||||
inputString = inputString.toUpperCase();
|
||||
}
|
||||
if(!leaveWhitespace){
|
||||
inputString = inputString.replaceAll("\\s+", "");
|
||||
}
|
||||
if(!leaveSymbols){
|
||||
inputString = inputString.replaceAll("[^a-zA-Z0-9\\s]", "");
|
||||
}
|
||||
|
||||
//Make replace all of the replacers with replaced
|
||||
inputString = inputString.replaceAll(Character.toString(replaced), Character.toString(replacer));
|
||||
|
||||
//If there is nothing in the input string throw an exception
|
||||
if(inputString.isBlank()){
|
||||
throw new InvalidCharacterException("The input string cannot be blank");
|
||||
}
|
||||
|
||||
//If this is encoding parse it and clean up an problems
|
||||
if(encoding){
|
||||
//Replace characters that need replaced
|
||||
inputString = inputString.replaceAll(Character.toString(replaced), Character.toString(replacer));
|
||||
|
||||
//Check if there are any doubled characters
|
||||
StringBuilder cleanInput = new StringBuilder();
|
||||
int letterCount = 0;
|
||||
for(int cnt = 0;cnt < inputString.length();){
|
||||
//Advance until you find a letter, saving the input for inclusion
|
||||
StringBuilder prepend = new StringBuilder();
|
||||
while((cnt < inputString.length()) && !Character.isAlphabetic(inputString.charAt(cnt))){
|
||||
prepend.append(inputString.charAt(cnt++));
|
||||
}
|
||||
//If we have reached the end of the string end the loop
|
||||
if(cnt == inputString.length()){
|
||||
cleanInput.append(prepend);
|
||||
break;
|
||||
}
|
||||
//Get the next character
|
||||
char firstLetter = inputString.charAt(cnt++);
|
||||
++letterCount;
|
||||
|
||||
//Advance until you find a letter, saving the input for inclusion
|
||||
StringBuilder middle = new StringBuilder();
|
||||
while((cnt < inputString.length()) && !Character.isAlphabetic(inputString.charAt(cnt))){
|
||||
middle.append(inputString.charAt(cnt++));
|
||||
}
|
||||
char secondLetter = '\0';
|
||||
//If we have not reached the end of the string get the next character
|
||||
if(cnt != inputString.length()){
|
||||
secondLetter = inputString.charAt(cnt++);
|
||||
++letterCount;
|
||||
}
|
||||
//If the second character is the same as the first character set the pointer back and use the doubled character
|
||||
if(secondLetter == firstLetter){
|
||||
--cnt;
|
||||
secondLetter = doubled;
|
||||
}
|
||||
|
||||
//Add all of the gathered input to the cleaned up input
|
||||
cleanInput.append(prepend);
|
||||
cleanInput.append(firstLetter);
|
||||
cleanInput.append(middle);
|
||||
if(secondLetter != '\0'){
|
||||
cleanInput.append(secondLetter);
|
||||
}
|
||||
}
|
||||
|
||||
//Check if there are an odd number of characters
|
||||
if((letterCount % 2) == 1){
|
||||
int lastLetterLocation = cleanInput.length() - 1;
|
||||
while(!Character.isAlphabetic(cleanInput.charAt(lastLetterLocation))){
|
||||
--lastLetterLocation;
|
||||
}
|
||||
if(cleanInput.charAt(lastLetterLocation) == doubled){
|
||||
cleanInput.append(replacer);
|
||||
}
|
||||
else{
|
||||
cleanInput.append(doubled);
|
||||
}
|
||||
}
|
||||
|
||||
this.inputString = cleanInput.toString();
|
||||
}
|
||||
//If this is decoding just add it without parsing it
|
||||
else{
|
||||
//Throw an exception if replaced is included
|
||||
if(inputString.contains(Character.toString(replaced))){
|
||||
throw new InvalidCharacterException("An encoded message cannot contain a letter that needs replaced");
|
||||
}
|
||||
|
||||
this.inputString = inputString;
|
||||
}
|
||||
}
|
||||
//Returns the input string ready for encoding
|
||||
private String getPreparedInputString(){
|
||||
String cleanString = inputString.toUpperCase();
|
||||
cleanString = cleanString.replaceAll("[^A-Z]", "");
|
||||
return cleanString;
|
||||
}
|
||||
//Strips invalid characters from the keyword and creates the grid
|
||||
private void setKeyword(String key){
|
||||
//Change everything to uppercase
|
||||
key = key.toUpperCase();
|
||||
|
||||
//Removing everything except capital letters
|
||||
key = key.replaceAll("[^A-Z]", "");
|
||||
|
||||
//Add all letters in the alphabet to the key
|
||||
key += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
//Replace all replaced characters
|
||||
key = key.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();
|
||||
|
||||
//Create the grid from the sanitized keyword
|
||||
createGrid();
|
||||
}
|
||||
//Returns the location of the given character in the grid
|
||||
private CharLocation findChar(char letter) throws Exception{
|
||||
for(int row = 0;row < grid.length;++row){
|
||||
for(int col = 0;col < grid[row].length;++col){
|
||||
if(grid[row][col] == letter){
|
||||
return new CharLocation(row, col);
|
||||
}
|
||||
}
|
||||
}
|
||||
//If it was not found something went wrong
|
||||
throw new Exception("That character was not found in the grid. ERROR");
|
||||
}
|
||||
//Returns the location in the grid of x and y, adjusting for out of bounds
|
||||
private char getGridChar(int x, int y){
|
||||
if(x < 0){
|
||||
x += 5;
|
||||
}
|
||||
if(y < 0){
|
||||
y += 5;
|
||||
}
|
||||
return grid[x % 5][y % 5];
|
||||
}
|
||||
//Adds characters that aren't letters to the output
|
||||
private void addCharactersToCleanString(String cleanString){
|
||||
int outputCnt = 0;
|
||||
StringBuilder fullOutput = new StringBuilder();
|
||||
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
|
||||
if(Character.isUpperCase(inputString.charAt(inputCnt))){
|
||||
fullOutput.append(cleanString.charAt(outputCnt++));
|
||||
}
|
||||
else if(Character.isLowerCase(inputString.charAt(inputCnt))){
|
||||
fullOutput.append(Character.toLowerCase(cleanString.charAt(outputCnt++)));
|
||||
}
|
||||
else{
|
||||
fullOutput.append(inputString.charAt(inputCnt));
|
||||
}
|
||||
}
|
||||
outputString = fullOutput.toString();
|
||||
}
|
||||
//Encodes inputString using the Playfair cipher and stores the result in outputString
|
||||
private String encode() throws Exception{
|
||||
StringBuilder output = new StringBuilder();
|
||||
int inputCnt = 0;
|
||||
String cleanString = getPreparedInputString();
|
||||
while(inputCnt < cleanString.length()){
|
||||
//Get the next 2 letters to be encoded
|
||||
char firstLetter = cleanString.charAt(inputCnt++);
|
||||
char secondLetter = cleanString.charAt(inputCnt++);
|
||||
|
||||
//Find the letters in the grid
|
||||
CharLocation firstLocation = findChar(firstLetter);
|
||||
CharLocation secondLocation = findChar(secondLetter);
|
||||
|
||||
//Encode the letters
|
||||
if(firstLocation.getX() == secondLocation.getX()){
|
||||
firstLetter = getGridChar(firstLocation.getX(), firstLocation.getY() + 1);
|
||||
secondLetter = getGridChar(secondLocation.getX(), secondLocation.getY() + 1);
|
||||
}
|
||||
else if(firstLocation.getY() == secondLocation.getY()){
|
||||
firstLetter = getGridChar(firstLocation.getX() + 1, firstLocation.getY());
|
||||
secondLetter = getGridChar(secondLocation.getX() + 1, secondLocation.getY());
|
||||
}
|
||||
else{
|
||||
firstLetter = getGridChar(firstLocation.getX(), secondLocation.getY());
|
||||
secondLetter = getGridChar(secondLocation.getX(), firstLocation.getY());
|
||||
}
|
||||
|
||||
//Add the new letters to the output string
|
||||
output.append(firstLetter);
|
||||
output.append(secondLetter);
|
||||
}
|
||||
|
||||
//Add other characters to the output string
|
||||
addCharactersToCleanString(output.toString());
|
||||
|
||||
//Return the output string
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString using the Playfair cipher and stores the result in outputString
|
||||
private String decode() throws Exception{
|
||||
StringBuilder output = new StringBuilder();
|
||||
int inputCnt = 0;
|
||||
String cleanString = getPreparedInputString();
|
||||
while(inputCnt < cleanString.length()){
|
||||
//Get the next 2 letters to be encoded
|
||||
char firstLetter = cleanString.charAt(inputCnt++);
|
||||
char secondLetter = cleanString.charAt(inputCnt++);
|
||||
|
||||
//Find the letters in the grid
|
||||
CharLocation firstLocation = findChar(firstLetter);
|
||||
CharLocation secondLocation = findChar(secondLetter);
|
||||
|
||||
//Decode the letters
|
||||
if(firstLocation.getX() == secondLocation.getX()){
|
||||
firstLetter = getGridChar(firstLocation.getX(), firstLocation.getY() - 1);
|
||||
secondLetter = getGridChar(secondLocation.getX(), secondLocation.getY() - 1);
|
||||
}
|
||||
else if(firstLocation.getY() == secondLocation.getY()){
|
||||
firstLetter = getGridChar(firstLocation.getX() - 1, firstLocation.getY());
|
||||
secondLetter = getGridChar(secondLocation.getX() - 1, secondLocation.getY());
|
||||
}
|
||||
else{
|
||||
firstLetter = getGridChar(firstLocation.getX(), secondLocation.getY());
|
||||
secondLetter = getGridChar(secondLocation.getX(), firstLocation.getY());
|
||||
}
|
||||
|
||||
//Add the new letters to the output string
|
||||
output.append(firstLetter);
|
||||
output.append(secondLetter);
|
||||
}
|
||||
|
||||
//Add other characters to the output string
|
||||
addCharactersToCleanString(output.toString());
|
||||
|
||||
//Return the output string
|
||||
return outputString;
|
||||
}
|
||||
|
||||
public Playfair() throws InvalidCharacterException{
|
||||
reset();
|
||||
leaveCapitals = false;
|
||||
leaveWhitespace = false;
|
||||
leaveSymbols = false;
|
||||
setReplaced('J');
|
||||
setReplacer('I');
|
||||
setDoubled('X');
|
||||
}
|
||||
public Playfair(boolean leaveCapitals, boolean leaveWhitespace, boolean leaveSymbols) throws InvalidCharacterException{
|
||||
reset();
|
||||
this.leaveCapitals = leaveCapitals;
|
||||
this.leaveWhitespace = leaveWhitespace;
|
||||
this.leaveSymbols = leaveSymbols;
|
||||
setReplaced('J');
|
||||
setReplacer('I');
|
||||
setDoubled('X');
|
||||
}
|
||||
public Playfair(boolean leaveCapitals, boolean leaveWhitespace, boolean leaveSymbols, char replaced, char replacer, char doubled) throws InvalidCharacterException{
|
||||
reset();
|
||||
this.leaveCapitals = leaveCapitals;
|
||||
this.leaveWhitespace = leaveWhitespace;
|
||||
this.leaveSymbols = leaveSymbols;
|
||||
setReplaced(replaced);
|
||||
setReplacer(replacer);
|
||||
setDoubled(doubled);
|
||||
}
|
||||
//Sets the keyword and inputString and encodes the message
|
||||
public String encode(String keyword, String input) throws InvalidCharacterException, Exception{
|
||||
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{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setInputString(input, false);
|
||||
return decode();
|
||||
}
|
||||
|
||||
//Makes sure all variables are empty
|
||||
public void reset(){
|
||||
grid = new char[5][5];
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
keyword = "";
|
||||
}
|
||||
//Gets
|
||||
public char getReplaced(){
|
||||
return replaced;
|
||||
}
|
||||
public void setReplaced(char replaced) throws InvalidCharacterException{
|
||||
if(!Character.isAlphabetic(replaced)){
|
||||
throw new InvalidCharacterException("The replaced character must be a letter");
|
||||
}
|
||||
|
||||
if(replaced == replacer){
|
||||
throw new InvalidCharacterException("The replaced letter cannot be the same as the replacing letter");
|
||||
}
|
||||
|
||||
if(replaced == doubled){
|
||||
throw new InvalidCharacterException("The replaced letter cannot be the same as the replacing letter");
|
||||
}
|
||||
|
||||
this.replaced = Character.toUpperCase(replaced);
|
||||
}
|
||||
public char getReplacer(){
|
||||
return replacer;
|
||||
}
|
||||
public void setReplacer(char replacer) throws InvalidCharacterException{
|
||||
//Make sure the character is a letter
|
||||
if(!Character.isAlphabetic(replacer)){
|
||||
throw new InvalidCharacterException("The replacer must be a letter");
|
||||
|
||||
}
|
||||
|
||||
//Make sure the character isn't the same as what it is supposed to replace
|
||||
if(replacer == replaced){
|
||||
throw new InvalidCharacterException("The replacer cannot be the same letter as what it is replacing");
|
||||
}
|
||||
|
||||
//Make sure the replacer isn't the same as the double letter replacer
|
||||
if(replacer == doubled){
|
||||
throw new InvalidCharacterException("The replacer cannot be the same as the double letter replacer");
|
||||
}
|
||||
|
||||
this.replacer = Character.toUpperCase(replacer);
|
||||
}
|
||||
public char getDoubled(){
|
||||
return doubled;
|
||||
}
|
||||
public void setDoubled(char doubled) throws InvalidCharacterException{
|
||||
//Make sure the character is a letter
|
||||
if(!Character.isAlphabetic(doubled)){
|
||||
throw new InvalidCharacterException("The double letter replacement must be a letter");
|
||||
}
|
||||
|
||||
//Make sure the 2 replacers are the same
|
||||
if(doubled == replacer){
|
||||
throw new InvalidCharacterException("The double letter replacement cannot be the same as the regular replacer");
|
||||
}
|
||||
|
||||
//Make sure the double letter replacement isn't the same as the letter being replaced
|
||||
if(doubled == replaced){
|
||||
throw new InvalidCharacterException("The double letter replacement cannot be the same as the letter replaced");
|
||||
}
|
||||
|
||||
this.doubled = Character.toUpperCase(doubled);
|
||||
}
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
public String getGrid(){
|
||||
StringBuilder gridString = new StringBuilder();
|
||||
for(char[] row : grid){
|
||||
for(char col : row){
|
||||
gridString.append(col);
|
||||
}
|
||||
}
|
||||
|
||||
return gridString.toString();
|
||||
}
|
||||
}
|
||||
334
src/main/java/com/mattrixwv/CipherStreamJava/PolybiusSquare.java
Normal file
334
src/main/java/com/mattrixwv/CipherStreamJava/PolybiusSquare.java
Normal file
@@ -0,0 +1,334 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/PolybiusSquare.java
|
||||
//Mattrixwv
|
||||
// Created: 01-04-22
|
||||
//Modified: 01-04-22
|
||||
package com.mattrixwv.CipherStreamJava;
|
||||
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import com.mattrixwv.CipherStreamJava.Exceptions.InvalidCharacterException;
|
||||
|
||||
|
||||
public class PolybiusSquare{
|
||||
//A class representing the location of a character in the grid
|
||||
private class CharLocation{
|
||||
private int x;
|
||||
private int y;
|
||||
public CharLocation(int x, int y){
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
public int getX(){
|
||||
return x;
|
||||
}
|
||||
public int getY(){
|
||||
return y;
|
||||
}
|
||||
}
|
||||
|
||||
//Variables
|
||||
private String inputString; //The message that needs to be encoded/decoded
|
||||
private String outputString; //The encoded/decoded message
|
||||
private String keyword; //The keyword used to create the grid
|
||||
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
|
||||
|
||||
//Create the grid from the keyword
|
||||
public void createGrid(){
|
||||
for(int row = 0;row < 5;++row){
|
||||
for(int col = 0;col < 5;++col){
|
||||
char letter = keyword.charAt((5 * row) + col);
|
||||
grid[row][col] = letter;
|
||||
}
|
||||
}
|
||||
}
|
||||
//Strips invalid characters from the string that needs encoded/decoded
|
||||
public void setInputStringEncoding(String inputString) throws InvalidCharacterException{
|
||||
//Make sure the string doesn't contain any numbers
|
||||
for(char ch = '0';ch <= '9';++ch){
|
||||
if(inputString.contains(Character.toString(ch))){
|
||||
throw new InvalidCharacterException("Inputs for encoding cannot contain numbers");
|
||||
}
|
||||
}
|
||||
|
||||
//Change to upper case
|
||||
inputString = inputString.toUpperCase();
|
||||
|
||||
//Remove any whitespace if selected
|
||||
if(!leaveWhitespace){
|
||||
inputString = inputString.replaceAll("\\s+", "");
|
||||
}
|
||||
|
||||
//Remove any symbols if selected
|
||||
if(!leaveSymbols){
|
||||
inputString = inputString.replaceAll("[^a-zA-Z0-9\\s]", "");
|
||||
}
|
||||
|
||||
if(!leaveWhitespace && !leaveSymbols){
|
||||
//Add whitespace after every character for the default look
|
||||
StringJoiner spacedString = new StringJoiner(" ");
|
||||
for(int cnt = 0;cnt < inputString.length();++cnt){
|
||||
spacedString.add(Character.toString(inputString.charAt(cnt)));
|
||||
}
|
||||
inputString = spacedString.toString();
|
||||
}
|
||||
|
||||
//Replace any characters that need replaced
|
||||
inputString = inputString.replaceAll(Character.toString(replaced), Character.toString(replacer));
|
||||
|
||||
//Save the string
|
||||
this.inputString = inputString;
|
||||
}
|
||||
public void setInputStringDecoding(String inputString) throws InvalidCharacterException{
|
||||
//Make sure the string contains an even number of digits and no letters
|
||||
int numberOfDigits = 0;
|
||||
for(int cnt = 0;cnt < inputString.length();++cnt){
|
||||
char ch = inputString.charAt(cnt);
|
||||
if(Character.isDigit(ch)){
|
||||
++numberOfDigits;
|
||||
}
|
||||
else if(Character.isAlphabetic(ch)){
|
||||
throw new InvalidCharacterException("Inputs for decoding cannot contains letters");
|
||||
}
|
||||
}
|
||||
if((numberOfDigits % 2) != 0){
|
||||
throw new InvalidCharacterException("There must be an even number of digits in an encoded string");
|
||||
}
|
||||
|
||||
//Remove any whitespace if selected
|
||||
if(!leaveWhitespace){
|
||||
inputString = inputString.replaceAll("\\s+", "");
|
||||
}
|
||||
|
||||
//Remove any symbols if selected
|
||||
if(!leaveSymbols){
|
||||
inputString = inputString.replaceAll("[^0-9\\s]", "");
|
||||
}
|
||||
|
||||
//Save the string
|
||||
this.inputString = inputString;
|
||||
}
|
||||
//Returns the input string ready for encoding
|
||||
public String getPreparedInputStringEncoding(){
|
||||
String cleanString = inputString.toUpperCase();
|
||||
cleanString = cleanString.replaceAll("[^A-Z]", "");
|
||||
return cleanString;
|
||||
}
|
||||
public String getPreparedInputStringDecoding(){
|
||||
String cleanString = inputString.replaceAll("[^0-9]", "");
|
||||
return cleanString;
|
||||
}
|
||||
//Strips invalid characters from the keyword and creates the grid
|
||||
public void setKeyword(String key){
|
||||
//Change everything to uppercase
|
||||
key = key.toUpperCase();
|
||||
|
||||
//Remove everything except capital letters
|
||||
key = key.replaceAll("[^A-Z]", "");
|
||||
|
||||
//Add all letters in the alphabet to the key
|
||||
key += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
//Replace all replaced characters
|
||||
key = key.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();
|
||||
|
||||
//Create the grid from the sanitized keyword
|
||||
createGrid();
|
||||
}
|
||||
//Returns the location of the given charcter in the grid
|
||||
public CharLocation findChar(char letter) throws Exception{
|
||||
for(int row = 0;row < grid.length;++row){
|
||||
for(int col = 0;col < grid[row].length;++col){
|
||||
if(grid[row][col] == letter){
|
||||
return new CharLocation(row, col);
|
||||
}
|
||||
}
|
||||
}
|
||||
//If it was not found something went wrong
|
||||
throw new Exception("That character was not found in the grid. ERROR");
|
||||
}
|
||||
//Adds characters that aren't letters to the output
|
||||
public void addCharactersToCleanStringEncode(String cleanString){
|
||||
int outputCnt = 0;
|
||||
StringBuilder fullOutput = new StringBuilder();
|
||||
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
|
||||
//Add both numbers of any letters to the output
|
||||
if(Character.isAlphabetic(inputString.charAt(inputCnt))){
|
||||
fullOutput.append(cleanString.charAt(outputCnt++));
|
||||
fullOutput.append(cleanString.charAt(outputCnt++));
|
||||
}
|
||||
//Add any other characters that appear to the output
|
||||
else{
|
||||
fullOutput.append(inputString.charAt(inputCnt));
|
||||
}
|
||||
}
|
||||
outputString = fullOutput.toString();
|
||||
}
|
||||
public void addCharactersToCleanStringDecode(String cleanString){
|
||||
int outputCnt = 0;
|
||||
StringBuilder fullOutput = new StringBuilder();
|
||||
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
|
||||
//Add the letter to the output and skip the second number
|
||||
if(Character.isDigit(inputString.charAt(inputCnt))){
|
||||
fullOutput.append(cleanString.charAt(outputCnt++));
|
||||
++inputCnt;
|
||||
}
|
||||
//Add any other characters that appear to the output
|
||||
else{
|
||||
fullOutput.append(inputString.charAt(inputCnt));
|
||||
}
|
||||
}
|
||||
outputString = fullOutput.toString();
|
||||
}
|
||||
//Encodes inputString using the Playfair cipher and stores the result in outputString
|
||||
private String encode() throws Exception{
|
||||
StringBuilder output = new StringBuilder();
|
||||
String cleanString = getPreparedInputStringEncoding();
|
||||
for(int cnt = 0;cnt < cleanString.length();++cnt){
|
||||
//Get the next character to be encoded
|
||||
char ch = cleanString.charAt(cnt);
|
||||
|
||||
//Find the letter in the grid
|
||||
CharLocation location = findChar(ch);
|
||||
|
||||
//Add the grid location to the output
|
||||
output.append(location.getX() + 1);
|
||||
output.append(location.getY() + 1);
|
||||
}
|
||||
|
||||
//Add other characters to the output string
|
||||
addCharactersToCleanStringEncode(output.toString());
|
||||
|
||||
//Return the output string
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString using the Playfair cipher and stores the result in outputString
|
||||
private String decode(){
|
||||
StringBuilder output = new StringBuilder();
|
||||
String cleanString = getPreparedInputStringDecoding();
|
||||
for(int cnt = 0;cnt < cleanString.length();){
|
||||
//Get the digits indicationg the location of the next character
|
||||
char firstDigit = cleanString.charAt(cnt++);
|
||||
char secondDigit = cleanString.charAt(cnt++);
|
||||
|
||||
//Get the next character
|
||||
char letter = grid[Integer.valueOf(Character.toString(firstDigit)) - 1][Integer.valueOf(Character.toString(secondDigit)) - 1];
|
||||
|
||||
//Add the new letter to the output
|
||||
output.append(letter);
|
||||
}
|
||||
|
||||
//Add other characters to the output
|
||||
addCharactersToCleanStringDecode(output.toString());
|
||||
|
||||
//Return the output string
|
||||
return outputString;
|
||||
}
|
||||
|
||||
public PolybiusSquare() throws InvalidCharacterException{
|
||||
reset();
|
||||
setReplaced('J');
|
||||
setReplacer('I');
|
||||
leaveWhitespace = false;
|
||||
leaveSymbols = false;
|
||||
}
|
||||
public PolybiusSquare(boolean leaveWhitespace, boolean leaveSymbols) throws InvalidCharacterException{
|
||||
reset();
|
||||
setReplaced('J');
|
||||
setReplacer('I');
|
||||
this.leaveWhitespace = leaveWhitespace;
|
||||
this.leaveSymbols = leaveSymbols;
|
||||
}
|
||||
public PolybiusSquare(boolean leaveWhitespace, boolean leaveSymbols, char replaced, char replacer) throws InvalidCharacterException{
|
||||
reset();
|
||||
setReplaced(replaced);
|
||||
setReplacer(replacer);
|
||||
this.leaveWhitespace = leaveWhitespace;
|
||||
this.leaveSymbols = leaveSymbols;
|
||||
}
|
||||
//Sets the keyword and inputString and encodes the message
|
||||
public String encode(String inputString) throws InvalidCharacterException, Exception{
|
||||
return encode("", inputString);
|
||||
}
|
||||
public String encode(String keyword, String inputString) throws InvalidCharacterException, Exception{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setInputStringEncoding(inputString);
|
||||
return encode();
|
||||
}
|
||||
//Sets the keyword and inputString and decodes the message
|
||||
public String decode(String inputString) throws InvalidCharacterException{
|
||||
return decode("", inputString);
|
||||
}
|
||||
public String decode(String keyword, String inputString) throws InvalidCharacterException{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setInputStringDecoding(inputString);
|
||||
return decode();
|
||||
}
|
||||
|
||||
//Makes sure all variables are empty
|
||||
public void reset(){
|
||||
grid = new char[5][5];
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
keyword = "";
|
||||
}
|
||||
//Gets
|
||||
public char getReplaced(){
|
||||
return replaced;
|
||||
}
|
||||
public void setReplaced(char replaced) throws InvalidCharacterException{
|
||||
if(!Character.isAlphabetic(replaced)){
|
||||
throw new InvalidCharacterException("The replaced character must be a letter");
|
||||
}
|
||||
|
||||
if(replaced == replacer){
|
||||
throw new InvalidCharacterException("The replaced letter cannot be the same as the replacing letter");
|
||||
}
|
||||
|
||||
this.replaced = Character.toUpperCase(replaced);
|
||||
}
|
||||
public char getReplacer(){
|
||||
return replacer;
|
||||
}
|
||||
public void setReplacer(char replacer) throws InvalidCharacterException{
|
||||
if(!Character.isAlphabetic(replacer)){
|
||||
throw new InvalidCharacterException("The replacer character must be a letter");
|
||||
}
|
||||
|
||||
if(replaced == replacer){
|
||||
throw new InvalidCharacterException("The replacer letter cannot be the same as the replaced letter");
|
||||
}
|
||||
|
||||
this.replacer = replacer;
|
||||
}
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
public String getGrid(){
|
||||
StringBuilder gridString = new StringBuilder();
|
||||
for(char[] row : grid){
|
||||
for(char col : row){
|
||||
gridString.append(col);
|
||||
}
|
||||
}
|
||||
|
||||
return gridString.toString();
|
||||
}
|
||||
}
|
||||
177
src/main/java/com/mattrixwv/CipherStreamJava/Vigenere.java
Normal file
177
src/main/java/com/mattrixwv/CipherStreamJava/Vigenere.java
Normal file
@@ -0,0 +1,177 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Vigenere.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-25-21
|
||||
//Modified: 01-04-22
|
||||
package com.mattrixwv.CipherStreamJava;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
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 leaveCapitals; //Whether to respect capitals in the output string
|
||||
protected boolean leaveWhitespace; //Whether to respect whitespace in the output string
|
||||
protected boolean leaveSymbols; //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){
|
||||
if(!leaveCapitals){
|
||||
inputString = inputString.toLowerCase();
|
||||
}
|
||||
if(!leaveWhitespace){
|
||||
inputString = inputString.replaceAll("\\s+", "");
|
||||
}
|
||||
if(!leaveSymbols){
|
||||
inputString = inputString.replaceAll("[^a-zA-Z0-9\\s]", "");
|
||||
}
|
||||
this.inputString = inputString;
|
||||
}
|
||||
//Sets keyword
|
||||
protected void setKeyword(String key) throws Exception{
|
||||
//Convert all letters to uppercase
|
||||
key = key.toUpperCase();
|
||||
//Remove all characters except capital letters
|
||||
key = key.replaceAll("[^A-Z]", "");
|
||||
//Save the string
|
||||
keyword = key;
|
||||
|
||||
//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 Exception("The keyword cannot be empty");
|
||||
}
|
||||
}
|
||||
//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() throws Exception{
|
||||
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<Integer>();
|
||||
reset();
|
||||
leaveCapitals = false;
|
||||
leaveWhitespace = false;
|
||||
leaveSymbols = false;
|
||||
}
|
||||
public Vigenere(boolean leaveCapitals, boolean leaveWhitespace, boolean leaveSymbols){
|
||||
offset = new ArrayList<Integer>();
|
||||
reset();
|
||||
this.leaveCapitals = leaveCapitals;
|
||||
this.leaveWhitespace = leaveWhitespace;
|
||||
this.leaveSymbols = leaveSymbols;
|
||||
}
|
||||
//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 ArrayList<Integer> getOffsets(){
|
||||
return offset;
|
||||
}
|
||||
//Encodes input using key and returns the result
|
||||
public String encode(String key, String input) throws Exception{
|
||||
reset();
|
||||
setKeyword(key);
|
||||
setInputString(input);
|
||||
return encode();
|
||||
}
|
||||
//Decodes input using key and returns the result
|
||||
public String decode(String key, String input) throws Exception{
|
||||
reset();
|
||||
setKeyword(key);
|
||||
setInputString(input);
|
||||
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