Finished implementing Hill cipher
This commit is contained in:
@@ -1,91 +1,529 @@
|
|||||||
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamjava/polySubstitution/TestHill.java
|
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamjava/polySubstitution/TestHill.java
|
||||||
//Mattrixwv
|
//Mattrixwv
|
||||||
// Created: 01-31-22
|
// Created: 01-31-22
|
||||||
//Modified: 02-11-22
|
//Modified: 02-17-22
|
||||||
package com.mattrixwv.CipherStreamJava.polySubstitution;
|
package com.mattrixwv.CipherStreamJava.polySubstitution;
|
||||||
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.security.InvalidKeyException;
|
||||||
|
|
||||||
|
import com.mattrixwv.CipherStreamJava.exceptions.InvalidCharacterException;
|
||||||
|
import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
|
||||||
public class TestHill{
|
public class TestHill{
|
||||||
@Test
|
@Test
|
||||||
public void testEncode(){
|
public void testEncode() throws InvalidCharacterException, InvalidKeyException, InvalidInputException{
|
||||||
Hill cipher = new Hill(true, true, true, true);
|
Hill cipher = new Hill(true, true, true, true);
|
||||||
|
|
||||||
//Test lowercase encoding
|
//Test lowercase encoding
|
||||||
String inputString = "messagetoencode";
|
String inputString = "messagetoencode";
|
||||||
int[][] key = {{2, 0, 0}, {0, 2, 0}, {0, 0, 2}};
|
int[][] key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
String correctOutput = "yikkamimciaecgi";
|
String correctOutput = "mgkeqgeulikhisp";
|
||||||
String output = cipher.encode(key, inputString);
|
String output = cipher.encode(key, inputString);
|
||||||
assertEquals("Hill failed lowercase encoding.", correctOutput, output);
|
assertEquals("Hill failed lowercase encoding.", correctOutput, output);
|
||||||
//Test uppercase encoding
|
//Test uppercase encoding
|
||||||
inputString = "MESSAGETOENCODE";
|
inputString = "MESSAGETOENCODE";
|
||||||
key = new int[][]{{2, 0, 0}, {0, 2, 0}, {0, 0, 2}};
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
correctOutput = "YIKKAMIMCIAECGI";
|
correctOutput = "MGKEQGEULIKHISP";
|
||||||
output = cipher.encode(key, inputString);
|
output = cipher.encode(key, inputString);
|
||||||
assertEquals("Hill failed uppercase encoding.", correctOutput, output);
|
assertEquals("Hill failed uppercase encoding.", correctOutput, output);
|
||||||
|
|
||||||
//Test whitespace encoding
|
//Test whitespace encoding
|
||||||
inputString = "message to encode";
|
inputString = "message to encode";
|
||||||
key = new int[][]{{2, 0, 0}, {0, 2, 0}, {0, 0, 2}};
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
correctOutput = "yikkami mc iaecgi";
|
correctOutput = "mgkeqge ul ikhisp";
|
||||||
output = cipher.encode(key, inputString);
|
output = cipher.encode(key, inputString);
|
||||||
assertEquals("Hill failed whitespace encoding.", correctOutput, output);
|
assertEquals("Hill failed whitespace encoding.", correctOutput, output);
|
||||||
|
|
||||||
//Test symbol encoding
|
//Test symbol encoding
|
||||||
inputString = "message*to+encode";
|
inputString = "message*to+encode";
|
||||||
key = new int[][]{{2, 0, 0}, {0, 2, 0}, {0, 0, 2}};
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
correctOutput = "yikkami*mc+iaecgi";
|
correctOutput = "mgkeqge*ul+ikhisp";
|
||||||
output = cipher.encode(key, inputString);
|
output = cipher.encode(key, inputString);
|
||||||
assertEquals("Hill failed symbol encoding.", correctOutput, output);
|
assertEquals("Hill failed symbol encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test padding encoding
|
||||||
|
inputString = "messagetoencod";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "mgkeqgeulikhul";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed padding encoding.", correctOutput, output);
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol encoding
|
//Test mixed case, whitespace, and symbol encoding
|
||||||
inputString = "Message to^encode";
|
inputString = "Message to^encode";
|
||||||
key = new int[][]{{2, 0, 0}, {0, 2, 0}, {0, 0, 2}};
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
correctOutput = "Yikkami mc^iaecgi";
|
correctOutput = "Mgkeqge ul^ikhisp";
|
||||||
output = cipher.encode(key, inputString);
|
output = cipher.encode(key, inputString);
|
||||||
assertEquals("Hill failed mixed case, whitespace, and symbol encoding.", correctOutput, output);
|
assertEquals("Hill failed mixed case, whitespace, and symbol encoding.", correctOutput, output);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDecode(){
|
public void testNoCapitalEncode() throws InvalidCharacterException, InvalidKeyException, InvalidInputException{
|
||||||
|
Hill cipher = new Hill(false, true, true, true);
|
||||||
|
|
||||||
|
//Test lowercase encoding
|
||||||
|
String inputString = "messagetoencode";
|
||||||
|
int[][] key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
String correctOutput = "MGKEQGEULIKHISP";
|
||||||
|
String output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed no capital lowercase encoding.", correctOutput, output);
|
||||||
|
//Test uppercase encoding
|
||||||
|
inputString = "MESSAGETOENCODE";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "MGKEQGEULIKHISP";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed no capital uppercase encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test whitespace encoding
|
||||||
|
inputString = "message to encode";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "MGKEQGE UL IKHISP";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed no capital whitespace encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test symbol encoding
|
||||||
|
inputString = "message*to+encode";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "MGKEQGE*UL+IKHISP";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed no capital symbol encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test padding encoding
|
||||||
|
inputString = "messagetoencod";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "MGKEQGEULIKHUL";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed no capital padding encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test mixed case, whitespace, and symbol encoding
|
||||||
|
inputString = "Message to^encode";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "MGKEQGE UL^IKHISP";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed no capital mixed case, whitespace, and symbol encoding.", correctOutput, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNoWhitespaceEncode() throws InvalidCharacterException, InvalidKeyException, InvalidInputException{
|
||||||
|
Hill cipher = new Hill(true, false, true, true);
|
||||||
|
|
||||||
|
//Test lowercase encoding
|
||||||
|
String inputString = "messagetoencode";
|
||||||
|
int[][] key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
String correctOutput = "mgkeqgeulikhisp";
|
||||||
|
String output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed no whitespace lowercase encoding.", correctOutput, output);
|
||||||
|
//Test uppercase encoding
|
||||||
|
inputString = "MESSAGETOENCODE";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "MGKEQGEULIKHISP";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed no whitespace uppercase encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test whitespace encoding
|
||||||
|
inputString = "message to encode";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "mgkeqgeulikhisp";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed no whitespace whitespace encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test symbol encoding
|
||||||
|
inputString = "message*to+encode";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "mgkeqge*ul+ikhisp";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed no whitespace symbol encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test padding encoding
|
||||||
|
inputString = "messagetoencod";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "mgkeqgeulikhul";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed no whitespace padding encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test mixed case, whitespace, and symbol encoding
|
||||||
|
inputString = "Message to^encode";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "Mgkeqgeul^ikhisp";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed no whitespace mixed case, whitespace, and symbol encoding.", correctOutput, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNoSymbolEncode() throws InvalidCharacterException, InvalidKeyException, InvalidInputException{
|
||||||
|
Hill cipher = new Hill(true, true, false, true);
|
||||||
|
|
||||||
|
//Test lowercase encoding
|
||||||
|
String inputString = "messagetoencode";
|
||||||
|
int[][] key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
String correctOutput = "mgkeqgeulikhisp";
|
||||||
|
String output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed no symbol lowercase encoding.", correctOutput, output);
|
||||||
|
//Test uppercase encoding
|
||||||
|
inputString = "MESSAGETOENCODE";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "MGKEQGEULIKHISP";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed no symbol uppercase encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test whitespace encoding
|
||||||
|
inputString = "message to encode";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "mgkeqge ul ikhisp";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed no symbol whitespace encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test symbol encoding
|
||||||
|
inputString = "message*to+encode";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "mgkeqgeulikhisp";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed no symbol symbol encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test padding encoding
|
||||||
|
inputString = "messagetoencod";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "mgkeqgeulikhul";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed no symbol padding encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test mixed case, whitespace, and symbol encoding
|
||||||
|
inputString = "Message to^encode";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "Mgkeqge ulikhisp";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed no symbol mixed case, whitespace, and symbol encoding.", correctOutput, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNoPaddingEncoding() throws InvalidCharacterException, InvalidKeyException, InvalidInputException{
|
||||||
|
Hill cipher = new Hill(true, true, true, false);
|
||||||
|
|
||||||
|
//Test lowercase encoding
|
||||||
|
String inputString = "messagetoencode";
|
||||||
|
int[][] key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
String correctOutput = "mgkeqgeulikhisp";
|
||||||
|
String output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed no padding lowercase encoding.", correctOutput, output);
|
||||||
|
//Test uppercase encoding
|
||||||
|
inputString = "MESSAGETOENCODE";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "MGKEQGEULIKHISP";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed no padding uppercase encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test whitespace encoding
|
||||||
|
inputString = "message to encode";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "mgkeqge ul ikhisp";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed no padding whitespace encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test symbol encoding
|
||||||
|
inputString = "message*to+encode";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "mgkeqge*ul+ikhisp";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed no padding symbol encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test padding encoding
|
||||||
|
inputString = "messagetoencod";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "mgkeqgeulikhulb";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed no padding padding encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test mixed case, whitespace, and symbol encoding
|
||||||
|
inputString = "Message to^encode";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "Mgkeqge ul^ikhisp";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed no padding mixed case, whitespace, and symbol encoding.", correctOutput, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNoCapitalWhitespaceSymbolEncode() throws InvalidCharacterException, InvalidKeyException, InvalidInputException{
|
||||||
|
Hill cipher = new Hill(false, false, false, false);
|
||||||
|
|
||||||
|
//Test lowercase encoding
|
||||||
|
String inputString = "messagetoencode";
|
||||||
|
int[][] key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
String correctOutput = "MGKEQGEULIKHISP";
|
||||||
|
String output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed secure lowercase encoding.", correctOutput, output);
|
||||||
|
//Test uppercase encoding
|
||||||
|
inputString = "MESSAGETOENCODE";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "MGKEQGEULIKHISP";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed secure uppercase encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test whitespace encoding
|
||||||
|
inputString = "message to encode";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "MGKEQGEULIKHISP";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed secure whitespace encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test symbol encoding
|
||||||
|
inputString = "message*to+encode";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "MGKEQGEULIKHISP";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed secure symbol encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test padding encoding
|
||||||
|
inputString = "messagetoencod";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "MGKEQGEULIKHULB";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed secure padding encoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test mixed case, whitespace, and symbol encoding
|
||||||
|
inputString = "Message to^encode";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "MGKEQGEULIKHISP";
|
||||||
|
output = cipher.encode(key, inputString);
|
||||||
|
assertEquals("Hill failed secure mixed case, whitespace, and symbol encoding.", correctOutput, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDecode() throws InvalidCharacterException, InvalidKeyException, InvalidInputException{
|
||||||
Hill cipher = new Hill(true, true, true, true);
|
Hill cipher = new Hill(true, true, true, true);
|
||||||
|
|
||||||
//Test lowercase decoding
|
//Test lowercase decoding
|
||||||
String inputString = "yikkamimciaecgi";
|
String inputString = "mgkeqgeulikhisp";
|
||||||
int[][] key = {{2, 0, 0}, {0, 2, 0}, {0, 0, 2}};
|
int[][] key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
String correctOutput = "messagetoencode";
|
String correctOutput = "messagetoencode";
|
||||||
String output = cipher.decode(key, inputString);
|
String output = cipher.decode(key, inputString);
|
||||||
assertEquals("Hill failed lowercase decoding.", correctOutput, output);
|
assertEquals("Hill failed lowercase decoding.", correctOutput, output);
|
||||||
//Test uppercase decoding
|
//Test uppercase decoding
|
||||||
inputString = "YIKKAMIMCIAECGI";
|
inputString = "MGKEQGEULIKHISP";
|
||||||
key = new int[][]{{2, 0, 0}, {0, 2, 0}, {0, 0, 2}};
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
correctOutput = "MESSAGETOENCODE";
|
correctOutput = "MESSAGETOENCODE";
|
||||||
output = cipher.decode(key, inputString);
|
output = cipher.decode(key, inputString);
|
||||||
assertEquals("Hill failed uppercase decoding.", correctOutput, output);
|
assertEquals("Hill failed uppercase decoding.", correctOutput, output);
|
||||||
|
|
||||||
//Test whitespace decoding
|
//Test whitespace decoding
|
||||||
inputString = "yikkami mc iaecgi";
|
inputString = "mgkeqge ul ikhisp";
|
||||||
key = new int[][]{{2, 0, 0}, {0, 2, 0}, {0, 0, 2}};
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
correctOutput = "message to encode";
|
correctOutput = "message to encode";
|
||||||
output = cipher.decode(key, inputString);
|
output = cipher.decode(key, inputString);
|
||||||
assertEquals("Hill failed whitespace decoding.", correctOutput, output);
|
assertEquals("Hill failed whitespace decoding.", correctOutput, output);
|
||||||
|
|
||||||
//Test symbol decoding
|
//Test symbol decoding
|
||||||
inputString = "yikkami*mc+iaecgi";
|
inputString = "mgkeqge*ul+ikhisp";
|
||||||
key = new int[][]{{2, 0, 0}, {0, 2, 0}, {0, 0, 2}};
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
correctOutput = "message*to+encode";
|
correctOutput = "message*to+encode";
|
||||||
output = cipher.decode(key, inputString);
|
output = cipher.decode(key, inputString);
|
||||||
assertEquals("Hill failed symbol decoding.", correctOutput, output);
|
assertEquals("Hill failed symbol decoding.", correctOutput, output);
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol decoding
|
//Test mixed case, whitespace, and symbol decoding
|
||||||
inputString = "Yikkami mc^iaecgi";
|
inputString = "Mgkeqge ul^ikhisp";
|
||||||
key = new int[][]{{2, 0, 0}, {0, 2, 0}, {0, 0, 2}};
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
correctOutput = "Message to^encode";
|
correctOutput = "Message to^encode";
|
||||||
output = cipher.decode(key, inputString);
|
output = cipher.decode(key, inputString);
|
||||||
assertEquals("Hill failed mixed case, whitespace, and symbol decoding.", correctOutput, output);
|
assertEquals("Hill failed mixed case, whitespace, and symbol decoding.", correctOutput, output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNoCapitalDecode() throws InvalidCharacterException, InvalidKeyException, InvalidInputException{
|
||||||
|
Hill cipher = new Hill(false, true, true, true);
|
||||||
|
|
||||||
|
//Test lowercase decoding
|
||||||
|
String inputString = "mgkeqgeulikhisp";
|
||||||
|
int[][] key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
String correctOutput = "MESSAGETOENCODE";
|
||||||
|
String output = cipher.decode(key, inputString);
|
||||||
|
assertEquals("Hill failed no capital lowercase decoding.", correctOutput, output);
|
||||||
|
//Test uppercase decoding
|
||||||
|
inputString = "MGKEQGEULIKHISP";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "MESSAGETOENCODE";
|
||||||
|
output = cipher.decode(key, inputString);
|
||||||
|
assertEquals("Hill failed no capital uppercase decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test whitespace decoding
|
||||||
|
inputString = "mgkeqge ul ikhisp";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "MESSAGE TO ENCODE";
|
||||||
|
output = cipher.decode(key, inputString);
|
||||||
|
assertEquals("Hill failed no capital whitespace decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test symbol decoding
|
||||||
|
inputString = "mgkeqge*ul+ikhisp";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "MESSAGE*TO+ENCODE";
|
||||||
|
output = cipher.decode(key, inputString);
|
||||||
|
assertEquals("Hill failed no capital symbol decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test mixed case, whitespace, and symbol decoding
|
||||||
|
inputString = "Mgkeqge ul^ikhisp";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "MESSAGE TO^ENCODE";
|
||||||
|
output = cipher.decode(key, inputString);
|
||||||
|
assertEquals("Hill failed no capital mixed case, whitespace, and symbol decoding.", correctOutput, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNoWhitespaceDecode() throws InvalidCharacterException, InvalidKeyException, InvalidInputException{
|
||||||
|
Hill cipher = new Hill(true, false, true, true);
|
||||||
|
|
||||||
|
//Test lowercase decoding
|
||||||
|
String inputString = "mgkeqgeulikhisp";
|
||||||
|
int[][] key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
String correctOutput = "messagetoencode";
|
||||||
|
String output = cipher.decode(key, inputString);
|
||||||
|
assertEquals("Hill failed no whitespace lowercase decoding.", correctOutput, output);
|
||||||
|
//Test uppercase decoding
|
||||||
|
inputString = "MGKEQGEULIKHISP";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "MESSAGETOENCODE";
|
||||||
|
output = cipher.decode(key, inputString);
|
||||||
|
assertEquals("Hill failed no whitespace uppercase decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test whitespace decoding
|
||||||
|
inputString = "mgkeqge ul ikhisp";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "messagetoencode";
|
||||||
|
output = cipher.decode(key, inputString);
|
||||||
|
assertEquals("Hill failed no whitespace whitespace decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test symbol decoding
|
||||||
|
inputString = "mgkeqge*ul+ikhisp";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "message*to+encode";
|
||||||
|
output = cipher.decode(key, inputString);
|
||||||
|
assertEquals("Hill failed no whitespace symbol decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test mixed case, whitespace, and symbol decoding
|
||||||
|
inputString = "Mgkeqge ul^ikhisp";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "Messageto^encode";
|
||||||
|
output = cipher.decode(key, inputString);
|
||||||
|
assertEquals("Hill failed no whitespace mixed case, whitespace, and symbol decoding.", correctOutput, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNoSymbolDecode() throws InvalidCharacterException, InvalidKeyException, InvalidInputException{
|
||||||
|
Hill cipher = new Hill(true, true, false, true);
|
||||||
|
|
||||||
|
//Test lowercase decoding
|
||||||
|
String inputString = "mgkeqgeulikhisp";
|
||||||
|
int[][] key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
String correctOutput = "messagetoencode";
|
||||||
|
String output = cipher.decode(key, inputString);
|
||||||
|
assertEquals("Hill failed no symbol lowercase decoding.", correctOutput, output);
|
||||||
|
//Test uppercase decoding
|
||||||
|
inputString = "MGKEQGEULIKHISP";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "MESSAGETOENCODE";
|
||||||
|
output = cipher.decode(key, inputString);
|
||||||
|
assertEquals("Hill failed no symbol uppercase decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test whitespace decoding
|
||||||
|
inputString = "mgkeqge ul ikhisp";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "message to encode";
|
||||||
|
output = cipher.decode(key, inputString);
|
||||||
|
assertEquals("Hill failed no symbol whitespace decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test symbol decoding
|
||||||
|
inputString = "mgkeqge*ul+ikhisp";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "messagetoencode";
|
||||||
|
output = cipher.decode(key, inputString);
|
||||||
|
assertEquals("Hill failed no symbol symbol decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test mixed case, whitespace, and symbol decoding
|
||||||
|
inputString = "Mgkeqge ul^ikhisp";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "Message toencode";
|
||||||
|
output = cipher.decode(key, inputString);
|
||||||
|
assertEquals("Hill failed no symbol mixed case, whitespace, and symbol decoding.", correctOutput, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNoPaddingDecoding() throws InvalidCharacterException, InvalidKeyException, InvalidInputException{
|
||||||
|
Hill cipher = new Hill(true, true, true, false);
|
||||||
|
|
||||||
|
//Test lowercase decoding
|
||||||
|
String inputString = "mgkeqgeulikhisp";
|
||||||
|
int[][] key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
String correctOutput = "messagetoencode";
|
||||||
|
String output = cipher.decode(key, inputString);
|
||||||
|
assertEquals("Hill failed no padding lowercase decoding.", correctOutput, output);
|
||||||
|
//Test uppercase decoding
|
||||||
|
inputString = "MGKEQGEULIKHISP";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "MESSAGETOENCODE";
|
||||||
|
output = cipher.decode(key, inputString);
|
||||||
|
assertEquals("Hill failed no padding uppercase decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test whitespace decoding
|
||||||
|
inputString = "mgkeqge ul ikhisp";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "message to encode";
|
||||||
|
output = cipher.decode(key, inputString);
|
||||||
|
assertEquals("Hill failed no padding whitespace decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test symbol decoding
|
||||||
|
inputString = "mgkeqge*ul+ikhisp";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "message*to+encode";
|
||||||
|
output = cipher.decode(key, inputString);
|
||||||
|
assertEquals("Hill failed no padding symbol decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test mixed case, whitespace, and symbol decoding
|
||||||
|
inputString = "Mgkeqge ul^ikhisp";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "Message to^encode";
|
||||||
|
output = cipher.decode(key, inputString);
|
||||||
|
assertEquals("Hill failed no padding mixed case, whitespace, and symbol decoding.", correctOutput, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNoCapitalWhitespaceSymbolDecode() throws InvalidCharacterException, InvalidKeyException, InvalidInputException{
|
||||||
|
Hill cipher = new Hill(false, false, false, false);
|
||||||
|
|
||||||
|
//Test lowercase decoding
|
||||||
|
String inputString = "mgkeqgeulikhisp";
|
||||||
|
int[][] key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
String correctOutput = "MESSAGETOENCODE";
|
||||||
|
String output = cipher.decode(key, inputString);
|
||||||
|
assertEquals("Hill failed secure lowercase decoding.", correctOutput, output);
|
||||||
|
//Test uppercase decoding
|
||||||
|
inputString = "MGKEQGEULIKHISP";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "MESSAGETOENCODE";
|
||||||
|
output = cipher.decode(key, inputString);
|
||||||
|
assertEquals("Hill failed secure uppercase decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test whitespace decoding
|
||||||
|
inputString = "mgkeqge ul ikhisp";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "MESSAGETOENCODE";
|
||||||
|
output = cipher.decode(key, inputString);
|
||||||
|
assertEquals("Hill failed secure whitespace decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test symbol decoding
|
||||||
|
inputString = "mgkeqge*ul+ikhisp";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "MESSAGETOENCODE";
|
||||||
|
output = cipher.decode(key, inputString);
|
||||||
|
assertEquals("Hill failed secure symbol decoding.", correctOutput, output);
|
||||||
|
|
||||||
|
//Test mixed case, whitespace, and symbol decoding
|
||||||
|
inputString = "Mgkeqge ul^ikhisp";
|
||||||
|
key = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||||
|
correctOutput = "MESSAGETOENCODE";
|
||||||
|
output = cipher.decode(key, inputString);
|
||||||
|
assertEquals("Hill failed secure mixed case, whitespace, and symbol decoding.", correctOutput, output);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user