From 11c06657a51ed2ade1d895f8abc531f93a9296a3 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Mon, 27 Dec 2021 12:21:33 -0500 Subject: [PATCH] Atbash chaptial transform started --- .../mattrixwv/CipherStreamJava/Atbash.java | 40 +++++++++++++++---- .../mattrixwv/CipherStreamJava/Morse.java | 1 - .../mattrixwv/CipherStreamJava/Vigenere.java | 1 - .../CipherStreamJava/TestAtbash.java | 10 ++--- 4 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/main/java/mattrixwv/CipherStreamJava/Atbash.java b/src/main/java/mattrixwv/CipherStreamJava/Atbash.java index 97060c5..461a1ab 100644 --- a/src/main/java/mattrixwv/CipherStreamJava/Atbash.java +++ b/src/main/java/mattrixwv/CipherStreamJava/Atbash.java @@ -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){ - //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 uppercase + input = input.toUpperCase(); + } + if(!leaveWhitespace){ + //Remove all characters except capital letters + 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; } diff --git a/src/main/java/mattrixwv/CipherStreamJava/Morse.java b/src/main/java/mattrixwv/CipherStreamJava/Morse.java index 44e9cf0..f5fdbe2 100644 --- a/src/main/java/mattrixwv/CipherStreamJava/Morse.java +++ b/src/main/java/mattrixwv/CipherStreamJava/Morse.java @@ -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 diff --git a/src/main/java/mattrixwv/CipherStreamJava/Vigenere.java b/src/main/java/mattrixwv/CipherStreamJava/Vigenere.java index 5b07e45..9d73600 100644 --- a/src/main/java/mattrixwv/CipherStreamJava/Vigenere.java +++ b/src/main/java/mattrixwv/CipherStreamJava/Vigenere.java @@ -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 diff --git a/src/test/java/mattrixwv/CipherStreamJava/TestAtbash.java b/src/test/java/mattrixwv/CipherStreamJava/TestAtbash.java index 60bd228..daa09ee 100644 --- a/src/test/java/mattrixwv/CipherStreamJava/TestAtbash.java +++ b/src/test/java/mattrixwv/CipherStreamJava/TestAtbash.java @@ -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); }