gchq/CyberChef · error · OperationError

Offset has to be a positive integer

Error message

Offset has to be a positive integer

What it means

Thrown by RailFenceCipherDecode when the 'offset' argument is negative. Offset shifts where the zigzag pattern starts and must be a non-negative integer.

Source

Thrown at src/core/operations/RailFenceCipherDecode.mjs:58

    /**
     * @param {string} input
     * @param {Object[]} args
     * @returns {string}
     */
    run(input, args) {
        const [key, offset] = args;

        const cipher = input;

        if (key < 2) {
            throw new OperationError("Key has to be bigger than 2");
        } else if (key > cipher.length) {
            throw new OperationError("Key should be smaller than the cipher's length");
        }

        if (offset < 0) {
            throw new OperationError("Offset has to be a positive integer");
        }

        const cycle = (key - 1) * 2;
        const plaintext = new Array(cipher.length);

        let j = 0;
        let x, y;

        for (y = 0; y < key; y++) {
            for (x = 0; x < cipher.length; x++) {
                if ((y + x + offset) % cycle === 0 || (y - x - offset) % cycle === 0) {
                    plaintext[x] = cipher[j++];
                }
            }
        }

        return plaintext.join("");
    }

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Set offset to 0 or a positive integer.
  2. Ensure the offset matches the value used during encoding.
  3. Coerce the input: offset = Math.max(0, offset).

Example fix

// before
//   offset: -2
// after
//   offset: 0
Defensive patterns

Strategy: validation

Validate before calling

if (!Number.isInteger(offset) || offset < 0) throw new Error('Offset must be a non-negative integer');

Type guard

const isOffset = o => Number.isInteger(o) && o >= 0;

Try / catch

try { decode(input, { offset }); } catch (e) { if (/Offset/.test(e.message)) offset = Math.max(0, offset); else throw e; }

Prevention

When it happens

Trigger: Entering a negative offset; a recipe/config that supplies -1 or a computed negative value.

Common situations: Typo/sign error; importing a recipe whose offset defaulted to a negative; mismatching offset between encode and decode.

Related errors


AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13). Data as JSON: /api/errors/bf6da0cd16b8fd30. Report an issue: GitHub.