Added encoding options
This commit is contained in:
@@ -1,18 +1,20 @@
|
|||||||
//CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/Vigenere.java
|
//CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/Vigenere.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 07-25-21
|
// Created: 07-25-21
|
||||||
//Modified: 07-25-21
|
//Modified: 12-30-21
|
||||||
//This is the declaration of the Vigenere class
|
//This is the declaration of the Vigenere class
|
||||||
package mattrixwv.CipherStreamJava;
|
package mattrixwv.CipherStreamJava;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class Vigenere{
|
public class Vigenere{
|
||||||
public static final String version = "1.0"; //The current library's version number
|
|
||||||
protected String inputString; //This is the string that needs encoded/decoded
|
protected String inputString; //This is the string that needs encoded/decoded
|
||||||
protected String outputString; //This is the string that is output after encoding/decoding
|
protected String outputString; //This is the string that is output after encoding/decoding
|
||||||
protected String keyword; //This is the keyword that is resposible for determining the offsets that you change each character by
|
protected String keyword; //This is the keyword that is resposible for determining the offsets that you change each character by
|
||||||
protected ArrayList<Integer> offset; //Holds the offsets coputed from each character in the keyword
|
protected ArrayList<Integer> offset; //Holds the offsets coputed from each character in the keyword
|
||||||
|
protected boolean leaveCapitals; //Whether to respect capitals in the output string
|
||||||
|
protected boolean leaveWhitespace; //Whether to respect whitespace in the output string
|
||||||
|
protected boolean leaveSymbols; //Whether to respect symbols in the output string
|
||||||
|
|
||||||
//Uses keyword to calculate the offset for the Caesar cipher for each character
|
//Uses keyword to calculate the offset for the Caesar cipher for each character
|
||||||
protected void setOffset(){
|
protected void setOffset(){
|
||||||
@@ -26,13 +28,17 @@ public class Vigenere{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Sets inputString
|
//Sets inputString
|
||||||
protected void setInputString(String input){
|
protected void setInputString(String inputString){
|
||||||
//Convert all letters to uppercase
|
if(!leaveCapitals){
|
||||||
input = input.toUpperCase();
|
inputString = inputString.toLowerCase();
|
||||||
//Remove all characters except capital letters
|
}
|
||||||
input = input.replaceAll("[^A-Z]", "");
|
if(!leaveWhitespace){
|
||||||
//Save the string
|
inputString = inputString.replaceAll("\\s+", "");
|
||||||
inputString = input;
|
}
|
||||||
|
if(!leaveSymbols){
|
||||||
|
inputString = inputString.replaceAll("[^a-zA-Z0-9\\s+]", "");
|
||||||
|
}
|
||||||
|
this.inputString = inputString;
|
||||||
}
|
}
|
||||||
//Sets keyword
|
//Sets keyword
|
||||||
protected void setKeyword(String key) throws Exception{
|
protected void setKeyword(String key) throws Exception{
|
||||||
@@ -56,14 +62,28 @@ public class Vigenere{
|
|||||||
StringBuilder output = new StringBuilder();
|
StringBuilder output = new StringBuilder();
|
||||||
|
|
||||||
//Step through every character in the inputString and advance it the correct amount, according to offset
|
//Step through every character in the inputString and advance it the correct amount, according to offset
|
||||||
for(int cnt = 0;cnt < inputString.length();++cnt){
|
int offsetCnt = 0;
|
||||||
char letter = (char)(inputString.charAt(cnt) + offset.get(cnt % offset.size()));
|
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
|
||||||
//Make sure the character is still a letter, if not, wrap around
|
char letter = inputString.charAt(inputCnt);
|
||||||
if(letter < 'A'){
|
if(Character.isUpperCase(letter)){
|
||||||
letter += 26;
|
letter += offset.get((offsetCnt++) % offset.size());
|
||||||
|
//Make sure the character is still a letter, if not, wrap around
|
||||||
|
if(letter < 'A'){
|
||||||
|
letter += 26;
|
||||||
|
}
|
||||||
|
else if(letter > 'Z'){
|
||||||
|
letter -= 26;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if(letter > 'Z'){
|
else if(Character.isLowerCase(letter)){
|
||||||
letter -= 26;
|
letter += offset.get((offsetCnt++) % offset.size());
|
||||||
|
//Make sure the character is still a letter, if not, wrap around
|
||||||
|
if(letter < 'a'){
|
||||||
|
letter += 26;
|
||||||
|
}
|
||||||
|
else if(letter > 'z'){
|
||||||
|
letter -= 26;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
output.append(letter);
|
output.append(letter);
|
||||||
}
|
}
|
||||||
@@ -76,13 +96,26 @@ public class Vigenere{
|
|||||||
StringBuilder output = new StringBuilder();
|
StringBuilder output = new StringBuilder();
|
||||||
|
|
||||||
//Step through every character in the inputString and advance it the correct amount, according to offset
|
//Step through every character in the inputString and advance it the correct amount, according to offset
|
||||||
for(int cnt = 0;cnt < inputString.length();++cnt){
|
int offsetCnt = 0;
|
||||||
char letter = (char)(inputString.charAt(cnt) - offset.get(cnt % offset.size()));
|
for(int letterCnt = 0;letterCnt < inputString.length();++letterCnt){
|
||||||
if(letter < 'A'){
|
char letter = inputString.charAt(letterCnt);
|
||||||
letter += 26;
|
if(Character.isUpperCase(letter)){
|
||||||
|
letter -= offset.get((offsetCnt++) % offset.size());
|
||||||
|
if(letter < 'A'){
|
||||||
|
letter += 26;
|
||||||
|
}
|
||||||
|
else if(letter > 'Z'){
|
||||||
|
letter -= 26;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if(letter > 'Z'){
|
else if(Character.isLowerCase(letter)){
|
||||||
letter -= 26;
|
letter -= offset.get((offsetCnt++) % offset.size());
|
||||||
|
if(letter < 'a'){
|
||||||
|
letter += 26;
|
||||||
|
}
|
||||||
|
else if(letter > 'z'){
|
||||||
|
letter -= 26;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
output.append(letter);
|
output.append(letter);
|
||||||
}
|
}
|
||||||
@@ -96,6 +129,16 @@ public class Vigenere{
|
|||||||
public Vigenere(){
|
public Vigenere(){
|
||||||
offset = new ArrayList<Integer>();
|
offset = new ArrayList<Integer>();
|
||||||
reset();
|
reset();
|
||||||
|
leaveCapitals = false;
|
||||||
|
leaveWhitespace = false;
|
||||||
|
leaveSymbols = false;
|
||||||
|
}
|
||||||
|
public Vigenere(boolean leaveCapitals, boolean leaveWhitespace, boolean leaveSymbols){
|
||||||
|
offset = new ArrayList<Integer>();
|
||||||
|
reset();
|
||||||
|
this.leaveCapitals = leaveCapitals;
|
||||||
|
this.leaveWhitespace = leaveWhitespace;
|
||||||
|
this.leaveSymbols = leaveSymbols;
|
||||||
}
|
}
|
||||||
//Returns the current inputString
|
//Returns the current inputString
|
||||||
public String getInputString(){
|
public String getInputString(){
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/TestVigenere.java
|
//CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/TestVigenere.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 07-25-21
|
// Created: 07-25-21
|
||||||
//Modified: 07-25-21
|
//Modified: 12-30-21
|
||||||
//These are the tests for the Vigenere class
|
//These are the tests for the Vigenere class
|
||||||
package mattrixwv.CipherStreamJava;
|
package mattrixwv.CipherStreamJava;
|
||||||
|
|
||||||
@@ -14,52 +14,510 @@ import org.junit.Test;
|
|||||||
public class TestVigenere{
|
public class TestVigenere{
|
||||||
@Test
|
@Test
|
||||||
public void testDecode() throws Exception{
|
public void testDecode() throws Exception{
|
||||||
Vigenere cipher = new Vigenere();
|
Vigenere cipher = new Vigenere(true, true, true);
|
||||||
|
|
||||||
//Test 1
|
//Test lowercase decoding
|
||||||
String input = "xzb";
|
String input = "xzb";
|
||||||
String keyword = "xyz";
|
String keyword = "xyz";
|
||||||
String correctOutput = "ABC";
|
String correctOutput = "abc";
|
||||||
String output = cipher.decode(keyword, input);
|
String output = cipher.decode(keyword, input);
|
||||||
assertEquals("Vigenere Decoding failed the first test", correctOutput, output);
|
assertEquals("Vigenere failed lowercase decoding.", correctOutput, output);
|
||||||
|
//Test uppercase decoding
|
||||||
//Test 2
|
input = "XZB";
|
||||||
input = "LXFOPVEFRNHR";
|
keyword = "XYZ";
|
||||||
keyword = "LEMON";
|
correctOutput = "ABC";
|
||||||
correctOutput = "ATTACKATDAWN";
|
|
||||||
output = cipher.decode(keyword, input);
|
output = cipher.decode(keyword, input);
|
||||||
assertEquals("Vigenere Decoding failed the second test", correctOutput, output);
|
assertEquals("Vigenere failed uppercase decoding.", correctOutput, output);
|
||||||
|
|
||||||
//Test 3
|
|
||||||
input = "CSASTPKVSIQUTGQUCSASTPIUAQJB";
|
//Test whitespace decoding
|
||||||
keyword = "ABCD";
|
input = "x z b";
|
||||||
correctOutput = "CRYPTOISSHORTFORCRYPTOGRAPHY";
|
keyword = "xyz";
|
||||||
|
correctOutput = "a b c";
|
||||||
output = cipher.decode(keyword, input);
|
output = cipher.decode(keyword, input);
|
||||||
assertEquals("Vigenere Decoding failed the third test", correctOutput, output);
|
assertEquals("Vigenere failed whitespace decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test symbol decoding
|
||||||
|
input = "x@z^b";
|
||||||
|
keyword = "xyz";
|
||||||
|
correctOutput = "a@b^c";
|
||||||
|
output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed symbol decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test mixed case, whitespace, symbol decoding
|
||||||
|
input = "Lxfopv ef - rnhr";
|
||||||
|
keyword = "lemon";
|
||||||
|
correctOutput = "Attack at - dawn";
|
||||||
|
output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed mixed case, whitespace, symbol decoding.", correctOutput, output);
|
||||||
|
//Test mixed case, whitespace, symbol decoding with mangled keyword
|
||||||
|
input = "'Csastp' kv siqut gqu 'Csastpiuaqjb'";
|
||||||
|
keyword = " a@b C=d";
|
||||||
|
correctOutput = "'Crypto' is short for 'Cryptography'";
|
||||||
|
output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed mixed case, whitespace, symbol decoding with mangled keyword.", correctOutput, output);
|
||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
public void testEncode() throws Exception{
|
public void testNoWhitespaceDecode() throws Exception{
|
||||||
Vigenere cipher = new Vigenere();
|
Vigenere cipher = new Vigenere(true, false, true);
|
||||||
|
|
||||||
//Test 1
|
//Test lowercase decoding
|
||||||
|
String input = "xzb";
|
||||||
|
String keyword = "xyz";
|
||||||
|
String correctOutput = "abc";
|
||||||
|
String output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed lowercase decoding.", correctOutput, output);
|
||||||
|
//Test uppercase decoding
|
||||||
|
input = "XZB";
|
||||||
|
keyword = "XYZ";
|
||||||
|
correctOutput = "ABC";
|
||||||
|
output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed uppercase decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test whitespace decoding
|
||||||
|
input = "x z b";
|
||||||
|
keyword = "xyz";
|
||||||
|
correctOutput = "abc";
|
||||||
|
output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed whitespace decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test symbol decoding
|
||||||
|
input = "x@z^b";
|
||||||
|
keyword = "xyz";
|
||||||
|
correctOutput = "a@b^c";
|
||||||
|
output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed symbol decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test mixed case, whitespace, symbol decoding
|
||||||
|
input = "Lxfopv ef - rnhr";
|
||||||
|
keyword = "lemon";
|
||||||
|
correctOutput = "Attackat-dawn";
|
||||||
|
output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed mixed case, whitespace, symbol decoding.", correctOutput, output);
|
||||||
|
//Test mixed case, whitespace, symbol decoding with mangled keyword
|
||||||
|
input = "'Csastp' kv siqut gqu 'Csastpiuaqjb'";
|
||||||
|
keyword = " a@b C=d";
|
||||||
|
correctOutput = "'Crypto'isshortfor'Cryptography'";
|
||||||
|
output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed mixed case, whitespace, symbol decoding with mangled keyword.", correctOutput, output);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void testNoCapitalDecode() throws Exception{
|
||||||
|
Vigenere cipher = new Vigenere(false, true, true);
|
||||||
|
|
||||||
|
//Test lowercase decoding
|
||||||
|
String input = "xzb";
|
||||||
|
String keyword = "xyz";
|
||||||
|
String correctOutput = "abc";
|
||||||
|
String output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed lowercase decoding.", correctOutput, output);
|
||||||
|
//Test uppercase decoding
|
||||||
|
input = "XZB";
|
||||||
|
keyword = "XYZ";
|
||||||
|
correctOutput = "abc";
|
||||||
|
output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed uppercase decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test whitespace decoding
|
||||||
|
input = "x z b";
|
||||||
|
keyword = "xyz";
|
||||||
|
correctOutput = "a b c";
|
||||||
|
output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed whitespace decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test symbol decoding
|
||||||
|
input = "x@z^b";
|
||||||
|
keyword = "xyz";
|
||||||
|
correctOutput = "a@b^c";
|
||||||
|
output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed symbol decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test mixed case, whitespace, symbol decoding
|
||||||
|
input = "Lxfopv ef - rnhr";
|
||||||
|
keyword = "lemon";
|
||||||
|
correctOutput = "attack at - dawn";
|
||||||
|
output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed mixed case, whitespace, symbol decoding.", correctOutput, output);
|
||||||
|
//Test mixed case, whitespace, symbol decoding with mangled keyword
|
||||||
|
input = "'Csastp' kv siqut gqu 'Csastpiuaqjb'";
|
||||||
|
keyword = " a@b C=d";
|
||||||
|
correctOutput = "'crypto' is short for 'cryptography'";
|
||||||
|
output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed mixed case, whitespace, symbol decoding with mangled keyword.", correctOutput, output);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void testNoSymbolDecodes() throws Exception{
|
||||||
|
Vigenere cipher = new Vigenere(true, true, false);
|
||||||
|
|
||||||
|
//Test lowercase decoding
|
||||||
|
String input = "xzb";
|
||||||
|
String keyword = "xyz";
|
||||||
|
String correctOutput = "abc";
|
||||||
|
String output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed lowercase decoding.", correctOutput, output);
|
||||||
|
//Test uppercase decoding
|
||||||
|
input = "XZB";
|
||||||
|
keyword = "XYZ";
|
||||||
|
correctOutput = "ABC";
|
||||||
|
output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed uppercase decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test whitespace decoding
|
||||||
|
input = "x z b";
|
||||||
|
keyword = "xyz";
|
||||||
|
correctOutput = "a b c";
|
||||||
|
output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed whitespace decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test symbol decoding
|
||||||
|
input = "x@z^b";
|
||||||
|
keyword = "xyz";
|
||||||
|
correctOutput = "abc";
|
||||||
|
output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed symbol decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test mixed case, whitespace, symbol decoding
|
||||||
|
input = "Lxfopv ef - rnhr";
|
||||||
|
keyword = "lemon";
|
||||||
|
correctOutput = "Attack at dawn";
|
||||||
|
output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed mixed case, whitespace, symbol decoding.", correctOutput, output);
|
||||||
|
//Test mixed case, whitespace, symbol decoding with mangled keyword
|
||||||
|
input = "'Csastp' kv siqut gqu 'Csastpiuaqjb'";
|
||||||
|
keyword = " a@b C=d";
|
||||||
|
correctOutput = "Crypto is short for Cryptography";
|
||||||
|
output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed mixed case, whitespace, symbol decoding with mangled keyword.", correctOutput, output);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void testNoCapitalWhitespaceSymbolDecode() throws Exception{
|
||||||
|
Vigenere cipher = new Vigenere(false, false, false);
|
||||||
|
|
||||||
|
//Test lowercase decoding
|
||||||
|
String input = "xzb";
|
||||||
|
String keyword = "xyz";
|
||||||
|
String correctOutput = "abc";
|
||||||
|
String output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed lowercase decoding.", correctOutput, output);
|
||||||
|
//Test uppercase decoding
|
||||||
|
input = "XZB";
|
||||||
|
keyword = "XYZ";
|
||||||
|
correctOutput = "abc";
|
||||||
|
output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed uppercase decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test whitespace decoding
|
||||||
|
input = "x z b";
|
||||||
|
keyword = "xyz";
|
||||||
|
correctOutput = "abc";
|
||||||
|
output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed whitespace decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test symbol decoding
|
||||||
|
input = "x@z^b";
|
||||||
|
keyword = "xyz";
|
||||||
|
correctOutput = "abc";
|
||||||
|
output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed symbol decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test mixed case, whitespace, symbol decoding
|
||||||
|
input = "Lxfopv ef rnhr";
|
||||||
|
keyword = "lemon";
|
||||||
|
correctOutput = "attackatdawn";
|
||||||
|
output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed mixed case, whitespace, symbol decoding.", correctOutput, output);
|
||||||
|
//Test mixed case, whitespace, symbol decoding with mangled keyword
|
||||||
|
input = "'Csastp' kv siqut gqu 'Csastpiuaqjb'";
|
||||||
|
keyword = " a@b C=d";
|
||||||
|
correctOutput = "cryptoisshortforcryptography";
|
||||||
|
output = cipher.decode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed mixed case, whitespace, symbol decoding with mangled keyword.", correctOutput, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEncode() throws Exception{
|
||||||
|
Vigenere cipher = new Vigenere(true, true, true);
|
||||||
|
|
||||||
|
//Test lowercase encoding
|
||||||
String input = "abc";
|
String input = "abc";
|
||||||
String keyword = "xyz";
|
String keyword = "xyz";
|
||||||
String correctOutput = "XZB";
|
String correctOutput = "xzb";
|
||||||
String output = cipher.encode(keyword, input);
|
String output = cipher.encode(keyword, input);
|
||||||
assertEquals("Vigenere Encoding failed the first test", correctOutput, output);
|
assertEquals("Vigenere failed lowercase encoding.", correctOutput, output);
|
||||||
|
//Test upercase encoding
|
||||||
|
input = "ABC";
|
||||||
|
keyword = "xyz";
|
||||||
|
correctOutput = "XZB";
|
||||||
|
output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed uppercase encoding.", correctOutput, output);
|
||||||
|
|
||||||
//Test 2
|
|
||||||
|
//Test whitespace encoding
|
||||||
|
input = "a b c";
|
||||||
|
keyword = "xyz";
|
||||||
|
correctOutput = "x z b";
|
||||||
|
output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed whitespace encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test symbol decoding
|
||||||
|
input = "a@b^c";
|
||||||
|
keyword = "xyz";
|
||||||
|
correctOutput = "x@z^b";
|
||||||
|
output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed symbol encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test mixed case, whitespace, symbol decoding
|
||||||
input = "Attack at dawn";
|
input = "Attack at dawn";
|
||||||
keyword = "Lemon";
|
keyword = "lemon";
|
||||||
correctOutput = "LXFOPVEFRNHR";
|
correctOutput = "Lxfopv ef rnhr";
|
||||||
output = cipher.encode(keyword, input);
|
output = cipher.encode(keyword, input);
|
||||||
assertEquals("Vigenere Encoding failed the second test", correctOutput, output);
|
assertEquals("Vigenere failed mixed case, whitespace, symbol decoding.", correctOutput, output);
|
||||||
|
//Test mixed case, whitespace, symbol decoding test 2
|
||||||
//Test 3
|
|
||||||
input = "'Crypto' is short for 'Cryptography'";
|
input = "'Crypto' is short for 'Cryptography'";
|
||||||
keyword = "abcd";
|
keyword = " a@b C=d";
|
||||||
correctOutput = "CSASTPKVSIQUTGQUCSASTPIUAQJB";
|
correctOutput = "'Csastp' kv siqut gqu 'Csastpiuaqjb'";
|
||||||
output = cipher.encode(keyword, input);
|
output = cipher.encode(keyword, input);
|
||||||
assertEquals("Vigenere Encoding failed the third test", correctOutput, output);
|
assertEquals("Vigenere failed mixed case, whitespace, symbol encoding with mangled keyword.", correctOutput, output);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void testNoWhitespaceEncode() throws Exception{
|
||||||
|
Vigenere cipher = new Vigenere(true, false, true);
|
||||||
|
|
||||||
|
//Test lowercase encoding
|
||||||
|
String input = "abc";
|
||||||
|
String keyword = "xyz";
|
||||||
|
String correctOutput = "xzb";
|
||||||
|
String output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed lowercase encoding.", correctOutput, output);
|
||||||
|
//Test upercase encoding
|
||||||
|
input = "ABC";
|
||||||
|
keyword = "xyz";
|
||||||
|
correctOutput = "XZB";
|
||||||
|
output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed uppercase encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test whitespace encoding
|
||||||
|
input = "a b c";
|
||||||
|
keyword = "xyz";
|
||||||
|
correctOutput = "xzb";
|
||||||
|
output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed whitespace encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test symbol decoding
|
||||||
|
input = "a@b^c";
|
||||||
|
keyword = "xyz";
|
||||||
|
correctOutput = "x@z^b";
|
||||||
|
output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed symbol encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test mixed case, whitespace, symbol decoding
|
||||||
|
input = "Attack at dawn";
|
||||||
|
keyword = "lemon";
|
||||||
|
correctOutput = "Lxfopvefrnhr";
|
||||||
|
output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed mixed case, whitespace, symbol decoding.", correctOutput, output);
|
||||||
|
//Test mixed case, whitespace, symbol decoding test 2
|
||||||
|
input = "'Crypto' is short for 'Cryptography'";
|
||||||
|
keyword = " a@b C=d";
|
||||||
|
correctOutput = "'Csastp'kvsiqutgqu'Csastpiuaqjb'";
|
||||||
|
output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed mixed case, whitespace, symbol encoding with mangled keyword.", correctOutput, output);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void testNoCaptialEncode() throws Exception{
|
||||||
|
Vigenere cipher = new Vigenere(false, true, true);
|
||||||
|
|
||||||
|
//Test lowercase encoding
|
||||||
|
String input = "abc";
|
||||||
|
String keyword = "xyz";
|
||||||
|
String correctOutput = "xzb";
|
||||||
|
String output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed lowercase encoding.", correctOutput, output);
|
||||||
|
//Test upercase encoding
|
||||||
|
input = "ABC";
|
||||||
|
keyword = "xyz";
|
||||||
|
correctOutput = "xzb";
|
||||||
|
output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed uppercase encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test whitespace encoding
|
||||||
|
input = "a b c";
|
||||||
|
keyword = "xyz";
|
||||||
|
correctOutput = "x z b";
|
||||||
|
output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed whitespace encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test symbol decoding
|
||||||
|
input = "a@b^c";
|
||||||
|
keyword = "xyz";
|
||||||
|
correctOutput = "x@z^b";
|
||||||
|
output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed symbol encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test mixed case, whitespace, symbol decoding
|
||||||
|
input = "Attack at dawn";
|
||||||
|
keyword = "lemon";
|
||||||
|
correctOutput = "lxfopv ef rnhr";
|
||||||
|
output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed mixed case, whitespace, symbol decoding.", correctOutput, output);
|
||||||
|
//Test mixed case, whitespace, symbol decoding test 2
|
||||||
|
input = "'Crypto' is short for 'Cryptography'";
|
||||||
|
keyword = " a@b C=d";
|
||||||
|
correctOutput = "'csastp' kv siqut gqu 'csastpiuaqjb'";
|
||||||
|
output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed mixed case, whitespace, symbol encoding with mangled keyword.", correctOutput, output);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void testNoSymbolEncode() throws Exception{
|
||||||
|
Vigenere cipher = new Vigenere(true, true, false);
|
||||||
|
|
||||||
|
//Test lowercase encoding
|
||||||
|
String input = "abc";
|
||||||
|
String keyword = "xyz";
|
||||||
|
String correctOutput = "xzb";
|
||||||
|
String output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed lowercase encoding.", correctOutput, output);
|
||||||
|
//Test upercase encoding
|
||||||
|
input = "ABC";
|
||||||
|
keyword = "xyz";
|
||||||
|
correctOutput = "XZB";
|
||||||
|
output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed uppercase encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test whitespace encoding
|
||||||
|
input = "a b c";
|
||||||
|
keyword = "xyz";
|
||||||
|
correctOutput = "x z b";
|
||||||
|
output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed whitespace encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test symbol decoding
|
||||||
|
input = "a@b^c";
|
||||||
|
keyword = "xyz";
|
||||||
|
correctOutput = "xzb";
|
||||||
|
output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed symbol encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test mixed case, whitespace, symbol decoding
|
||||||
|
input = "Attack at dawn";
|
||||||
|
keyword = "lemon";
|
||||||
|
correctOutput = "Lxfopv ef rnhr";
|
||||||
|
output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed mixed case, whitespace, symbol decoding.", correctOutput, output);
|
||||||
|
//Test mixed case, whitespace, symbol decoding test 2
|
||||||
|
input = "'Crypto' is short for 'Cryptography'";
|
||||||
|
keyword = " a@b C=d";
|
||||||
|
correctOutput = "Csastp kv siqut gqu Csastpiuaqjb";
|
||||||
|
output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed mixed case, whitespace, symbol encoding with mangled keyword.", correctOutput, output);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void testNoCapitalWhitespaceSymbolEncode() throws Exception{
|
||||||
|
Vigenere cipher = new Vigenere(false, false, false);
|
||||||
|
|
||||||
|
//Test lowercase encoding
|
||||||
|
String input = "abc";
|
||||||
|
String keyword = "xyz";
|
||||||
|
String correctOutput = "xzb";
|
||||||
|
String output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed lowercase encoding.", correctOutput, output);
|
||||||
|
//Test upercase encoding
|
||||||
|
input = "ABC";
|
||||||
|
keyword = "xyz";
|
||||||
|
correctOutput = "xzb";
|
||||||
|
output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed uppercase encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test whitespace encoding
|
||||||
|
input = "a b c";
|
||||||
|
keyword = "xyz";
|
||||||
|
correctOutput = "xzb";
|
||||||
|
output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed whitespace encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test symbol decoding
|
||||||
|
input = "a@b^c";
|
||||||
|
keyword = "xyz";
|
||||||
|
correctOutput = "xzb";
|
||||||
|
output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed symbol encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test mixed case, whitespace, symbol decoding
|
||||||
|
input = "Attack at dawn";
|
||||||
|
keyword = "lemon";
|
||||||
|
correctOutput = "lxfopvefrnhr";
|
||||||
|
output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed mixed case, whitespace, symbol decoding.", correctOutput, output);
|
||||||
|
//Test mixed case, whitespace, symbol decoding test 2
|
||||||
|
input = "'Crypto' is short for 'Cryptography'";
|
||||||
|
keyword = " a@b C=d";
|
||||||
|
correctOutput = "csastpkvsiqutgqucsastpiuaqjb";
|
||||||
|
output = cipher.encode(keyword, input);
|
||||||
|
assertEquals("Vigenere failed mixed case, whitespace, symbol encoding with mangled keyword.", correctOutput, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testKeyword() throws Exception{
|
||||||
|
Vigenere cipher = new Vigenere();
|
||||||
|
|
||||||
|
//Test keyword with whitespace
|
||||||
|
String keyword = "x y z ";
|
||||||
|
String correctOutput = "XYZ";
|
||||||
|
cipher.setKeyword(keyword);
|
||||||
|
String output = cipher.getKeyword();
|
||||||
|
assertEquals("Vigenere failed keyword with whitespace", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test keyword with symbol
|
||||||
|
keyword = "x-y@z0";
|
||||||
|
correctOutput = "XYZ";
|
||||||
|
cipher.setKeyword(keyword);
|
||||||
|
output = cipher.getKeyword();
|
||||||
|
assertEquals("Vigenere failed keyword with symbol", correctOutput, output);
|
||||||
|
|
||||||
|
|
||||||
|
//Test keyword with mixed case
|
||||||
|
keyword = "xYz";
|
||||||
|
correctOutput = "XYZ";
|
||||||
|
cipher.setKeyword(keyword);
|
||||||
|
output = cipher.getKeyword();
|
||||||
|
assertEquals("Vigenere failed keyword with mixed case", correctOutput, output);
|
||||||
|
|
||||||
|
//Test keyword with whitespace, symbol and keyword
|
||||||
|
keyword = "x Y%z ";
|
||||||
|
correctOutput = "XYZ";
|
||||||
|
cipher.setKeyword(keyword);
|
||||||
|
output = cipher.getKeyword();
|
||||||
|
assertEquals("Vigenere failed keyword with space, symbol, and mixed case", correctOutput, output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user