Atbash cipher implemented

This commit is contained in:
2021-07-25 16:59:48 -04:00
parent 23c58a6fdc
commit 300a3acd06
2 changed files with 110 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
//CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/Atbash.java
//Matthew Ellison
// Created: 07-25-21
//Modified: 07-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
//Decodes inputString and stores in outputString
private String decode(){
//Stop through every element in the inputString and shift it the correct amount
for(int cnt = 0;cnt < inputString.length();++cnt){
outputString += (char)(inputString.charAt(cnt) + 25 - (2 * (inputString.charAt(cnt) - 'A')));
}
return outputString;
}
//Encodes inputString and stores in outputString
private String encode(){
//Step through every element in the inputString and shift it the correct amount
for(int cnt = 0;cnt < inputString.length();++cnt){
outputString += (char)(inputString.charAt(cnt) + 25 - (2 * (inputString.charAt(cnt) - 'A')));
}
return outputString;
}
//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]", "");
//Save the string
inputString = input;
}
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 = "";
}
}