Added encoding options

This commit is contained in:
2021-12-30 18:05:24 -05:00
parent feb5047d2a
commit b79e19e59b
2 changed files with 553 additions and 52 deletions

View File

@@ -1,18 +1,20 @@
//CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/Vigenere.java
//Matthew Ellison
// Created: 07-25-21
//Modified: 07-25-21
//Modified: 12-30-21
//This is the declaration of the Vigenere class
package mattrixwv.CipherStreamJava;
import java.util.ArrayList;
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 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 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
protected void setOffset(){
@@ -26,13 +28,17 @@ public class Vigenere{
}
}
//Sets inputString
protected void setInputString(String input){
//Convert all letters to uppercase
input = input.toUpperCase();
//Remove all characters except capital letters
input = input.replaceAll("[^A-Z]", "");
//Save the string
inputString = input;
protected void setInputString(String inputString){
if(!leaveCapitals){
inputString = inputString.toLowerCase();
}
if(!leaveWhitespace){
inputString = inputString.replaceAll("\\s+", "");
}
if(!leaveSymbols){
inputString = inputString.replaceAll("[^a-zA-Z0-9\\s+]", "");
}
this.inputString = inputString;
}
//Sets keyword
protected void setKeyword(String key) throws Exception{
@@ -56,14 +62,28 @@ public class Vigenere{
StringBuilder output = new StringBuilder();
//Step through every character in the inputString and advance it the correct amount, according to offset
for(int cnt = 0;cnt < inputString.length();++cnt){
char letter = (char)(inputString.charAt(cnt) + offset.get(cnt % offset.size()));
//Make sure the character is still a letter, if not, wrap around
if(letter < 'A'){
letter += 26;
int offsetCnt = 0;
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
char letter = inputString.charAt(inputCnt);
if(Character.isUpperCase(letter)){
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'){
letter -= 26;
else if(Character.isLowerCase(letter)){
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);
}
@@ -76,13 +96,26 @@ public class Vigenere{
StringBuilder output = new StringBuilder();
//Step through every character in the inputString and advance it the correct amount, according to offset
for(int cnt = 0;cnt < inputString.length();++cnt){
char letter = (char)(inputString.charAt(cnt) - offset.get(cnt % offset.size()));
if(letter < 'A'){
letter += 26;
int offsetCnt = 0;
for(int letterCnt = 0;letterCnt < inputString.length();++letterCnt){
char letter = inputString.charAt(letterCnt);
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'){
letter -= 26;
else if(Character.isLowerCase(letter)){
letter -= offset.get((offsetCnt++) % offset.size());
if(letter < 'a'){
letter += 26;
}
else if(letter > 'z'){
letter -= 26;
}
}
output.append(letter);
}
@@ -96,6 +129,16 @@ public class Vigenere{
public Vigenere(){
offset = new ArrayList<Integer>();
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
public String getInputString(){

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/TestVigenere.java
//Matthew Ellison
// Created: 07-25-21
//Modified: 07-25-21
//Modified: 12-30-21
//These are the tests for the Vigenere class
package mattrixwv.CipherStreamJava;
@@ -14,52 +14,510 @@ import org.junit.Test;
public class TestVigenere{
@Test
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 keyword = "xyz";
String correctOutput = "ABC";
String correctOutput = "abc";
String output = cipher.decode(keyword, input);
assertEquals("Vigenere Decoding failed the first test", correctOutput, output);
//Test 2
input = "LXFOPVEFRNHR";
keyword = "LEMON";
correctOutput = "ATTACKATDAWN";
assertEquals("Vigenere failed lowercase decoding.", correctOutput, output);
//Test uppercase decoding
input = "XZB";
keyword = "XYZ";
correctOutput = "ABC";
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";
keyword = "ABCD";
correctOutput = "CRYPTOISSHORTFORCRYPTOGRAPHY";
//Test whitespace decoding
input = "x z b";
keyword = "xyz";
correctOutput = "a b c";
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
public void testEncode() throws Exception{
Vigenere cipher = new Vigenere();
public void testNoWhitespaceDecode() throws Exception{
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 keyword = "xyz";
String correctOutput = "XZB";
String correctOutput = "xzb";
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";
keyword = "Lemon";
correctOutput = "LXFOPVEFRNHR";
keyword = "lemon";
correctOutput = "Lxfopv ef rnhr";
output = cipher.encode(keyword, input);
assertEquals("Vigenere Encoding failed the second test", correctOutput, output);
//Test 3
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 = "abcd";
correctOutput = "CSASTPKVSIQUTGQUCSASTPIUAQJB";
keyword = " a@b C=d";
correctOutput = "'Csastp' kv siqut gqu 'Csastpiuaqjb'";
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);
}
}