Added encryption options

This commit is contained in:
2021-12-30 16:20:22 -05:00
parent 05bcb03536
commit feb5047d2a
2 changed files with 406 additions and 28 deletions

View File

@@ -1,21 +1,36 @@
//CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/Atbash.java
//Matthew Ellison
// Created: 07-25-21
//Modified: 07-25-21
//Modified: 12-25-21
//This is the declaration of the Atbash class
package mattrixwv.CipherStreamJava;
public class Atbash{
public static final String version = "1.0";
private String inputString; //Holds the string that needs encoded or decoded
private String outputString; //Holds teh current version of the library
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){
output.append((char)(inputString.charAt(cnt) + 25 - (2 * (inputString.charAt(cnt) - 'A'))));
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 punctuation/whitespace the way it is
else{
output.append(currentChar);
}
}
outputString = output.toString();
@@ -26,7 +41,20 @@ public class Atbash{
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){
output.append((char)(inputString.charAt(cnt) + 25 - (2 * (inputString.charAt(cnt) - 'A'))));
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();
@@ -34,15 +62,35 @@ public class Atbash{
}
//Removes all invalid characters and sets inputString
private void setInputString(String input){
//Convert all letters to uppercase
input = input.toUpperCase();
//Remove all characters except capital letters
input = input.replaceAll("[^A-Z]", "");
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;
}