Atbash chaptial transform started

This commit is contained in:
2021-12-27 12:21:33 -05:00
parent 6c40edbdfe
commit 11c06657a5
4 changed files with 37 additions and 15 deletions

View File

@@ -1,15 +1,16 @@
//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
//Decodes inputString and stores in outputString
private String decode(){
StringBuilder output = new StringBuilder();
@@ -26,7 +27,16 @@ 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'))));
//Skip any whitespace
if(Character.isWhitespace(inputString.charAt(cnt))){
continue;
}
//Use either uppercase or lowercase for the base
char letterBase = 'a';
if(Character.isUpperCase(inputString.charAt(cnt))){
letterBase = 'A';
}
output.append((char)(inputString.charAt(cnt) + 25 - (2 * (inputString.charAt(cnt) - letterBase))));
}
outputString = output.toString();
@@ -34,15 +44,29 @@ public class Atbash{
}
//Removes all invalid characters and sets inputString
private void setInputString(String input){
if(!leaveCapitals){
//Convert all letters to uppercase
input = input.toUpperCase();
}
if(!leaveWhitespace){
//Remove all characters except capital letters
input = input.replaceAll("[^A-Z]", "");
input = input.toLowerCase();
}
//Save the string
inputString = input;
}
public Atbash(){
reset();
leaveCapitals = false;
leaveWhitespace = false;
}
public Atbash(boolean leaveCapitals, boolean leaveWhitespace){
reset();
this.leaveCapitals = leaveCapitals;
this.leaveWhitespace = leaveWhitespace;
}
public String getInputString(){
return inputString;
}

View File

@@ -13,7 +13,6 @@ public class Morse{
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", //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

View File

@@ -8,7 +8,6 @@ package mattrixwv.CipherStreamJava;
import java.util.ArrayList;
public class Vigenere{
public static final String version = "1.0"; //The current library's version number
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

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/TestAtbash.java
//Matthew Ellison
// Created: 07-25-21
//Modified: 07-25-21
//Modified: 12-25-21
//These are the tests for the Atbash class
package mattrixwv.CipherStreamJava;
@@ -18,13 +18,13 @@ public class TestAtbash{
//Test 1
String input = "zyx";
String correctOutput = "ABC";
String correctOutput = "abc";
String output = cipher.decode(input);
assertEquals("Atbash Decoding failed the first test", correctOutput, output);
//Test 2
input = "GSV JFRXP YILDM ULC QFNKH LEVI - GSV OZAB WLT";
correctOutput = "THEQUICKBROWNFOXJUMPSOVERTHELAZYDOG";
correctOutput = "thequickbrownfoxjumpsoverthelazydog";
output = cipher.decode(input);
assertEquals("Atbash Decoding failed the second test", correctOutput, output);
}
@@ -34,13 +34,13 @@ public class TestAtbash{
//Test 1
String input = "abc";
String correctOutput = "ZYX";
String correctOutput = "zyx";
String output = cipher.encode(input);
assertEquals("Atbash Encoding failed the first test", correctOutput, output);
//Test 2
input = "The quick brown fox jumps over - the lazy dog";
correctOutput = "GSVJFRXPYILDMULCQFNKHLEVIGSVOZABWLT";
correctOutput = "gsvjfrxpyildmulcqfnkhlevigsvozabwlt";
output = cipher.encode(input);
assertEquals("Atbash Encoding failed the second test", correctOutput, output);
}