From 34cd4af5f5205f1d28fa0b98c0a8c127b0c3c869 Mon Sep 17 00:00:00 2001 From: n1073645 Date: Fri, 14 Feb 2020 10:46:29 +0000 Subject: [PATCH] Hill Cipher tests and info added --- src/core/lib/HillCipher.mjs | 8 ++++ src/core/operations/HillCipherDecode.mjs | 10 +++-- src/core/operations/HillCipherEncode.mjs | 9 +++-- tests/operations/index.mjs | 1 + tests/operations/tests/HillCipher.mjs | 47 ++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 tests/operations/tests/HillCipher.mjs diff --git a/src/core/lib/HillCipher.mjs b/src/core/lib/HillCipher.mjs index 0bc41eb8..3d91cb9b 100644 --- a/src/core/lib/HillCipher.mjs +++ b/src/core/lib/HillCipher.mjs @@ -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)); } diff --git a/src/core/operations/HillCipherDecode.mjs b/src/core/operations/HillCipherDecode.mjs index 97a38c13..a46292b1 100644 --- a/src/core/operations/HillCipherDecode.mjs +++ b/src/core/operations/HillCipherDecode.mjs @@ -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); } diff --git a/src/core/operations/HillCipherEncode.mjs b/src/core/operations/HillCipherEncode.mjs index 87fcb72e..08fb1c7e 100644 --- a/src/core/operations/HillCipherEncode.mjs +++ b/src/core/operations/HillCipherEncode.mjs @@ -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); } diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index bf440414..3f519724 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -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 diff --git a/tests/operations/tests/HillCipher.mjs b/tests/operations/tests/HillCipher.mjs new file mode 100644 index 00000000..0cac47c3 --- /dev/null +++ b/tests/operations/tests/HillCipher.mjs @@ -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": [""] } + ] + } +]);