CyberChef icon indicating copy to clipboard operation
CyberChef copied to clipboard

Bug report: Utils.modInv assumes modulus of 26

Open profbbrown opened this issue 8 months ago • 1 comments

Describe the bug The Utils.modInv function internally uses 26 as the modulus rather than the argument y. The bug is not externally visible because it's used in the Affine Decode operation, which uses an alphabet with 26 letters.

Source code

static modInv(x, y) {
    x %= y;
    for (let i = 1; i < y; i++) {
        if ((x * i) % 26 === 1) {
            return i;
        }
    }
}

Remedy Replace 26 with y.

Better still: replace the entire function with one that uses the Extended Euclidean Algorithm to compute the modular multiplicative inverse. It will be faster than a brute-force loop for large values of y.

profbbrown avatar Jun 13 '24 20:06 profbbrown