From c65947a630f73157210aea0a490a265f8c26126e Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Tue, 25 Jan 2022 23:23:49 -0500 Subject: [PATCH] Updated Atbash to run faster --- .../monoSubstitution/Atbash.java | 48 +- .../monoSubstitution/TestAtbash.java | 446 +++++++++--------- 2 files changed, 255 insertions(+), 239 deletions(-) diff --git a/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Atbash.java b/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Atbash.java index 5752e69..a79f397 100644 --- a/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Atbash.java +++ b/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Atbash.java @@ -5,6 +5,9 @@ package com.mattrixwv.CipherStreamJava.monoSubstitution; +import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException; + + public class Atbash{ private String inputString; //Holds the string that needs encoded or decoded private String outputString; //The encoded/decoded string @@ -20,12 +23,13 @@ public class Atbash{ //Decode if the letter is alphabetic if(Character.isAlphabetic(currentChar)){ //Use either uppercase or lowercase for the base - char letterBase = 'a'; + //(letterbase + 25 - (currentChar - letterBase)) if(Character.isUpperCase(currentChar)){ - letterBase = 'A'; + output.append((char)(155 - currentChar)); + } + else{ + output.append((char)(219 - currentChar)); } - //TODO: Test and see if there is a more efficient way to do this - output.append((char)(currentChar + 25 - (2 * (currentChar - letterBase)))); } //Keep any punctuation/whitespace the way it is else{ @@ -45,12 +49,13 @@ public class Atbash{ //Decode if the letter is alphabetic if(Character.isAlphabetic(currentChar)){ //Use either uppercase or lowercase for the base - char letterBase = 'a'; + //(letterbase + 25 - (currentChar - letterBase)) if(Character.isUpperCase(currentChar)){ - letterBase = 'A'; + output.append((char)(155 - currentChar)); + } + else{ + output.append((char)(219 - currentChar)); } - //TODO: Test and see if there is a more efficient way to do this - output.append((char)(currentChar + 25 - (2 * (currentChar - letterBase)))); } //Keep any punctuatio/whitespace the way it is else{ @@ -62,21 +67,30 @@ public class Atbash{ return outputString; } //Removes all invalid characters and sets inputString - private void setInputString(String input){ + private void setInputString(String inputString) throws InvalidInputException{ + if(inputString == null){ + throw new NullPointerException("Input cannot be null"); + } + if(!preserveCapitals){ //Convert all letters to lowercase - input = input.toLowerCase(); + inputString = inputString.toLowerCase(); } if(!preserveWhitespace){ //Remove all characters except capital letters - input = input.replaceAll("\\s+", ""); + inputString = inputString.replaceAll("\\s+", ""); } if(!preserveSymbols){ //Remove all non-alpha numeric and whitespace symbols - input = input.replaceAll("[^a-zA-Z0-9\\s]", ""); + inputString = inputString.replaceAll("[^a-zA-Z\\s]", ""); } + //Save the string - inputString = input; + this.inputString = inputString; + + if(this.inputString.isBlank()){ + throw new InvalidInputException("Input must contain at least 1 character"); + } } @@ -98,16 +112,16 @@ public class Atbash{ public String getOutputString(){ return outputString; } - public String encode(String input){ + public String encode(String inputString) throws InvalidInputException{ //Make sure everything is empty before you begin reset(); - setInputString(input); + setInputString(inputString); return encode(); } - public String decode(String input){ + public String decode(String inputString) throws InvalidInputException{ //Make sure everything is empty before you begin reset(); - setInputString(input); + setInputString(inputString); return decode(); } public void reset(){ diff --git a/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestAtbash.java b/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestAtbash.java index 28cdf18..a754d9e 100644 --- a/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestAtbash.java +++ b/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestAtbash.java @@ -7,194 +7,14 @@ package com.mattrixwv.CipherStreamJava.monoSubstitution; import static org.junit.Assert.assertEquals; +import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException; + import org.junit.Test; public class TestAtbash{ @Test - public void testDecode(){ - Atbash cipher = new Atbash(true, true, true); - - //Test lowercase decoding - String input = "zyx"; - String correctOutput = "abc"; - String output = cipher.decode(input); - assertEquals("Atbash failed lowercase decoding.", correctOutput, output); - //Test uppercase decoding - input = "ZYX"; - correctOutput = "ABC"; - output = cipher.decode(input); - 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(){ + public void testEncode() throws InvalidInputException{ Atbash cipher = new Atbash(true, true, true); //Test lowercase encoding @@ -230,43 +50,7 @@ public class TestAtbash{ 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(){ + public void testNoCapitalEncode() throws InvalidInputException{ Atbash cipher = new Atbash(false, true, true); //Test lowercase encoding @@ -302,7 +86,43 @@ public class TestAtbash{ assertEquals("Atbash failed no capital mixed case, whitespace, and symbol encoding.", correctOutput, output); } @Test - public void testNoSymbolEncode(){ + public void testNoWhitespaceEncode() throws InvalidInputException{ + 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 testNoSymbolEncode() throws InvalidInputException{ Atbash cipher = new Atbash(true, true, false); //Test lowercase encoding @@ -338,7 +158,7 @@ public class TestAtbash{ assertEquals("Atbash failed no symbol mixed case, whitespace, and symbol encoding.", correctOutput, output); } @Test - public void testNoCapitalWhitespaceSymbolEncode(){ + public void testNoCapitalWhitespaceSymbolEncode() throws InvalidInputException{ Atbash cipher = new Atbash(false, false, false); //Test lowercase encoding @@ -373,4 +193,186 @@ public class TestAtbash{ output = cipher.encode(input); assertEquals("Atbash failed secure mixed case, whitespace, and symbol encoding.", correctOutput, output); } + + + @Test + public void testDecode() throws InvalidInputException{ + Atbash cipher = new Atbash(true, true, true); + + //Test lowercase decoding + String input = "zyx"; + String correctOutput = "abc"; + String output = cipher.decode(input); + assertEquals("Atbash failed lowercase decoding.", correctOutput, output); + //Test uppercase decoding + input = "ZYX"; + correctOutput = "ABC"; + output = cipher.decode(input); + 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 testNoCapitalDecode() throws InvalidInputException{ + 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 testNoWhitespaceDecode() throws InvalidInputException{ + 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 testNoSymbolDecode() throws InvalidInputException{ + 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() throws InvalidInputException{ + 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); + } }