{"record":{"id":"f7a7d3f6b7d34d54","repo":"gchq/CyberChef","slug":"invalid-key-length-key-length-bytes-present-u-f7a7d3","errorCode":null,"errorMessage":"Invalid key length: ${key.length} bytes\n\nPRESENT uses a key length of 10 bytes (80 bits) or 16 bytes (128 bits).","messagePattern":"Invalid key length: (.+?) bytes\n\nPRESENT uses a key length of 10 bytes \\(80 bits\\) or 16 bytes \\(128 bits\\)\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/PRESENTEncrypt.mjs","lineNumber":77,"sourceCode":"                \"name\": \"Padding\",\n                \"type\": \"option\",\n                \"value\": [\"PKCS5\", \"NO\", \"ZERO\", \"RANDOM\", \"BIT\"]\n            }\n        ];\n    }\n\n    /**\n     * @param {string} input\n     * @param {Object[]} args\n     * @returns {string}\n     */\n    run(input, args) {\n        const key = Utils.convertToByteArray(args[0].string, args[0].option),\n            iv = Utils.convertToByteArray(args[1].string, args[1].option),\n            [,, mode, inputType, outputType, padding] = args;\n\n        if (key.length !== 10 && key.length !== 16)\n            throw new OperationError(`Invalid key length: ${key.length} bytes\n\nPRESENT uses a key length of 10 bytes (80 bits) or 16 bytes (128 bits).`);\n\n        if (iv.length !== 8 && mode !== \"ECB\")\n            throw new OperationError(`Invalid IV length: ${iv.length} bytes\n\nPRESENT uses an IV length of 8 bytes (64 bits).\nMake sure you have specified the type correctly (e.g. Hex vs UTF8).`);\n\n        input = Utils.convertToByteArray(input, inputType);\n        const output = encryptPRESENT(input, key, iv, mode, padding);\n        return outputType === \"Hex\" ? toHex(output, \"\") : Utils.byteArrayToUtf8(output);\n    }\n\n}\n\nexport default PRESENTEncrypt;\n","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/PRESENTEncrypt.mjs#L59-L95","documentation":"Identical validation to error 547 but in the encrypt operation. The PRESENT block cipher requires a key of exactly 10 bytes (80 bits) or 16 bytes (128 bits). The encrypt operation validates the key length before calling encryptPRESENT.","triggerScenarios":"The key argument, after convertToByteArray with the selected encoding option, yields a byte array whose length is not 10 or 16. Encoding mismatch is the most common cause — e.g., a hex key string interpreted as UTF8 doubles the effective byte count.","commonSituations":"User enters a hex key with the encoding option left on UTF8, producing the wrong byte count. User provides a key intended for a different cipher. Multibyte UTF8 characters inflate the byte count unexpectedly.","solutions":["Provide exactly 10 or 16 bytes of key material","Double-check the key encoding option (Hex vs UTF8 vs Base64) matches your key representation","For hex keys, use 20 hex chars (10 bytes) or 32 hex chars (16 bytes) with the Hex option"],"exampleFix":"// before: key=\"aabbccddeeff0011\", option=\"Hex\" -> 8 bytes (FAILS)\n// after:  key=\"aabbccddeeff00112233\", option=\"Hex\" -> 10 bytes (OK for 80-bit)","handlingStrategy":"validation","validationCode":"// Pre-check: validate key length before calling the operation\nconst keyBytes = Utils.convertToByteArray(keyString, keyOption);\nif (keyBytes.length !== 10 && keyBytes.length !== 16) {\n  throw new Error(`Key must be 10 or 16 bytes, got ${keyBytes.length}. Check encoding option.`);\n}","typeGuard":"function isValidPresentKey(keyBytes) {\n  return keyBytes.length === 10 || keyBytes.length === 16;\n}","tryCatchPattern":null,"preventionTips":["Verify the key encoding option matches the key string representation","For hex keys, use 20 or 32 hex characters with the Hex option selected","Account for multibyte UTF8 characters affecting byte count"],"tags":["present","cipher","crypto","key-length","validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}