From feb5047d2a93b3bf074b4fe0d4a1b6cf14f8c5bc Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Thu, 30 Dec 2021 16:20:22 -0500 Subject: [PATCH] Added encryption options --- .../mattrixwv/CipherStreamJava/Atbash.java | 66 +++- .../CipherStreamJava/TestAtbash.java | 368 +++++++++++++++++- 2 files changed, 406 insertions(+), 28 deletions(-) diff --git a/src/main/java/mattrixwv/CipherStreamJava/Atbash.java b/src/main/java/mattrixwv/CipherStreamJava/Atbash.java index 97060c5..ead61d1 100644 --- a/src/main/java/mattrixwv/CipherStreamJava/Atbash.java +++ b/src/main/java/mattrixwv/CipherStreamJava/Atbash.java @@ -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; } diff --git a/src/test/java/mattrixwv/CipherStreamJava/TestAtbash.java b/src/test/java/mattrixwv/CipherStreamJava/TestAtbash.java index 60bd228..706d467 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; @@ -14,34 +14,364 @@ import org.junit.Test; public class TestAtbash{ @Test public void testDecode(){ - Atbash cipher = new Atbash(); + Atbash cipher = new Atbash(true, true, true); - //Test 1 + //Test lowercase decoding 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"; + assertEquals("Atbash failed lowercase decoding.", correctOutput, output); + //Test uppercase decoding + input = "ZYX"; + correctOutput = "ABC"; output = cipher.decode(input); - assertEquals("Atbash Decoding failed the second test", correctOutput, output); + assertEquals("Atbash failed uppercase decoding.", correctOutput, output); + + + //Test whitespace decoding + input = "zyx wvu"; + correctOutput = "abc def"; + output = cipher.decode(input); + assertEquals("Atbash failed whitespace decoding.", correctOutput, output); + + + //Test symbol decoding + input = "zyx-wvu@"; + correctOutput = "abc-def@"; + output = cipher.decode(input); + assertEquals("Atbash failed symbol decoding.", correctOutput, output); + + + //Test mixed case, whitespace, and symbol decoding + input = "Gsv jfrxp yildm ulc qfnkh levi - gsv ozab wlt"; + correctOutput = "The quick brown fox jumps over - the lazy dog"; + output = cipher.decode(input); + assertEquals("Atbash failed mixed case, whitespace, and symbol decoding.", correctOutput, output); } + @Test + public void testNoWhitespaceDecode(){ + Atbash cipher = new Atbash(true, false, true); + + //Test lowercase decoding + String input = "zyx"; + String correctOutput = "abc"; + String output = cipher.decode(input); + assertEquals("Atbash failed no whitespace lowercase decoding.", correctOutput, output); + //Test uppercase decoding + input = "ZYX"; + correctOutput = "ABC"; + output = cipher.decode(input); + assertEquals("Atbash failed no whitespace uppercase decoding.", correctOutput, output); + + + //Test whitespace decoding + input = "zyx wvu"; + correctOutput = "abcdef"; + output = cipher.decode(input); + assertEquals("Atbash failed no whitespace whitespace decoding.", correctOutput, output); + + + //Test symbol decoding + input = "zyx-wvu@"; + correctOutput = "abc-def@"; + output = cipher.decode(input); + assertEquals("Atbash failed no whitespace symbol decoding.", correctOutput, output); + + + //Test mixed case, whitespace, and symbol decoding + input = "Gsv jfrxp yildm ulc qfnkh levi - gsv ozab wlt"; + correctOutput = "Thequickbrownfoxjumpsover-thelazydog"; + output = cipher.decode(input); + assertEquals("Atbash failed no whitespace mixed case, whitespace, and symbol decoding.", correctOutput, output); + } + @Test + public void testNoCapitalDecode(){ + Atbash cipher = new Atbash(false, true, true); + + //Test lowercase decoding + String input = "zyx"; + String correctOutput = "abc"; + String output = cipher.decode(input); + assertEquals("Atbash failed no capital lowercase decoding.", correctOutput, output); + //Test uppercase decoding + input = "ZYX"; + correctOutput = "abc"; + output = cipher.decode(input); + assertEquals("Atbash failed no capital uppercase decoding.", correctOutput, output); + + + //Test whitespace decoding + input = "zyx wvu"; + correctOutput = "abc def"; + output = cipher.decode(input); + assertEquals("Atbash failed no capital whitespace decoding.", correctOutput, output); + + + //Test symbol decoding + input = "zyx-wvu@"; + correctOutput = "abc-def@"; + output = cipher.decode(input); + assertEquals("Atbash failed no capital symbol decoding.", correctOutput, output); + + + //Test mixed case, whitespace, and symbol decoding + input = "Gsv jfrxp yildm ulc qfnkh levi - gsv ozab wlt"; + correctOutput = "the quick brown fox jumps over - the lazy dog"; + output = cipher.decode(input); + assertEquals("Atbash failed no capital mixed case, whitespace, and symbol decoding.", correctOutput, output); + } + @Test + public void testNoSymbolDecode(){ + Atbash cipher = new Atbash(true, true, false); + + //Test lowercase decoding + String input = "zyx"; + String correctOutput = "abc"; + String output = cipher.decode(input); + assertEquals("Atbash failed no symbol lowercase decoding.", correctOutput, output); + //Test uppercase decoding + input = "ZYX"; + correctOutput = "ABC"; + output = cipher.decode(input); + assertEquals("Atbash failed no symbol uppercase decoding.", correctOutput, output); + + + //Test whitespace decoding + input = "zyx wvu"; + correctOutput = "abc def"; + output = cipher.decode(input); + assertEquals("Atbash failed no symbol whitespace decoding.", correctOutput, output); + + + //Test symbol decoding + input = "zyx-wvu@"; + correctOutput = "abcdef"; + output = cipher.decode(input); + assertEquals("Atbash failed no symbol symbol decoding.", correctOutput, output); + + + //Test mixed case, whitespace, and symbol decoding + input = "Gsv jfrxp yildm ulc qfnkh levi - gsv ozab wlt"; + correctOutput = "The quick brown fox jumps over the lazy dog"; + output = cipher.decode(input); + assertEquals("Atbash failed no symbol mixed case, whitespace, and symbol decoding.", correctOutput, output); + } + @Test + public void testNoCapitalwhitesapceSymbolDecode(){ + Atbash cipher = new Atbash(false, false, false); + + //Test lowercase decoding + String input = "zyx"; + String correctOutput = "abc"; + String output = cipher.decode(input); + assertEquals("Atbash failed secure lowercase decoding.", correctOutput, output); + //Test uppercase decoding + input = "ZYX"; + correctOutput = "abc"; + output = cipher.decode(input); + assertEquals("Atbash failed secure uppercase decoding.", correctOutput, output); + + + //Test whitespace decoding + input = "zyx wvu"; + correctOutput = "abcdef"; + output = cipher.decode(input); + assertEquals("Atbash failed secure whitespace decoding.", correctOutput, output); + + + //Test symbol decoding + input = "zyx-wvu@"; + correctOutput = "abcdef"; + output = cipher.decode(input); + assertEquals("Atbash failed secure symbol decoding.", correctOutput, output); + + + //Test mixed case, whitespace, and symbol decoding + input = "Gsv jfrxp yildm ulc qfnkh levi - gsv ozab wlt"; + correctOutput = "thequickbrownfoxjumpsoverthelazydog"; + output = cipher.decode(input); + assertEquals("Atbash failed secure mixed case, whitespace, and symbol decoding.", correctOutput, output); + } + + @Test public void testEncode(){ - Atbash cipher = new Atbash(); + Atbash cipher = new Atbash(true, true, true); - //Test 1 + //Test lowercase encoding 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"; + assertEquals("Atbash failed lowercase encoding.", correctOutput, output); + //Test uppercase encoding + input = "ABC"; + correctOutput = "ZYX"; output = cipher.encode(input); - assertEquals("Atbash Encoding failed the second test", correctOutput, output); + assertEquals("Atbash failed uppercase encoding.", correctOutput, output); + + + //Test whitespace encoding + input = "abc def"; + correctOutput = "zyx wvu"; + output = cipher.encode(input); + assertEquals("Atbash failed whitespace encoding.", correctOutput, output); + + + //Test symbol decoding + input = "abc-def@"; + correctOutput = "zyx-wvu@"; + output = cipher.encode(input); + assertEquals("Atbash failed symbol encoding.", correctOutput, output); + + + //Test mixed case, whitespace, and symbol decoding + input = "The quick brown fox jumps over - the lazy dog"; + correctOutput = "Gsv jfrxp yildm ulc qfnkh levi - gsv ozab wlt"; + output = cipher.encode(input); + assertEquals("Atbash failed mixed case, whitespace, and symbol encoding.", correctOutput, output); + } + @Test + public void testNoWhitespaceEncode(){ + Atbash cipher = new Atbash(true, false, true); + + //Test lowercase encoding + String input = "abc"; + String correctOutput = "zyx"; + String output = cipher.encode(input); + assertEquals("Atbash failed no whitespace lowercase encoding.", correctOutput, output); + //Test uppercase encoding + input = "ABC"; + correctOutput = "ZYX"; + output = cipher.encode(input); + assertEquals("Atbash failed no whitespace uppercase encoding.", correctOutput, output); + + + //Test whitespace encoding + input = "abc def"; + correctOutput = "zyxwvu"; + output = cipher.encode(input); + assertEquals("Atbash failed no whitespace whitespace encoding.", correctOutput, output); + + + //Test symbol decoding + input = "abc-def@"; + correctOutput = "zyx-wvu@"; + output = cipher.encode(input); + assertEquals("Atbash failed no whitespace symbol encoding.", correctOutput, output); + + + //Test mixed case, whitespace, and symbol decoding + input = "The quick brown fox jumps over - the lazy dog"; + correctOutput = "Gsvjfrxpyildmulcqfnkhlevi-gsvozabwlt"; + output = cipher.encode(input); + assertEquals("Atbash failed no whitespace mixed case, whitespace, and symbol encoding.", correctOutput, output); + } + @Test + public void testNoCapitalEncode(){ + Atbash cipher = new Atbash(false, true, true); + + //Test lowercase encoding + String input = "abc"; + String correctOutput = "zyx"; + String output = cipher.encode(input); + assertEquals("Atbash failed no capital lowercase encoding.", correctOutput, output); + //Test uppercase encoding + input = "ABC"; + correctOutput = "zyx"; + output = cipher.encode(input); + assertEquals("Atbash failed no capital uppercase encoding.", correctOutput, output); + + + //Test whitespace encoding + input = "abc def"; + correctOutput = "zyx wvu"; + output = cipher.encode(input); + assertEquals("Atbash failed no capital whitespace encoding.", correctOutput, output); + + + //Test symbol decoding + input = "abc-def@"; + correctOutput = "zyx-wvu@"; + output = cipher.encode(input); + assertEquals("Atbash failed no capital symbol encoding.", correctOutput, output); + + + //Test mixed case, whitespace, and symbol decoding + input = "The quick brown fox jumps over - the lazy dog"; + correctOutput = "gsv jfrxp yildm ulc qfnkh levi - gsv ozab wlt"; + output = cipher.encode(input); + assertEquals("Atbash failed no capital mixed case, whitespace, and symbol encoding.", correctOutput, output); + } + @Test + public void testNoSymbolEncode(){ + Atbash cipher = new Atbash(true, true, false); + + //Test lowercase encoding + String input = "abc"; + String correctOutput = "zyx"; + String output = cipher.encode(input); + assertEquals("Atbash failed no symbol lowercase encoding.", correctOutput, output); + //Test uppercase encoding + input = "ABC"; + correctOutput = "ZYX"; + output = cipher.encode(input); + assertEquals("Atbash failed no symbol uppercase encoding.", correctOutput, output); + + + //Test whitespace encoding + input = "abc def"; + correctOutput = "zyx wvu"; + output = cipher.encode(input); + assertEquals("Atbash failed no symbol whitespace encoding.", correctOutput, output); + + + //Test symbol decoding + input = "abc-def@"; + correctOutput = "zyxwvu"; + output = cipher.encode(input); + assertEquals("Atbash failed no symbol symbol encoding.", correctOutput, output); + + + //Test mixed case, whitespace, and symbol decoding + input = "The quick brown fox jumps over - the lazy dog"; + correctOutput = "Gsv jfrxp yildm ulc qfnkh levi gsv ozab wlt"; + output = cipher.encode(input); + assertEquals("Atbash failed no symbol mixed case, whitespace, and symbol encoding.", correctOutput, output); + } + @Test + public void testNoCapitalWhitespaceSymbolEncode(){ + Atbash cipher = new Atbash(false, false, false); + + //Test lowercase encoding + String input = "abc"; + String correctOutput = "zyx"; + String output = cipher.encode(input); + assertEquals("Atbash failed secure lowercase encoding.", correctOutput, output); + //Test uppercase encoding + input = "ABC"; + correctOutput = "zyx"; + output = cipher.encode(input); + assertEquals("Atbash failed secure uppercase encoding.", correctOutput, output); + + + //Test whitespace encoding + input = "abc def"; + correctOutput = "zyxwvu"; + output = cipher.encode(input); + assertEquals("Atbash failed secure whitespace encoding.", correctOutput, output); + + + //Test symbol decoding + input = "abc-def@"; + correctOutput = "zyxwvu"; + output = cipher.encode(input); + assertEquals("Atbash failed secure symbol encoding.", correctOutput, output); + + + //Test mixed case, whitespace, and symbol decoding + input = "The quick brown fox jumps over - the lazy dog"; + correctOutput = "gsvjfrxpyildmulcqfnkhlevigsvozabwlt"; + output = cipher.encode(input); + assertEquals("Atbash failed secure mixed case, whitespace, and symbol encoding.", correctOutput, output); } }