From 7ae35bc587d4090a51b71267b38c0beeeb64c57f Mon Sep 17 00:00:00 2001 From: Barry Brown Date: Thu, 13 Jun 2024 17:32:21 +0000 Subject: [PATCH] Uses new Utils.modInv function --- src/core/lib/Ciphers.mjs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/core/lib/Ciphers.mjs b/src/core/lib/Ciphers.mjs index 9f8aa410..9ab5e811 100644 --- a/src/core/lib/Ciphers.mjs +++ b/src/core/lib/Ciphers.mjs @@ -86,7 +86,7 @@ export function affineApplication(input, a, b, alphabet, affineFn) { } if (Utils.gcd(a, modulus) !== 1) { - throw new OperationError("The value of `a` must be coprime to " + modulus + "."); + throw new OperationError("The value of `a` (" + a + ") must be coprime to " + modulus + "."); } // Apply affine function to each character in the input @@ -168,9 +168,10 @@ export function affineDecrypt(input, a, b, alphabet="a-z") { const m = Utils.expandAlphRange(alphabet).length; if (Utils.gcd(a, m) !== 1) throw new OperationError("The value of `a` (" + a + ") must be coprime to " + m + "."); + const aInv = Utils.modInv(a, m); const bInv = (m - b) % m; - if (aInv === undefined) + if (aInv === null) throw new OperationError("The value of `a` (" + a + ") must be coprime to " + m + "."); else return affineApplication(input, aInv, bInv, alphabet, decryptFn); }