gchq / CyberChef

The Cyber Swiss Army Knife - a web app for encryption, encoding, compression and data analysis
https://gchq.github.io/CyberChef
Apache License 2.0
29.28k stars 3.28k forks source link

Bug report: Utils.modInv assumes modulus of 26 #1835

Open profbbrown opened 5 months ago

profbbrown commented 5 months ago

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 commented 5 months ago

Pull request #1836