From 750e3c774e18f4e5f02c46c81bb18832c4f7bcd0 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Wed, 23 Feb 2022 19:33:01 +0000 Subject: [PATCH] Added One Time Pad cipher --- .../monoSubstitution/TestOneTimePad.java | 407 ++++++++++++++++++ 1 file changed, 407 insertions(+) create mode 100644 src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestOneTimePad.java diff --git a/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestOneTimePad.java b/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestOneTimePad.java new file mode 100644 index 0000000..1f07f03 --- /dev/null +++ b/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestOneTimePad.java @@ -0,0 +1,407 @@ +//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestOneTimePad.java +//Mattrixwv +// Created: 02-23-22 +//Modified: 02-23-22 +package com.mattrixwv.CipherStreamJava.monoSubstitution; + + +import static org.junit.Assert.assertEquals; + +import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException; +import com.mattrixwv.CipherStreamJava.exceptions.InvalidKeywordException; + +import org.junit.Test; + + +public class TestOneTimePad{ + @Test + public void testEncode() throws InvalidKeywordException, InvalidInputException{ + OneTimePad cipher = new OneTimePad(true, true, true); + + //Test lowercase encoding + String inputString = "messagetoencode"; + String keyword = "keywordThatIsTotallyRandom"; + String correctOutput = "wiqooxhmvegkgws"; + String output = cipher.encode(keyword, inputString); + assertEquals("OneTimePad failed lowercase encoding.", correctOutput, output); + //Test uppercase encoding + inputString = "MESSAGETOENCODE"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "WIQOOXHMVEGKGWS"; + output = cipher.encode(keyword, inputString); + assertEquals("OneTimePad failed uppercase encoding.", correctOutput, output); + + //Test whitespace encoding + inputString = "message to encode"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "wiqooxh mv egkgws"; + output = cipher.encode(keyword, inputString); + assertEquals("OneTimePad failed whitespace encoding.", correctOutput, output); + + //Test symbol encoding + inputString = "message*to+encode"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "wiqooxh*mv+egkgws"; + output = cipher.encode(keyword, inputString); + assertEquals("OneTimePad failed symbol encoding.", correctOutput, output); + + //Test mixed case, whtiespace, symbol encoding + inputString = "Message to^encode"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "Wiqooxh mv^egkgws"; + output = cipher.encode(keyword, inputString); + assertEquals("OneTimePad failed mixed case, whitespace, symbol encoding.", correctOutput, output); + } + + @Test + public void testNoCapitalEncode() throws InvalidKeywordException, InvalidInputException{ + OneTimePad cipher = new OneTimePad(false, true, true); + + //Test lowercase encoding + String inputString = "messagetoencode"; + String keyword = "keywordThatIsTotallyRandom"; + String correctOutput = "WIQOOXHMVEGKGWS"; + String output = cipher.encode(keyword, inputString); + assertEquals("OneTimePad failed no capital lowercase encoding.", correctOutput, output); + //Test uppercase encoding + inputString = "MESSAGETOENCODE"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "WIQOOXHMVEGKGWS"; + output = cipher.encode(keyword, inputString); + assertEquals("OneTimePad failed no capital uppercase encoding.", correctOutput, output); + + //Test whitespace encoding + inputString = "message to encode"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "WIQOOXH MV EGKGWS"; + output = cipher.encode(keyword, inputString); + assertEquals("OneTimePad failed no capital whitespace encoding.", correctOutput, output); + + //Test symbol encoding + inputString = "message*to+encode"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "WIQOOXH*MV+EGKGWS"; + output = cipher.encode(keyword, inputString); + assertEquals("OneTimePad failed no capital symbol encoding.", correctOutput, output); + + //Test mixed case, whtiespace, symbol encoding + inputString = "Message to^encode"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "WIQOOXH MV^EGKGWS"; + output = cipher.encode(keyword, inputString); + assertEquals("OneTimePad failed no capital mixed case, whitespace, symbol encoding.", correctOutput, output); + } + + @Test + public void testNoWhitespaceEncode() throws InvalidKeywordException, InvalidInputException{ + OneTimePad cipher = new OneTimePad(true, false, true); + + //Test lowercase encoding + String inputString = "messagetoencode"; + String keyword = "keywordThatIsTotallyRandom"; + String correctOutput = "wiqooxhmvegkgws"; + String output = cipher.encode(keyword, inputString); + assertEquals("OneTimePad failed no whitespace lowercase encoding.", correctOutput, output); + //Test uppercase encoding + inputString = "MESSAGETOENCODE"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "WIQOOXHMVEGKGWS"; + output = cipher.encode(keyword, inputString); + assertEquals("OneTimePad failed no whitespace uppercase encoding.", correctOutput, output); + + //Test whitespace encoding + inputString = "message to encode"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "wiqooxhmvegkgws"; + output = cipher.encode(keyword, inputString); + assertEquals("OneTimePad failed no whitespace whitespace encoding.", correctOutput, output); + + //Test symbol encoding + inputString = "message*to+encode"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "wiqooxh*mv+egkgws"; + output = cipher.encode(keyword, inputString); + assertEquals("OneTimePad failed no whitespace symbol encoding.", correctOutput, output); + + //Test mixed case, whtiespace, symbol encoding + inputString = "Message to^encode"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "Wiqooxhmv^egkgws"; + output = cipher.encode(keyword, inputString); + assertEquals("OneTimePad failed no whitespace mixed case, whitespace, symbol encoding.", correctOutput, output); + } + + @Test + public void testNoSymbolEncode() throws InvalidKeywordException, InvalidInputException{ + OneTimePad cipher = new OneTimePad(true, true, false); + + //Test lowercase encoding + String inputString = "messagetoencode"; + String keyword = "keywordThatIsTotallyRandom"; + String correctOutput = "wiqooxhmvegkgws"; + String output = cipher.encode(keyword, inputString); + assertEquals("OneTimePad failed no symbol lowercase encoding.", correctOutput, output); + //Test uppercase encoding + inputString = "MESSAGETOENCODE"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "WIQOOXHMVEGKGWS"; + output = cipher.encode(keyword, inputString); + assertEquals("OneTimePad failed no symbol uppercase encoding.", correctOutput, output); + + //Test whitespace encoding + inputString = "message to encode"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "wiqooxh mv egkgws"; + output = cipher.encode(keyword, inputString); + assertEquals("OneTimePad failed no symbol whitespace encoding.", correctOutput, output); + + //Test symbol encoding + inputString = "message*to+encode"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "wiqooxhmvegkgws"; + output = cipher.encode(keyword, inputString); + assertEquals("OneTimePad failed no symbol symbol encoding.", correctOutput, output); + + //Test mixed case, whtiespace, symbol encoding + inputString = "Message to^encode"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "Wiqooxh mvegkgws"; + output = cipher.encode(keyword, inputString); + assertEquals("OneTimePad failed no symbol mixed case, whitespace, symbol encoding.", correctOutput, output); + } + + @Test + public void testNoWhitespaceSymbolEncode() throws InvalidKeywordException, InvalidInputException{ + OneTimePad cipher = new OneTimePad(false, false, false); + + //Test lowercase encoding + String inputString = "messagetoencode"; + String keyword = "keywordThatIsTotallyRandom"; + String correctOutput = "WIQOOXHMVEGKGWS"; + String output = cipher.encode(keyword, inputString); + assertEquals("OneTimePad failed secure lowercase encoding.", correctOutput, output); + //Test uppercase encoding + inputString = "MESSAGETOENCODE"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "WIQOOXHMVEGKGWS"; + output = cipher.encode(keyword, inputString); + assertEquals("OneTimePad failed secure uppercase encoding.", correctOutput, output); + + //Test whitespace encoding + inputString = "message to encode"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "WIQOOXHMVEGKGWS"; + output = cipher.encode(keyword, inputString); + assertEquals("OneTimePad failed secure whitespace encoding.", correctOutput, output); + + //Test symbol encoding + inputString = "message*to+encode"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "WIQOOXHMVEGKGWS"; + output = cipher.encode(keyword, inputString); + assertEquals("OneTimePad failed secure symbol encoding.", correctOutput, output); + + //Test mixed case, whtiespace, symbol encoding + inputString = "Message to^encode"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "WIQOOXHMVEGKGWS"; + output = cipher.encode(keyword, inputString); + assertEquals("OneTimePad failed secure mixed case, whitespace, symbol encoding.", correctOutput, output); + } + + + @Test + public void testDecode() throws InvalidKeywordException, InvalidInputException{ + OneTimePad cipher = new OneTimePad(true, true, true); + + //Test lowercase decoding + String inputString = "wiqooxhmvegkgws"; + String keyword = "keywordThatIsTotallyRandom"; + String correctOutput = "messagetoencode"; + String output = cipher.decode(keyword, inputString); + assertEquals("OneTimePad failed lowercase decoding.", correctOutput, output); + //Test uppercase decoding + inputString = "WIQOOXHMVEGKGWS"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "MESSAGETOENCODE"; + output = cipher.decode(keyword, inputString); + assertEquals("OneTimePad failed uppercase decoding.", correctOutput, output); + + //Test whitespace decoding + inputString = "wiqooxh mv egkgws"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "message to encode"; + output = cipher.decode(keyword, inputString); + assertEquals("OneTimePad failed whitespace decoding.", correctOutput, output); + + //Test symbol decoding + inputString = "wiqooxh*mv+egkgws"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "message*to+encode"; + output = cipher.decode(keyword, inputString); + assertEquals("OneTimePad failed symbol decoding.", correctOutput, output); + + //Test mixed case, whtiespace, symbol decoding + inputString = "Wiqooxh mv^egkgws"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "Message to^encode"; + output = cipher.decode(keyword, inputString); + assertEquals("OneTimePad failed mixed case, whitespace, symbol decoding.", correctOutput, output); + } + + @Test + public void testNoCapitalDecode() throws InvalidKeywordException, InvalidInputException{ + OneTimePad cipher = new OneTimePad(false, true, true); + + //Test lowercase decoding + String inputString = "wiqooxhmvegkgws"; + String keyword = "keywordThatIsTotallyRandom"; + String correctOutput = "MESSAGETOENCODE"; + String output = cipher.decode(keyword, inputString); + assertEquals("OneTimePad failed no capital lowercase decoding.", correctOutput, output); + //Test uppercase decoding + inputString = "WIQOOXHMVEGKGWS"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "MESSAGETOENCODE"; + output = cipher.decode(keyword, inputString); + assertEquals("OneTimePad failed no capital uppercase decoding.", correctOutput, output); + + //Test whitespace decoding + inputString = "wiqooxh mv egkgws"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "MESSAGE TO ENCODE"; + output = cipher.decode(keyword, inputString); + assertEquals("OneTimePad failed no capital whitespace decoding.", correctOutput, output); + + //Test symbol decoding + inputString = "wiqooxh*mv+egkgws"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "MESSAGE*TO+ENCODE"; + output = cipher.decode(keyword, inputString); + assertEquals("OneTimePad failed no capital symbol decoding.", correctOutput, output); + + //Test mixed case, whtiespace, symbol decoding + inputString = "Wiqooxh mv^egkgws"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "MESSAGE TO^ENCODE"; + output = cipher.decode(keyword, inputString); + assertEquals("OneTimePad failed no capital mixed case, whitespace, symbol decoding.", correctOutput, output); + } + + @Test + public void testNoWhitespaceDecode() throws InvalidKeywordException, InvalidInputException{ + OneTimePad cipher = new OneTimePad(true, false, true); + + //Test lowercase decoding + String inputString = "wiqooxhmvegkgws"; + String keyword = "keywordThatIsTotallyRandom"; + String correctOutput = "messagetoencode"; + String output = cipher.decode(keyword, inputString); + assertEquals("OneTimePad failed no whitespace lowercase decoding.", correctOutput, output); + //Test uppercase decoding + inputString = "WIQOOXHMVEGKGWS"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "MESSAGETOENCODE"; + output = cipher.decode(keyword, inputString); + assertEquals("OneTimePad failed no whitespace uppercase decoding.", correctOutput, output); + + //Test whitespace decoding + inputString = "wiqooxh mv egkgws"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "messagetoencode"; + output = cipher.decode(keyword, inputString); + assertEquals("OneTimePad failed no whitespace whitespace decoding.", correctOutput, output); + + //Test symbol decoding + inputString = "wiqooxh*mv+egkgws"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "message*to+encode"; + output = cipher.decode(keyword, inputString); + assertEquals("OneTimePad failed no whitespace symbol decoding.", correctOutput, output); + + //Test mixed case, whtiespace, symbol decoding + inputString = "Wiqooxh mv^egkgws"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "Messageto^encode"; + output = cipher.decode(keyword, inputString); + assertEquals("OneTimePad failed no whitespace mixed case, whitespace, symbol decoding.", correctOutput, output); + } + + @Test + public void testNoSymbolDecode() throws InvalidKeywordException, InvalidInputException{ + OneTimePad cipher = new OneTimePad(true, true, false); + + //Test lowercase decoding + String inputString = "wiqooxhmvegkgws"; + String keyword = "keywordThatIsTotallyRandom"; + String correctOutput = "messagetoencode"; + String output = cipher.decode(keyword, inputString); + assertEquals("OneTimePad failed no symbol lowercase decoding.", correctOutput, output); + //Test uppercase decoding + inputString = "WIQOOXHMVEGKGWS"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "MESSAGETOENCODE"; + output = cipher.decode(keyword, inputString); + assertEquals("OneTimePad failed no symbol uppercase decoding.", correctOutput, output); + + //Test whitespace decoding + inputString = "wiqooxh mv egkgws"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "message to encode"; + output = cipher.decode(keyword, inputString); + assertEquals("OneTimePad failed no symbol whitespace decoding.", correctOutput, output); + + //Test symbol decoding + inputString = "wiqooxh*mv+egkgws"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "messagetoencode"; + output = cipher.decode(keyword, inputString); + assertEquals("OneTimePad failed no symbol symbol decoding.", correctOutput, output); + + //Test mixed case, whtiespace, symbol decoding + inputString = "Wiqooxh mv^egkgws"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "Message toencode"; + output = cipher.decode(keyword, inputString); + assertEquals("OneTimePad failed no symbol mixed case, whitespace, symbol decoding.", correctOutput, output); + } + + @Test + public void testNoCapitalWhitespaceSymbolDecode() throws InvalidKeywordException, InvalidInputException{ + OneTimePad cipher = new OneTimePad(false, false, false); + + //Test lowercase decoding + String inputString = "wiqooxhmvegkgws"; + String keyword = "keywordThatIsTotallyRandom"; + String correctOutput = "MESSAGETOENCODE"; + String output = cipher.decode(keyword, inputString); + assertEquals("OneTimePad failed secure lowercase decoding.", correctOutput, output); + //Test uppercase decoding + inputString = "WIQOOXHMVEGKGWS"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "MESSAGETOENCODE"; + output = cipher.decode(keyword, inputString); + assertEquals("OneTimePad failed secure uppercase decoding.", correctOutput, output); + + //Test whitespace decoding + inputString = "wiqooxh mv egkgws"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "MESSAGETOENCODE"; + output = cipher.decode(keyword, inputString); + assertEquals("OneTimePad failed secure whitespace decoding.", correctOutput, output); + + //Test symbol decoding + inputString = "wiqooxh*mv+egkgws"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "MESSAGETOENCODE"; + output = cipher.decode(keyword, inputString); + assertEquals("OneTimePad failed secure symbol decoding.", correctOutput, output); + + //Test mixed case, whtiespace, symbol decoding + inputString = "Wiqooxh mv^egkgws"; + keyword = "keywordThatIsTotallyRandom"; + correctOutput = "MESSAGETOENCODE"; + output = cipher.decode(keyword, inputString); + assertEquals("OneTimePad failed secure mixed case, whitespace, symbol decoding.", correctOutput, output); + } +}