Merge branch 'Matthew-Ellison/added-one-time-pad-cipher-1645644781440' into develop

This commit is contained in:
2022-03-01 18:54:22 -05:00
3 changed files with 593 additions and 0 deletions

View File

@@ -0,0 +1,145 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Beaufort.java
//Mattrixwv
// Created: 02-23-22
//Modified: 02-23-22
package com.mattrixwv.CipherStreamJava.monoSubstitution;
import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException;
import com.mattrixwv.CipherStreamJava.exceptions.InvalidKeywordException;
public class Beaufort{
private String inputString; //This is the string that needs encoded/decoded
private String outputString; //This is the string that is output after encoding/decoding
private String keyword; //This is the keyword that is responsible for determining the offsets that you change each character by
private boolean preserveCapitals; //Whether to respect capitals in the output string
private boolean preserveWhitespace; //Whether to respect whitespace in the output string
private boolean preserveSymbols; //Whether to respect symbols in the output string
private Atbash atbash; //The first step in encoding/decoding the cipher
private Caesar caesar; //The second step in encoding/decoding the cipher
private Vigenere vigenere; //The third step in encoding/decoding the cipher
//Ensures inputString constraints
public void setInputString(String inputString) throws InvalidInputException{
//Make sure the input isn't null
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
}
//Apply removal options
if(!preserveCapitals){
inputString = inputString.toUpperCase();
}
if(!preserveWhitespace){
inputString = inputString.replaceAll("\\s", "");
}
if(!preserveSymbols){
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
}
//Save the string
this.inputString = inputString;
//Make sure the string isn't blank
if(this.inputString.isBlank()){
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
//Ensures keyword constraints
public void setKeyword(String keyword) throws InvalidKeywordException{
//Make sure the keyword isn't null
if(keyword == null){
throw new InvalidKeywordException("Keyword cannot be null");
}
//Convert all letters to uppercase
keyword = keyword.toUpperCase();
//Remove all characters except capital letters
keyword = keyword.replaceAll("[^A-Z]", "");
//Save the string
this.keyword = keyword;
//If after all the elimination of unusable characters the keyword is empty throw an exception
if(this.keyword.isBlank() || (this.keyword.length() < 2)){
throw new InvalidKeywordException("Keyword must contain at least 2 letters");
}
}
//Encodes the inputString and stores the result in outputString
public void encode() throws InvalidKeywordException, InvalidInputException{
//Reverse the string
String atbashString = atbash.encode(inputString);
//Shift the reversal by 1
//?Not quite sure why this is needed. Need to look into this cipher a bit more closely
String caesarString = caesar.encode(1, atbashString);
//Shift each letter according to the key
String vigenereString = vigenere.encode(keyword, caesarString);
//Save the output
this.outputString = vigenereString;
}
//Decodes the inputString and stores the result in outputString
public void decode() throws InvalidKeywordException, InvalidInputException{
//Decoding is just encoding again
encode();
}
//Constructor
public Beaufort(){
preserveCapitals = false;
preserveWhitespace = false;
preserveSymbols = false;
atbash = new Atbash(false, false, false);
caesar = new Caesar(false, false, false);
vigenere = new Vigenere(false, false, false);
reset();
}
public Beaufort(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
this.preserveCapitals = preserveCapitals;
this.preserveWhitespace = preserveWhitespace;
this.preserveSymbols = preserveSymbols;
atbash = new Atbash(preserveCapitals, preserveWhitespace, preserveSymbols);
caesar = new Caesar(preserveCapitals, preserveWhitespace, preserveSymbols);
vigenere = new Vigenere(preserveCapitals, preserveWhitespace, preserveSymbols);
reset();
}
//Returns the current inputString
public String getInputString(){
return inputString;
}
//Returns the current outputString
public String getOutputString(){
return outputString;
}
//Returns the current keyword
public String getKeyword(){
return keyword;
}
//Encodes inputString using keyword and returns the result
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
//Set the parameters
setKeyword(keyword);
setInputString(inputString);
//Encode and return the message
encode();
return outputString;
}
//Decodes inputString using keyword and returns the result
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
//Set the parameters
setKeyword(keyword);
setInputString(inputString);
//Decode and return the message
decode();
return outputString;
}
//Makes sure all of the variables are empty
public void reset(){
inputString = "";
outputString = "";
keyword = "";
}
}

View File

@@ -0,0 +1,41 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/OneTimePad.java
//Mattrixwv
// Created: 02-23-22
//Modified: 02-23-22
package com.mattrixwv.CipherStreamJava.monoSubstitution;
import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException;
import com.mattrixwv.CipherStreamJava.exceptions.InvalidKeywordException;
public class OneTimePad extends Vigenere{
//?Add some kind of entropy calculator?
//?Add some kind of "book passage includer"?
//Constructor
public OneTimePad(){
super();
}
public OneTimePad(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
super(preserveCapitals, preserveWhitespace, preserveSymbols);
}
//Encodes input using key and returns the result
@Override
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
if(keyword.length() < inputString.length()){
throw new InvalidKeywordException("Key must be at least as long as the input");
}
return super.encode(keyword, inputString);
}
//Decodes input using key and returns the result
@Override
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
if(keyword.length() < inputString.length()){
throw new InvalidKeywordException("Key must be at least as long as the input");
}
return super.decode(keyword, inputString);
}
}

View File

@@ -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);
}
}