Hill Cipher tests and info added

This commit is contained in:
n1073645 2020-02-14 10:46:29 +00:00
parent f46d9414cf
commit 34cd4af5f5
5 changed files with 69 additions and 6 deletions

View file

@ -203,6 +203,14 @@ export function encode(plaintext, key) {
// Generate matrix representation of the plaintext.
const plaintextMatrix = genMatrix(plaintext);
const det = determinant(keyMatrix, N);
if ((det % 2 === 0) || (det % 13 === 0))
throw new OperationError("Determinant has common factors with the modular base.");
if (det === 0)
throw new OperationError("Determinant is 0");
return join(multiply(keyMatrix, plaintextMatrix));
}

View file

@ -20,8 +20,8 @@ class HillCipherDecode extends Operation {
this.name = "Hill Cipher Decode";
this.module = "Crypto";
this.description = "";
this.infoURL = "";
this.description = "The Hill cipher is a polygraphic substitution cipher based on linear algebra. Invented by Lester S. Hill in 1929, it was the first polygraphic cipher in which it was practical (though barely) to operate on more than three symbols at once.";
this.infoURL = "https://wikipedia.org/wiki/Hill_cipher";
this.inputType = "string";
this.outputType = "string";
this.args = [
@ -39,7 +39,7 @@ class HillCipherDecode extends Operation {
* @returns {string}
*/
run(input, args) {
const key = args[0].toLowerCase();
let key = args[0].toLowerCase();
input = input.toLowerCase();
if (input.length === 0 || key.length === 0)
return "";
@ -47,6 +47,10 @@ class HillCipherDecode extends Operation {
while (input.indexOf(" ") !== -1)
input = input.replace(" ", "");
while (key.indexOf(" ") !== -1)
key = key.replace(" ", "");
return HillCipher.decode(input, key);
}

View file

@ -20,8 +20,8 @@ class HillCipherEncode extends Operation {
this.name = "Hill Cipher Encode";
this.module = "Crypto";
this.description = "";
this.infoURL = "";
this.description = "The Hill cipher is a polygraphic substitution cipher based on linear algebra. Invented by Lester S. Hill in 1929, it was the first polygraphic cipher in which it was practical (though barely) to operate on more than three symbols at once.";
this.infoURL = "https://wikipedia.org/wiki/Hill_cipher";
this.inputType = "string";
this.outputType = "string";
this.args = [
@ -40,7 +40,7 @@ class HillCipherEncode extends Operation {
*/
run(input, args) {
const key = args[0].toLowerCase();
let key = args[0].toLowerCase();
input = input.toLowerCase();
// The algorithm has to have a non-empty input and a non-empty key.
@ -51,6 +51,9 @@ class HillCipherEncode extends Operation {
while (input.indexOf(" ") !== -1)
input = input.replace(" ", "");
while (key.indexOf(" ") !== -1)
key = key.replace(" ", "");
return HillCipher.encode(input, key);
}

View file

@ -96,6 +96,7 @@ import "./tests/DefangIP.mjs";
import "./tests/ParseUDP.mjs";
import "./tests/AvroToJSON.mjs";
import "./tests/Lorenz.mjs";
import "./tests/HillCipher.mjs";
// Cannot test operations that use the File type yet

View file

@ -0,0 +1,47 @@
/**
* HillCipher tests
*
* @author n1073645 [n1073645@gmail.com]
* @copyright Crown Copyright 2020
* @license Apache-2.0
*/
import TestRegister from "../../lib/TestRegister.mjs";
TestRegister.addTests([
{
name: "Hill Cipher Encode",
input: "Hello World",
expectedOutput: "tutrquwdnv",
recipeConfig: [
{ "op": "Hill Cipher Encode",
"args": ["Test"] }
]
},
{
name: "Hill Cipher Decode",
input: "tutrquwdnv",
expectedOutput: "helloworld",
recipeConfig: [
{ "op": "Hill Cipher Decode",
"args": ["Test"] }
]
},
{
name: "Hill Cipher Bad Matrix",
input: "Hello World",
expectedOutput: "Determinant has common factors with the modular base.",
recipeConfig: [
{ "op": "Hill Cipher Encode",
"args": ["th"] }
]
},
{
name: "Hill Cipher Empty Key",
input: "Hello World",
expectedOutput: "",
recipeConfig: [
{ "op": "Hill Cipher Encode",
"args": [""] }
]
}
]);