Added Polybius Square implementation

This commit is contained in:
2022-01-04 22:47:28 -05:00
parent cb7bf18836
commit 7987df7635
2 changed files with 666 additions and 0 deletions

View File

@@ -0,0 +1,320 @@
//CipherStreamJava/src/test/java/mattrixwv/CipherStreamJava/TestPolybiusSquare.java
//Mattrixwv
// Created: 01-04-21
//Modified: 01-04-21
package mattrixwv.CipherStreamJava;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import mattrixwv.CipherStreamJava.PolybiusSquare.InvalidCharacterException;
public class TestPolybiusSquare{
@Test
public void testDecode() throws InvalidCharacterException{
PolybiusSquare cipher = new PolybiusSquare(true, true);
//Test simple decoding
String inputString = "121144";
String keyword = "";
String correctOutput = "BAT";
String output = cipher.decode(keyword, inputString);
assertEquals("PolybiusSquare failed simple decoding.", correctOutput, output);
//Test whitespace decoding
inputString = "12 11 44";
keyword = "";
correctOutput = "B A T";
output = cipher.decode(keyword, inputString);
assertEquals("PolybiusSquare failed whitespace decoding.", correctOutput, output);
//Test symbol decoding
inputString = "12@11+44-";
keyword = "";
correctOutput = "B@A+T-";
output = cipher.decode(keyword, inputString);
assertEquals("PolybiusSquare failed symbol decoding.", correctOutput, output);
//Test whitespace, symbol decoding
inputString = "12 11-44";
keyword = "";
correctOutput = "B A-T";
output = cipher.decode(keyword, inputString);
assertEquals("PolybiusSquare failed whitespace, symbol decoding.", correctOutput, output);
//Test whitespace, symbol decoding with mangled keyword
inputString = "15 14-52";
keyword = "Z Y+ X-";
correctOutput = "B A-T";
output = cipher.decode(keyword, inputString);
assertEquals("PolybiusSquare failed whitespace, symbol decoding with mangled keyword.", correctOutput, output);
}
@Test
public void testNoWhitespaceDecode() throws InvalidCharacterException{
PolybiusSquare cipher = new PolybiusSquare(false, true);
//Test simple decoding
String inputString = "121144";
String keyword = "";
String correctOutput = "BAT";
String output = cipher.decode(keyword, inputString);
assertEquals("PolybiusSquare failed no whitespace simple decoding.", correctOutput, output);
//Test whitespace decoding
inputString = "12 11 44";
keyword = "";
correctOutput = "BAT";
output = cipher.decode(keyword, inputString);
assertEquals("PolybiusSquare failed no whitespace whitespace decoding.", correctOutput, output);
//Test symbol decoding
inputString = "12@11+44-";
keyword = "";
correctOutput = "B@A+T-";
output = cipher.decode(keyword, inputString);
assertEquals("PolybiusSquare failed no whitespace symbol decoding.", correctOutput, output);
//Test whitespace, symbol decoding
inputString = "12 11-44";
keyword = "";
correctOutput = "BA-T";
output = cipher.decode(keyword, inputString);
assertEquals("PolybiusSquare failed no whitespace whitespace, symbol decoding.", correctOutput, output);
//Test whitespace, symbol decoding with mangled keyword
inputString = "15 14-52";
keyword = "Z Y+ X-";
correctOutput = "BA-T";
output = cipher.decode(keyword, inputString);
assertEquals("PolybiusSquare failed no whitespace whitespace, symbol decoding with mangled keyword.", correctOutput, output);
}
@Test
public void testNoSymbolDeocde() throws InvalidCharacterException{
PolybiusSquare cipher = new PolybiusSquare(true, false);
//Test simple decoding
String inputString = "121144";
String keyword = "";
String correctOutput = "BAT";
String output = cipher.decode(keyword, inputString);
assertEquals("PolybiusSquare failed no symbol simple decoding.", correctOutput, output);
//Test whitespace decoding
inputString = "12 11 44";
keyword = "";
correctOutput = "B A T";
output = cipher.decode(keyword, inputString);
assertEquals("PolybiusSquare failed no symbol whitespace decoding.", correctOutput, output);
//Test symbol decoding
inputString = "12@11+44-";
keyword = "";
correctOutput = "BAT";
output = cipher.decode(keyword, inputString);
assertEquals("PolybiusSquare failed no symbol symbol decoding.", correctOutput, output);
//Test whitespace, symbol decoding
inputString = "12 11-44";
keyword = "";
correctOutput = "B AT";
output = cipher.decode(keyword, inputString);
assertEquals("PolybiusSquare failed no symbol whitespace, symbol decoding.", correctOutput, output);
//Test whitespace, symbol decoding with mangled keyword
inputString = "15 14-52";
keyword = "Z Y+ X-";
correctOutput = "B AT";
output = cipher.decode(keyword, inputString);
assertEquals("PolybiusSquare failed no symbol whitespace, symbol decoding with mangled keyword.", correctOutput, output);
}
@Test
public void testNoWhitespaceSymbolDecode() throws InvalidCharacterException{
PolybiusSquare cipher = new PolybiusSquare(false, false);
//Test simple decoding
String inputString = "121144";
String keyword = "";
String correctOutput = "BAT";
String output = cipher.decode(keyword, inputString);
assertEquals("PolybiusSquare failed secure simple decoding.", correctOutput, output);
//Test whitespace decoding
inputString = "12 11 44";
keyword = "";
correctOutput = "BAT";
output = cipher.decode(keyword, inputString);
assertEquals("PolybiusSquare failed secure whitespace decoding.", correctOutput, output);
//Test symbol decoding
inputString = "12@11+44-";
keyword = "";
correctOutput = "BAT";
output = cipher.decode(keyword, inputString);
assertEquals("PolybiusSquare failed secure symbol decoding.", correctOutput, output);
//Test whitespace, symbol decoding
inputString = "12 11-44";
keyword = "";
correctOutput = "BAT";
output = cipher.decode(keyword, inputString);
assertEquals("PolybiusSquare failed secure whitespace, symbol decoding.", correctOutput, output);
//Test whitespace, symbol decoding with mangled keyword
inputString = "15 14-52";
keyword = "Z Y+ X-";
correctOutput = "BAT";
output = cipher.decode(keyword, inputString);
assertEquals("PolybiusSquare failed secure whitespace, symbol decoding with mangled keyword.", correctOutput, output);
}
@Test
public void testEncode() throws InvalidCharacterException, Exception{
PolybiusSquare cipher = new PolybiusSquare(true, true);
//Test simple encoding
String inputString = "BAT";
String keyword = "";
String correctOutput = "121144";
String output = cipher.encode(keyword, inputString);
assertEquals("PolybiusSquare failed simple encoding.", correctOutput, output);
//Test whitespace encoding
inputString = "B A T";
keyword = "";
correctOutput = "12 11 44";
output = cipher.encode(keyword, inputString);
assertEquals("PolybiusSquare failed whitespace encoding.", correctOutput, output);
//Test symbol encoding
inputString = "B@A+T-";
keyword = "";
correctOutput = "12@11+44-";
output = cipher.encode(keyword, inputString);
assertEquals("PolybiusSquare failed symbol encoding.", correctOutput, output);
//Test whitespace, symbol decoding
inputString = "B A-T";
keyword = "";
correctOutput = "12 11-44";
output = cipher.encode(keyword, inputString);
assertEquals("PolybiusSquare failed whitespace, symbol encoding.", correctOutput, output);
//Test whitespace, symbol decoding with mangled keyword
inputString = "B A-T";
keyword = "Z Y+ X-";
correctOutput = "15 14-52";
output = cipher.encode(keyword, inputString);
assertEquals("PolybiusSquare failed whitespace, symbol encoding with mangled keyword.", correctOutput, output);
}
@Test
public void testNoWhitespaceEncode() throws InvalidCharacterException, Exception{
PolybiusSquare cipher = new PolybiusSquare(false, true);
//Test simple encoding
String inputString = "BAT";
String keyword = "";
String correctOutput = "121144";
String output = cipher.encode(keyword, inputString);
assertEquals("PolybiusSquare failed no whitespace simple encoding.", correctOutput, output);
//Test whitespace encoding
inputString = "B A T";
keyword = "";
correctOutput = "121144";
output = cipher.encode(keyword, inputString);
assertEquals("PolybiusSquare failed no whitespace whitespace encoding.", correctOutput, output);
//Test symbol encoding
inputString = "B@A+T-";
keyword = "";
correctOutput = "12@11+44-";
output = cipher.encode(keyword, inputString);
assertEquals("PolybiusSquare failed no whitespace symbol encoding.", correctOutput, output);
//Test whitespace, symbol decoding
inputString = "B A-T";
keyword = "";
correctOutput = "1211-44";
output = cipher.encode(keyword, inputString);
assertEquals("PolybiusSquare failed no whitespace whitespace, symbol encoding.", correctOutput, output);
//Test whitespace, symbol decoding with mangled keyword
inputString = "B A-T";
keyword = "Z Y+ X-";
correctOutput = "1514-52";
output = cipher.encode(keyword, inputString);
assertEquals("PolybiusSquare failed no whitespace whitespace, symbol encoding with mangled keyword.", correctOutput, output);
}
@Test
public void testNoSymbolEncode() throws InvalidCharacterException, Exception{
PolybiusSquare cipher = new PolybiusSquare(true, false);
//Test simple encoding
String inputString = "BAT";
String keyword = "";
String correctOutput = "121144";
String output = cipher.encode(keyword, inputString);
assertEquals("PolybiusSquare failed no symbol simple encoding.", correctOutput, output);
//Test whitespace encoding
inputString = "B A T";
keyword = "";
correctOutput = "12 11 44";
output = cipher.encode(keyword, inputString);
assertEquals("PolybiusSquare failed no symbol whitespace encoding.", correctOutput, output);
//Test symbol encoding
inputString = "B@A+T-";
keyword = "";
correctOutput = "121144";
output = cipher.encode(keyword, inputString);
assertEquals("PolybiusSquare failed no symbol symbol encoding.", correctOutput, output);
//Test whitespace, symbol decoding
inputString = "B A-T";
keyword = "";
correctOutput = "12 1144";
output = cipher.encode(keyword, inputString);
assertEquals("PolybiusSquare failed whitespace, symbol encoding.", correctOutput, output);
//Test whitespace, symbol decoding with mangled keyword
inputString = "B A-T";
keyword = "Z Y+ X-";
correctOutput = "15 1452";
output = cipher.encode(keyword, inputString);
assertEquals("PolybiusSquare failed no symbol whitespace, symbol encoding with mangled keyword.", correctOutput, output);
}
@Test
public void testNoWhitespaceSymbolEncode() throws InvalidCharacterException, Exception{
PolybiusSquare cipher = new PolybiusSquare(false, false);
//Test simple encoding
String inputString = "BAT";
String keyword = "";
String correctOutput = "12 11 44";
String output = cipher.encode(keyword, inputString);
assertEquals("PolybiusSquare failed secure simple encoding.", correctOutput, output);
//Test whitespace encoding
inputString = "B A T";
keyword = "";
correctOutput = "12 11 44";
assertEquals("PolybiusSquare failed secure whitespace encoding.", correctOutput, output);
output = cipher.encode(keyword, inputString);
//Test symbol encoding
inputString = "B@A+T-";
keyword = "";
correctOutput = "12 11 44";
output = cipher.encode(keyword, inputString);
assertEquals("PolybiusSquare failed secure symbol encoding.", correctOutput, output);
//Test whitespace, symbol decoding
inputString = "B A-T";
keyword = "";
correctOutput = "12 11 44";
output = cipher.encode(keyword, inputString);
assertEquals("PolybiusSquare failed secure whitespace, symbol encoding.", correctOutput, output);
//Test whitespace, symbol decoding with mangled keyword
inputString = "B A-T";
keyword = "Z Y+ X-";
correctOutput = "15 14 52";
output = cipher.encode(keyword, inputString);
assertEquals("PolybiusSquare failed secure whitespace, symbol encoding with mangled keyword.", correctOutput, output);
}
}