From a09f8451fdaf7d35880b694a6b432c2f59d863b1 Mon Sep 17 00:00:00 2001 From: Ethan Harvey <57858587+EvieHarv@users.noreply.github.com> Date: Wed, 10 Apr 2024 23:19:50 +0000 Subject: [PATCH 1/2] Require (a, 26) to be coprime in affine encode --- src/core/lib/Ciphers.mjs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/core/lib/Ciphers.mjs b/src/core/lib/Ciphers.mjs index a4d174b5..6266a8e1 100644 --- a/src/core/lib/Ciphers.mjs +++ b/src/core/lib/Ciphers.mjs @@ -3,6 +3,7 @@ * * @author Matt C [matt@artemisbot.uk] * @author n1474335 [n1474335@gmail.com] + * @author Evie H [evie@evie.sh] * * @copyright Crown Copyright 2018 * @license Apache-2.0 @@ -10,6 +11,7 @@ */ import OperationError from "../errors/OperationError.mjs"; +import Utils from "../Utils.mjs"; import CryptoJS from "crypto-js"; /** @@ -30,6 +32,10 @@ export function affineEncode(input, args) { throw new OperationError("The values of a and b can only be integers."); } + if (Utils.gcd(a, 26) !== 1) { + throw new OperationError("The value of `a` must be coprime to 26."); + } + for (let i = 0; i < input.length; i++) { if (alphabet.indexOf(input[i]) >= 0) { // Uses the affine function ax+b % m = y (where m is length of the alphabet) From 00f7914c5cae10680d81c97a679b602ad81acecb Mon Sep 17 00:00:00 2001 From: Ethan Harvey <57858587+EvieHarv@users.noreply.github.com> Date: Wed, 10 Apr 2024 23:57:53 +0000 Subject: [PATCH 2/2] Fix affine encode testcase --- tests/browser/02_ops.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/browser/02_ops.js b/tests/browser/02_ops.js index e2b9b28a..200c0d3f 100644 --- a/tests/browser/02_ops.js +++ b/tests/browser/02_ops.js @@ -37,7 +37,7 @@ module.exports = { testOp(browser, ["From Hex", "Add Text To Image", "To Base64"], Images.PNG_HEX, Images.PNG_CHEF_B64, [[], ["Chef", "Center", "Middle", 0, 0, 16], []]); testOp(browser, "Adler-32 Checksum", "test input", "16160411"); testOp(browser, "Affine Cipher Decode", "test input", "rcqr glnsr", [1, 2]); - testOp(browser, "Affine Cipher Encode", "test input", "njln rbfpn", [2, 1]); + testOp(browser, "Affine Cipher Encode", "test input", "gndg zoujg", [3, 1]); testOp(browser, "AMF Decode", "\u000A\u0013\u0001\u0003a\u0006\u0009test", /"\$value": "test"/); testOp(browser, "AMF Encode", '{"a": "test"}', "\u000A\u0013\u0001\u0003a\u0006\u0009test"); testOp(browser, "Analyse hash", "0123456789abcdef", /CRC-64/);