Finished Playfair cipher

This commit is contained in:
2022-01-04 02:46:58 -05:00
parent 45db9cc7fb
commit 97f1cd4ad6
2 changed files with 701 additions and 72 deletions

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/Playfair.java //CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/Playfair.java
//Matthew Ellison //Matthew Ellison
// Created: 07-30-21 // Created: 07-30-21
//Modified: 07-30-21 //Modified: 01-04-22
//This is the declaration of the Playfair class //This is the declaration of the Playfair class
package mattrixwv.CipherStreamJava; package mattrixwv.CipherStreamJava;
@@ -19,6 +19,21 @@ public class Playfair{
super(message, throwable); super(message, throwable);
} }
} }
//A class representing the location of a character in the grid
private class CharLocation{
private int x;
private int y;
public CharLocation(int x, int y){
this.x = x;
this.y = y;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
}
//Variables //Variables
private boolean leaveCapitals; //Whether to respect captials in the output string private boolean leaveCapitals; //Whether to respect captials in the output string
@@ -41,7 +56,7 @@ public class Playfair{
} }
} }
//Strips invalid characters from the string that needs encoded/decoded //Strips invalid characters from the string that needs encoded/decoded
private void setInputString(String inputString) throws InvalidCharacterException{ private void setInputString(String inputString, boolean encoding) throws InvalidCharacterException{
//Make sure the input string is not null //Make sure the input string is not null
if(inputString == null){ if(inputString == null){
throw new InvalidCharacterException("The input string cannot be null"); throw new InvalidCharacterException("The input string cannot be null");
@@ -49,13 +64,13 @@ public class Playfair{
//Set the options //Set the options
if(!leaveCapitals){ if(!leaveCapitals){
inputString.toLowerCase(); inputString = inputString.toUpperCase();
} }
if(!leaveWhitespace){ if(!leaveWhitespace){
inputString = inputString.replaceAll("\\s+", ""); inputString = inputString.replaceAll("\\s+", "");
} }
if(!leaveSymbols){ if(!leaveSymbols){
inputString = inputString.replaceAll("[^a-zA-Z0-9\\s+]", ""); inputString = inputString.replaceAll("[^a-zA-Z0-9\\s]", "");
} }
//Make replace all of the replacers with replaced //Make replace all of the replacers with replaced
@@ -66,45 +81,80 @@ public class Playfair{
throw new InvalidCharacterException("The input string cannot be blank"); throw new InvalidCharacterException("The input string cannot be blank");
} }
//Replace characters that need replaced //If this is encoding parse it and clean up an problems
StringBuilder sanitizedInput = new StringBuilder(); if(encoding){
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){ //Replace characters that need replaced
//Get the first letter inputString = inputString.replaceAll(Character.toString(replaced), Character.toString(replacer));
char firstLetter = inputString.charAt(inputCnt++);
//Make sure the first letter doesn't need replaced //Check if there are any doubled characters
if(firstLetter == replaced){ StringBuilder cleanInput = new StringBuilder();
firstLetter = replacer; int letterCount = 0;
for(int cnt = 0;cnt < inputString.length();){
//Advance until you find a letter, saving the input for inclusion
StringBuilder prepend = new StringBuilder();
while((cnt < inputString.length()) && !Character.isAlphabetic(inputString.charAt(cnt))){
prepend.append(inputString.charAt(cnt++));
}
//If we have reached the end of the string end the loop
if(cnt == inputString.length()){
cleanInput.append(prepend);
break;
}
//Get the next character
char firstLetter = inputString.charAt(cnt++);
++letterCount;
//Advance until you find a letter, saving the input for inclusion
StringBuilder middle = new StringBuilder();
while((cnt < inputString.length()) && !Character.isAlphabetic(inputString.charAt(cnt))){
middle.append(inputString.charAt(cnt++));
}
char secondLetter = '\0';
//If we have not reached the end of the string get the next character
if(cnt != inputString.length()){
secondLetter = inputString.charAt(cnt++);
++letterCount;
}
//If the second character is the same as the first character set the pointer back and use the doubled character
if(secondLetter == firstLetter){
--cnt;
secondLetter = doubled;
}
//Add all of the gathered input to the cleaned up input
cleanInput.append(prepend);
cleanInput.append(firstLetter);
cleanInput.append(middle);
if(secondLetter != '\0'){
cleanInput.append(secondLetter);
}
} }
//Save all of the characters inbetween letters //Check if there are an odd number of characters
StringBuilder inBetween = new StringBuilder(); if((letterCount % 2) == 1){
while((!Character.isAlphabetic(inputString.charAt(inputCnt))) && (inputCnt != inputString.length())){ int lastLetterLocation = cleanInput.length() - 1;
inBetween.append(inputString.charAt(inputCnt++)); while(!Character.isAlphabetic(cleanInput.charAt(lastLetterLocation))){
--lastLetterLocation;
}
if(cleanInput.charAt(lastLetterLocation) == doubled){
cleanInput.append(replacer);
}
else{
cleanInput.append(doubled);
}
} }
//Get the second letter this.inputString = cleanInput.toString();
char secondLetter = '\0'; }
if(inputCnt != inputString.length()){ //If this is decoding just add it without parsing it
secondLetter = inputString.charAt(inputCnt++); else{
} //Throw an exception if replaced is included
else{ if(inputString.contains(Character.toString(replaced))){
secondLetter = doubled; throw new InvalidCharacterException("An encoded message cannot contain a letter that needs replaced");
} }
//Make sure the second letter doesn't need replaced
if(secondLetter == replaced){ this.inputString = inputString;
secondLetter = replacer;
}
if(secondLetter == firstLetter){
secondLetter = doubled;
--inputCnt;
}
//Add the letters to the sanitized string
sanitizedInput.append(firstLetter);
sanitizedInput.append(inBetween.toString());
sanitizedInput.append(secondLetter);
} }
this.inputString = sanitizedInput.toString();
} }
//Returns the inputs string ready for encoding //Returns the inputs string ready for encoding
private String getPreparedInputString(){ private String getPreparedInputString(){
@@ -134,72 +184,163 @@ public class Playfair{
//Create the grid from the sanitized keyword //Create the grid from the sanitized keyword
createGrid(); createGrid();
} }
//Returns the location of the given character in the grid
private CharLocation findChar(char letter) throws Exception{
for(int row = 0;row < grid.length;++row){
for(int col = 0;col < grid[row].length;++col){
if(grid[row][col] == letter){
return new CharLocation(row, col);
}
}
}
//If it was not found something went wrong
throw new Exception("That character was not found in the grid. ERROR");
}
//Returns the location in the grid of x and y, adjusting for out of bounds
private char getGridChar(int x, int y){
if(x < 0){
x += 5;
}
if(y < 0){
y += 5;
}
return grid[x % 5][y % 5];
}
//Adds characters that aren't letters to the output
private void addCharactersToCleanString(String cleanString){
int outputCnt = 0;
StringBuilder fullOutput = new StringBuilder();
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
if(Character.isUpperCase(inputString.charAt(inputCnt))){
fullOutput.append(cleanString.charAt(outputCnt++));
}
else if(Character.isLowerCase(inputString.charAt(inputCnt))){
fullOutput.append(Character.toLowerCase(cleanString.charAt(outputCnt++)));
}
else{
fullOutput.append(inputString.charAt(inputCnt));
}
}
outputString = fullOutput.toString();
}
//Encoded inputString using the Playfair cipher and stores the result in outputString //Encoded inputString using the Playfair cipher and stores the result in outputString
private String encode(){ private String encode() throws Exception{
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
int inputCnt = 0; int inputCnt = 0;
String cleanString = getPreparedInputString(); String cleanString = getPreparedInputString();
while(inputCnt < inputString.length()){ while(inputCnt < cleanString.length()){
//Get the next 2 letters to be encoded //Get the next 2 letters to be encoded
char firstLetter = inputString.charAt(inputCnt++); char firstLetter = cleanString.charAt(inputCnt++);
char secondLetter = '\0'; char secondLetter = cleanString.charAt(inputCnt++);
StringBuilder inBetween = new StringBuilder();
//TODO:
//Find the letters in the grid //Find the letters in the grid
CharLocation firstLocation = findChar(firstLetter);
CharLocation secondLocation = findChar(secondLetter);
//Encode the letters //Encode the letters
if(firstLocation.getX() == secondLocation.getX()){
firstLetter = getGridChar(firstLocation.getX(), firstLocation.getY() + 1);
secondLetter = getGridChar(secondLocation.getX(), secondLocation.getY() + 1);
}
else if(firstLocation.getY() == secondLocation.getY()){
firstLetter = getGridChar(firstLocation.getX() + 1, firstLocation.getY());
secondLetter = getGridChar(secondLocation.getX() + 1, secondLocation.getY());
}
else{
firstLetter = getGridChar(firstLocation.getX(), secondLocation.getY());
secondLetter = getGridChar(secondLocation.getX(), firstLocation.getY());
}
//Add the new letters to the output string //Add the new letters to the output string
output.append(firstLetter);
output.append(secondLetter);
} }
//Add other characters to the output string
addCharactersToCleanString(output.toString());
//Return the output string //Return the output string
return outputString;
} }
//Decodes inputString using the Playfair cipher and stores the result in outputString //Decodes inputString using the Playfair cipher and stores the result in outputString
private String decode(){ private String decode() throws Exception{
//TODO: StringBuilder output = new StringBuilder();
return null; int inputCnt = 0;
String cleanString = getPreparedInputString();
while(inputCnt < cleanString.length()){
//Get the next 2 letters to be encoded
char firstLetter = cleanString.charAt(inputCnt++);
char secondLetter = cleanString.charAt(inputCnt++);
//Find the letters in the grid
CharLocation firstLocation = findChar(firstLetter);
CharLocation secondLocation = findChar(secondLetter);
//Decode the letters
if(firstLocation.getX() == secondLocation.getX()){
firstLetter = getGridChar(firstLocation.getX(), firstLocation.getY() - 1);
secondLetter = getGridChar(secondLocation.getX(), secondLocation.getY() - 1);
}
else if(firstLocation.getY() == secondLocation.getY()){
firstLetter = getGridChar(firstLocation.getX() - 1, firstLocation.getY());
secondLetter = getGridChar(secondLocation.getX() - 1, secondLocation.getY());
}
else{
firstLetter = getGridChar(firstLocation.getX(), secondLocation.getY());
secondLetter = getGridChar(secondLocation.getX(), firstLocation.getY());
}
//Add the new letters to the output string
output.append(firstLetter);
output.append(secondLetter);
}
//Add other characters to the output string
addCharactersToCleanString(output.toString());
//Return the output string
return outputString;
} }
public Playfair(){ public Playfair() throws InvalidCharacterException{
reset(); reset();
replaced = 'J';
replacer = 'I';
doubled = 'X';
leaveCapitals = false; leaveCapitals = false;
leaveWhitespace = false; leaveWhitespace = false;
leaveSymbols = false; leaveSymbols = false;
setReplaced('J');
setReplacer('I');
setDoubled('X');
} }
public Playfair(boolean leaveCapitals, boolean leaveWhitespace, boolean leaveSymbols){ public Playfair(boolean leaveCapitals, boolean leaveWhitespace, boolean leaveSymbols) throws InvalidCharacterException{
reset(); reset();
replaced = 'J';
replacer = 'I';
doubled = 'X';
this.leaveCapitals = leaveCapitals; this.leaveCapitals = leaveCapitals;
this.leaveWhitespace = leaveWhitespace; this.leaveWhitespace = leaveWhitespace;
this.leaveSymbols = leaveSymbols; this.leaveSymbols = leaveSymbols;
setReplaced('J');
setReplacer('I');
setDoubled('X');
} }
public Playfair(boolean leaveCapitals, boolean leaveWhitespace, boolean leaveSymbols, char replaced, char replacer, char doubled) throws InvalidCharacterException{ public Playfair(boolean leaveCapitals, boolean leaveWhitespace, boolean leaveSymbols, char replaced, char replacer, char doubled) throws InvalidCharacterException{
reset(); reset();
setReplaced(replaced);
setReplacer(replacer);
setDoubled(doubled);
this.leaveCapitals = leaveCapitals; this.leaveCapitals = leaveCapitals;
this.leaveWhitespace = leaveWhitespace; this.leaveWhitespace = leaveWhitespace;
this.leaveSymbols = leaveSymbols; this.leaveSymbols = leaveSymbols;
setReplaced(replaced);
setReplacer(replacer);
setDoubled(doubled);
} }
//Sets the keyword and inputString and encodes the message //Sets the keyword and inputString and encodes the message
public String encode(String keyword, String input) throws InvalidCharacterException{ public String encode(String keyword, String input) throws InvalidCharacterException, Exception{
reset(); reset();
setKeyword(keyword); setKeyword(keyword);
setInputString(input); setInputString(input, true);
return encode(); return encode();
} }
//Sets the keyword and inputString and decodes the message //Sets the keyword and inputString and decodes the message
public String decode(String keyword, String input) throws InvalidCharacterException{ public String decode(String keyword, String input) throws InvalidCharacterException, Exception{
reset(); reset();
setKeyword(keyword); setKeyword(keyword);
setInputString(input); setInputString(input, false);
return decode(); return decode();
} }
@@ -224,9 +365,7 @@ public class Playfair{
throw new InvalidCharacterException("The replaced letter cannot be the same as the replacing letter"); throw new InvalidCharacterException("The replaced letter cannot be the same as the replacing letter");
} }
if((Character.isAlphabetic(replaced)) && (replaced != replacer)){ this.replaced = Character.toUpperCase(replaced);
this.replaced = Character.toUpperCase(replaced);
}
} }
public char getReplacer(){ public char getReplacer(){
return replacer; return replacer;

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/TestPlayfair.java //CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/TestPlayfair.java
//Matthew Ellison //Matthew Ellison
// Created: 07-30-21 // Created: 07-30-21
//Modified: 07-30-21 //Modified: 01-04-22
//These are the tests for the Playfair class //These are the tests for the Playfair class
package mattrixwv.CipherStreamJava; package mattrixwv.CipherStreamJava;
@@ -10,18 +10,508 @@ import static org.junit.Assert.assertEquals;
import org.junit.Test; import org.junit.Test;
import mattrixwv.CipherStreamJava.Playfair.InvalidCharacterException;
public class TestPlayfair{ public class TestPlayfair{
@Test @Test
public void testDecode(){ public void testDecode() throws InvalidCharacterException, Exception{
Playfair cipher = new Playfair(); Playfair cipher = new Playfair(true, true, true);
//Test 1 //Test lowercase decoding
String inputString = "bmodzbxdnabekudmuixmmouvif";
String keyword = "Playfair Example";
String correctOutput = "hidethegoldinthetrexestump";
String output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed lowercase decoding.", correctOutput, output);
//Test uppercase decoding
inputString = "BMODZBXDNABEKUDMUIXMMOUVIF";
keyword = "Playfair Example";
correctOutput = "HIDETHEGOLDINTHETREXESTUMP";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed uppercase decoding.", correctOutput, output);
//Test odd letter count decoding
inputString = "bmodzbxdnabekudmuixmmouvim";
keyword = "Playfair Example";
correctOutput = "hidethegoldinthetrexestumx";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed odd letter count decoding.", correctOutput, output);
//Test whitespace decoding
inputString = "bmod zbx dnab ek udm uixmm ouvif";
keyword = "Playfair Example";
correctOutput = "hide the gold in the trexe stump";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed whitespace decoding.", correctOutput, output);
//Test symbol decoding
inputString = "bmodzbxdnabek-udm@uixmm+ouvif";
keyword = "Playfair Example";
correctOutput = "hidethegoldin-the@trexe+stump";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed symbol decoding.", correctOutput, output);
//Test mixed case, whtiespace, symbol decoding
inputString = "Bmod zbx dnab ek - udm@uixmm+ouvif";
keyword = "Playfair Example";
correctOutput = "Hide the gold in - the@trexe+stump";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed mixed case, whitespace, symbol decoding.", correctOutput, output);
//Test mixed case, whtiespace, symbol decoding with mangled keyword
inputString = "Bmod zbx dnab ek - udm@uixmm+ouvif";
keyword = "Play-fair@Exam ple";
correctOutput = "Hide the gold in - the@trexe+stump";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed mixed case, whitespace, symbol decoding with mangled keyword.", correctOutput, output);
} }
@Test @Test
public void testEncode(){ public void testNoWhitespaceDecode() throws InvalidCharacterException, Exception{
Playfair cipher = new Playfair(); Playfair cipher = new Playfair(true, false, true);
//Test 1 //Test lowercase decoding
String inputString = "bmodzbxdnabekudmuixmmouvif";
String keyword = "Playfair Example";
String correctOutput = "hidethegoldinthetrexestump";
String output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed no whitespace lowercase decoding.", correctOutput, output);
//Test uppercase decoding
inputString = "BMODZBXDNABEKUDMUIXMMOUVIF";
keyword = "Playfair Example";
correctOutput = "HIDETHEGOLDINTHETREXESTUMP";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed no whitespace uppercase decoding.", correctOutput, output);
//Test odd letter count decoding
inputString = "bmodzbxdnabekudmuixmmouvim";
keyword = "Playfair Example";
correctOutput = "hidethegoldinthetrexestumx";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed no whitespace odd letter count decoding.", correctOutput, output);
//Test whitespace decoding
inputString = "bmodzbxdnabekudmuixmmouvif";
keyword = "Playfair Example";
correctOutput = "hidethegoldinthetrexestump";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed no whitespace whitespace decoding.", correctOutput, output);
//Test symbol decoding
inputString = "bmodzbxdnabek-udm@uixmm+ouvif";
keyword = "Playfair Example";
correctOutput = "hidethegoldin-the@trexe+stump";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed no whitespace symbol decoding.", correctOutput, output);
//Test mixed case, whtiespace, symbol decoding
inputString = "Bmodzbxdnabek-udm@uixmm+ouvif";
keyword = "Playfair Example";
correctOutput = "Hidethegoldin-the@trexe+stump";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed no whitespace mixed case, whitespace, symbol decoding.", correctOutput, output);
//Test mixed case, whtiespace, symbol decoding with mangled keyword
inputString = "Bmod zbx dnab ek - udm@uixmm+ouvif";
keyword = "Play-fair@Exam ple";
correctOutput = "Hidethegoldin-the@trexe+stump";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed no whitespace mixed case, whitespace, symbol decoding with mangled keyword.", correctOutput, output);
}
@Test
public void testNoCapitalDecode() throws InvalidCharacterException, Exception{
Playfair cipher = new Playfair(false, true, true);
//Test lowercase decoding
String inputString = "bmodzbxdnabekudmuixmmouvif";
String keyword = "Playfair Example";
String correctOutput = "HIDETHEGOLDINTHETREXESTUMP";
String output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed no capital lowercase decoding.", correctOutput, output);
//Test uppercase decoding
inputString = "BMODZBXDNABEKUDMUIXMMOUVIF";
keyword = "Playfair Example";
correctOutput = "HIDETHEGOLDINTHETREXESTUMP";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed no capital uppercase decoding.", correctOutput, output);
//Test odd letter count decoding
inputString = "bmodzbxdnabekudmuixmmouvim";
keyword = "Playfair Example";
correctOutput = "HIDETHEGOLDINTHETREXESTUMX";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed no capital odd letter count decoding.", correctOutput, output);
//Test whitespace decoding
inputString = "bmod zbx dnab ek udm uixmm ouvif";
keyword = "Playfair Example";
correctOutput = "HIDE THE GOLD IN THE TREXE STUMP";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed no capital whitespace decoding.", correctOutput, output);
//Test symbol decoding
inputString = "bmodzbxdnabek-udm@uixmm+ouvif";
keyword = "Playfair Example";
correctOutput = "HIDETHEGOLDIN-THE@TREXE+STUMP";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed no capital symbol decoding.", correctOutput, output);
//Test mixed case, whtiespace, symbol decoding
inputString = "Bmod zbx dnab ek - udm@uixmm+ouvif";
keyword = "Playfair Example";
correctOutput = "HIDE THE GOLD IN - THE@TREXE+STUMP";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed no capital mixed case, whitespace, symbol decoding.", correctOutput, output);
//Test mixed case, whtiespace, symbol decoding with mangled keyword
inputString = "Bmod zbx dnab ek - udm@uixmm+ouvif";
keyword = "Play-fair@Exam ple";
correctOutput = "HIDE THE GOLD IN - THE@TREXE+STUMP";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed no capital mixed case, whitespace, symbol decoding with mangled keyword.", correctOutput, output);
}
@Test
public void testNoSymbolDecode() throws InvalidCharacterException, Exception{
Playfair cipher = new Playfair(true, true, false);
//Test lowercase decoding
String inputString = "bmodzbxdnabekudmuixmmouvif";
String keyword = "Playfair Example";
String correctOutput = "hidethegoldinthetrexestump";
String output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed no symbol lowercase decoding.", correctOutput, output);
//Test uppercase decoding
inputString = "BMODZBXDNABEKUDMUIXMMOUVIF";
keyword = "Playfair Example";
correctOutput = "HIDETHEGOLDINTHETREXESTUMP";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed no symbol uppercase decoding.", correctOutput, output);
//Test odd letter count decoding
inputString = "bmodzbxdnabekudmuixmmouvim";
keyword = "Playfair Example";
correctOutput = "hidethegoldinthetrexestumx";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed no symbol odd letter count decoding.", correctOutput, output);
//Test whitespace decoding
inputString = "bmod zbx dnab ek udm uixmm ouvif";
keyword = "Playfair Example";
correctOutput = "hide the gold in the trexe stump";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed no symbol whitespace decoding.", correctOutput, output);
//Test symbol decoding
inputString = "bmodzbxdnabek-udm@uixmm+ouvif";
keyword = "Playfair Example";
correctOutput = "hidethegoldinthetrexestump";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed no symbol symbol decoding.", correctOutput, output);
//Test mixed case, whtiespace, symbol decoding
inputString = "Bmod zbx dnab ek - udm@uixmm+ouvif";
keyword = "Playfair Example";
correctOutput = "Hide the gold in thetrexestump";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed no symbol mixed case, whitespace, symbol decoding.", correctOutput, output);
//Test mixed case, whtiespace, symbol decoding with mangled keyword
inputString = "Bmod zbx dnab ek - udm@uixmm+ouvif";
keyword = "Play-fair@Exam ple";
correctOutput = "Hide the gold in thetrexestump";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed no symbol mixed case, whitespace, symbol decoding with mangled keyword.", correctOutput, output);
}
@Test
public void testNoCapitalWhitespaceSymbolDecode() throws InvalidCharacterException, Exception{
Playfair cipher = new Playfair(false, false, false);
//Test lowercase decoding
String inputString = "bmodzbxdnabekudmuixmmouvif";
String keyword = "Playfair Example";
String correctOutput = "HIDETHEGOLDINTHETREXESTUMP";
String output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed secure lowercase decoding.", correctOutput, output);
//Test uppercase decoding
inputString = "BMODZBXDNABEKUDMUIXMMOUVIF";
keyword = "Playfair Example";
correctOutput = "HIDETHEGOLDINTHETREXESTUMP";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed secure uppercase decoding.", correctOutput, output);
//Test odd letter count decoding
inputString = "bmodzbxdnabekudmuixmmouvim";
keyword = "Playfair Example";
correctOutput = "HIDETHEGOLDINTHETREXESTUMX";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed secure odd letter count decoding.", correctOutput, output);
//Test whitespace decoding
inputString = "bmod zbx dnab ek udm uixmm ouvif";
keyword = "Playfair Example";
correctOutput = "HIDETHEGOLDINTHETREXESTUMP";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed secure whitespace decoding.", correctOutput, output);
//Test symbol decoding
inputString = "bmodzbxdnabek-udm@uixmm+ouvif";
keyword = "Playfair Example";
correctOutput = "HIDETHEGOLDINTHETREXESTUMP";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed secure symbol decoding.", correctOutput, output);
//Test mixed case, whtiespace, symbol decoding
inputString = "Bmod zbx dnab ek - udm@uixmm+ouvif";
keyword = "Playfair Example";
correctOutput = "HIDETHEGOLDINTHETREXESTUMP";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed secure mixed case, whitespace, symbol decoding.", correctOutput, output);
//Test mixed case, whtiespace, symbol decoding with mangled keyword
inputString = "Bmod zbx dnab ek - udm@uixmm+ouvif";
keyword = "Play-fair@Exam ple";
correctOutput = "HIDETHEGOLDINTHETREXESTUMP";
output = cipher.decode(keyword, inputString);
assertEquals("Playfair failed secure mixed case, whitespace, symbol decoding with mangled keyword.", correctOutput, output);
}
@Test
public void testEncode() throws InvalidCharacterException, Exception{
Playfair cipher = new Playfair(true, true, true);
//Test lowercase encoding
String inputString = "hidethegoldinthetrexestump";
String keyword = "Playfair Example";
String correctOutput = "bmodzbxdnabekudmuixmmouvif";
String output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed lowercase encoding.", correctOutput, output);
//Test uppercase encoding
inputString = "HIDETHEGOLDINTHETREXESTUMP";
keyword = "Playfair Example";
correctOutput = "BMODZBXDNABEKUDMUIXMMOUVIF";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed uppercase encoding.", correctOutput, output);
//Test odd letter count encoding
inputString = "hidethegoldinthetrexestum";
keyword = "Playfair Example";
correctOutput = "bmodzbxdnabekudmuixmmouviM";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed odd letter count encoding.", correctOutput, output);
//Test whitespace encoding
inputString = "hide the gold in the trexe stump";
keyword = "Playfair Example";
correctOutput = "bmod zbx dnab ek udm uixmm ouvif";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed whitespace encoding.", correctOutput, output);
//Test symbol encoding
inputString = "hidethegoldin-the@trexe+stump";
keyword = "Playfair Example";
correctOutput = "bmodzbxdnabek-udm@uixmm+ouvif";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed symbol encoding.", correctOutput, output);
//Test mixed case, whitespace, symbol encoding
inputString = "Hide the gold in - the@tree+stump";
keyword = "Playfair Example";
correctOutput = "Bmod zbx dnab ek - udm@uixMm+ouvif";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed mixed case, whitespace, symbol encoding.", correctOutput, output);
//Test mixed case, whitespace, symbol encoding with mangled keyword
inputString = "Hide the gold in - the@tree+stump";
keyword = "Play-fair@Exam ple";
correctOutput = "Bmod zbx dnab ek - udm@uixMm+ouvif";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed mixed case, whitespace, symbol encoding with mangled keyword.", correctOutput, output);
}
@Test
public void testNoWhitespaceEncode() throws InvalidCharacterException, Exception{
Playfair cipher = new Playfair(true, false, true);
//Test lowercase encoding
String inputString = "hidethegoldinthetrexestump";
String keyword = "Playfair Example";
String correctOutput = "bmodzbxdnabekudmuixmmouvif";
String output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed no whitespace lowercase encoding.", correctOutput, output);
//Test uppercase encoding
inputString = "HIDETHEGOLDINTHETREXESTUMP";
keyword = "Playfair Example";
correctOutput = "BMODZBXDNABEKUDMUIXMMOUVIF";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed no whitespace uppercase encoding.", correctOutput, output);
//Test odd letter count encoding
inputString = "hidethegoldinthetrexestum";
keyword = "Playfair Example";
correctOutput = "bmodzbxdnabekudmuixmmouviM";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed no whitespace odd letter count encoding.", correctOutput, output);
//Test whitespace encoding
inputString = "hide the gold in the trexe stump";
keyword = "Playfair Example";
correctOutput = "bmodzbxdnabekudmuixmmouvif";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed no whitespace whitespace encoding.", correctOutput, output);
//Test symbol encoding
inputString = "hidethegoldin-the@trexe+stump";
keyword = "Playfair Example";
correctOutput = "bmodzbxdnabek-udm@uixmm+ouvif";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed no whitespace symbol encoding.", correctOutput, output);
//Test mixed case, whitespace, symbol encoding
inputString = "Hide the gold in - the@tree+stump";
keyword = "Playfair Example";
correctOutput = "Bmodzbxdnabek-udm@uixMm+ouvif";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed no whitespace mixed case, whitespace, symbol encoding.", correctOutput, output);
//Test mixed case, whitespace, symbol encoding with mangled keyword
inputString = "Hide the gold in - the@tree+stump";
keyword = "Play-fair@Exam ple";
correctOutput = "Bmodzbxdnabek-udm@uixMm+ouvif";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed no whitespace mixed case, whitespace, symbol encoding with mangled keyword.", correctOutput, output);
}
@Test
public void testNoCapitalEncode() throws InvalidCharacterException, Exception{
Playfair cipher = new Playfair(false, true, true);
//Test lowercase encoding
String inputString = "hidethegoldinthetrexestump";
String keyword = "Playfair Example";
String correctOutput = "BMODZBXDNABEKUDMUIXMMOUVIF";
String output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed no capital lowercase encoding.", correctOutput, output);
//Test uppercase encoding
inputString = "HIDETHEGOLDINTHETREXESTUMP";
keyword = "Playfair Example";
correctOutput = "BMODZBXDNABEKUDMUIXMMOUVIF";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed no capital uppercase encoding.", correctOutput, output);
//Test odd letter count encoding
inputString = "hidethegoldinthetrexestum";
keyword = "Playfair Example";
correctOutput = "BMODZBXDNABEKUDMUIXMMOUVIM";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed no capital odd letter count encoding.", correctOutput, output);
//Test whitespace encoding
inputString = "hide the gold in the trexe stump";
keyword = "Playfair Example";
correctOutput = "BMOD ZBX DNAB EK UDM UIXMM OUVIF";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed no capital whitespace encoding.", correctOutput, output);
//Test symbol encoding
inputString = "hidethegoldin-the@trexe+stump";
keyword = "Playfair Example";
correctOutput = "BMODZBXDNABEK-UDM@UIXMM+OUVIF";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed no capital symbol encoding.", correctOutput, output);
//Test mixed case, whitespace, symbol encoding
inputString = "Hide the gold in - the@tree+stump";
keyword = "Playfair Example";
correctOutput = "BMOD ZBX DNAB EK - UDM@UIXMM+OUVIF";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed no capital mixed case, whitespace, symbol encoding.", correctOutput, output);
//Test mixed case, whitespace, symbol encoding with mangled keyword
inputString = "Hide the gold in - the@tree+stump";
keyword = "Play-fair@Exam ple";
correctOutput = "BMOD ZBX DNAB EK - UDM@UIXMM+OUVIF";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed no capital mixed case, whitespace, symbol encoding with mangled keyword.", correctOutput, output);
}
@Test
public void testNoSymbolEncode() throws InvalidCharacterException, Exception{
Playfair cipher = new Playfair(true, true, false);
//Test lowercase encoding
String inputString = "hidethegoldinthetrexestump";
String keyword = "Playfair Example";
String correctOutput = "bmodzbxdnabekudmuixmmouvif";
String output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed no symbol lowercase encoding.", correctOutput, output);
//Test uppercase encoding
inputString = "HIDETHEGOLDINTHETREXESTUMP";
keyword = "Playfair Example";
correctOutput = "BMODZBXDNABEKUDMUIXMMOUVIF";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed no symbol uppercase encoding.", correctOutput, output);
//Test odd letter count encoding
inputString = "hidethegoldinthetrexestum";
keyword = "Playfair Example";
correctOutput = "bmodzbxdnabekudmuixmmouviM";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed no symbol odd letter count encoding.", correctOutput, output);
//Test whitespace encoding
inputString = "hide the gold in the trexe stump";
keyword = "Playfair Example";
correctOutput = "bmod zbx dnab ek udm uixmm ouvif";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed no symbol whitespace encoding.", correctOutput, output);
//Test symbol encoding
inputString = "hidethegoldin-the@trexe+stump";
keyword = "Playfair Example";
correctOutput = "bmodzbxdnabekudmuixmmouvif";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed no symbol symbol encoding.", correctOutput, output);
//Test mixed case, whitespace, symbol encoding
inputString = "Hide the gold in - the@tree+stump";
keyword = "Playfair Example";
correctOutput = "Bmod zbx dnab ek udmuixMmouvif";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed no symbol mixed case, whitespace, symbol encoding.", correctOutput, output);
//Test mixed case, whitespace, symbol encoding with mangled keyword
inputString = "Hide the gold in - the@tree+stump";
keyword = "Play-fair@Exam ple";
correctOutput = "Bmod zbx dnab ek udmuixMmouvif";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed no symbol mixed case, whitespace, symbol encoding with mangled keyword.", correctOutput, output);
}
@Test
public void testNoCapitalWhitespaceSymbolEncode() throws InvalidCharacterException, Exception{
Playfair cipher = new Playfair(false, false, false);
//Test lowercase encoding
String inputString = "hidethegoldinthetrexestump";
String keyword = "Playfair Example";
String correctOutput = "BMODZBXDNABEKUDMUIXMMOUVIF";
String output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed secure lowercase encoding.", correctOutput, output);
//Test uppercase encoding
inputString = "HIDETHEGOLDINTHETREXESTUMP";
keyword = "Playfair Example";
correctOutput = "BMODZBXDNABEKUDMUIXMMOUVIF";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed secure uppercase encoding.", correctOutput, output);
//Test odd letter count encoding
inputString = "hidethegoldinthetrexestum";
keyword = "Playfair Example";
correctOutput = "BMODZBXDNABEKUDMUIXMMOUVIM";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed secure odd letter count encoding.", correctOutput, output);
//Test whitespace encoding
inputString = "hide the gold in the trexe stump";
keyword = "Playfair Example";
correctOutput = "BMODZBXDNABEKUDMUIXMMOUVIF";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed secure whitespace encoding.", correctOutput, output);
//Test symbol encoding
inputString = "hidethegoldin-the@trexe+stump";
keyword = "Playfair Example";
correctOutput = "BMODZBXDNABEKUDMUIXMMOUVIF";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed secure symbol encoding.", correctOutput, output);
//Test mixed case, whitespace, symbol encoding
inputString = "Hide the gold in - the@tree+stump";
keyword = "Playfair Example";
correctOutput = "BMODZBXDNABEKUDMUIXMMOUVIF";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed secure mixed case, whitespace, symbol encoding.", correctOutput, output);
//Test mixed case, whitespace, symbol encoding with mangled keyword
inputString = "Hide the gold in - the@tree+stump";
keyword = "Play-fair@Exam ple";
correctOutput = "BMODZBXDNABEKUDMUIXMMOUVIF";
output = cipher.encode(keyword, inputString);
assertEquals("Playfair failed secure mixed case, whitespace, symbol encoding with mangled keyword.", correctOutput, output);
} }
} }