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 RailFenceCipherEncode when the 'offset' argument is negative. Offset must be a non-negative integer because it shifts the zigzag starting position.

Source

Thrown at src/core/operations/RailFenceCipherEncode.mjs:57

    }

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

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

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

        const cycle = (key - 1) * 2;
        const rows = new Array(key).fill("");

        for (let pos = 0; pos < plaintext.length; pos++) {
            const rowIdx = key - 1 - Math.abs(cycle / 2 - (pos + offset) % cycle);

            rows[rowIdx] += plaintext[pos];
        }

        return rows.join("");
    }

}

export default RailFenceCipherEncode;

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Set offset to 0 or a positive integer.
  2. Use the same offset used when decoding.
  3. Clamp with Math.max(0, offset).

Example fix

// before
//   offset: -1
// 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 { encode(input, { offset }); } catch (e) { if (/Offset/.test(e.message)) offset = 0; else throw e; }

Prevention

When it happens

Trigger: Negative offset value; recipe/config supplying a negative number.

Common situations: Sign typo; mismatched offset between encode/decode; imported recipe with bad default.

Related errors


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