{"record":{"id":"39d59a1983aee33c","repo":"gchq/CyberChef","slug":"invalid-key-length-key-length-bytes-present-u","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/PRESENTDecrypt.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 = decryptPRESENT(input, key, iv, mode, padding);\n        return outputType === \"Hex\" ? toHex(output, \"\") : Utils.byteArrayToUtf8(output);\n    }\n\n}\n\nexport default PRESENTDecrypt;\n","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/PRESENTDecrypt.mjs#L59-L95","documentation":"The PRESENT block cipher requires a key of exactly 10 bytes (80 bits) or 16 bytes (128 bits) per ISO/IEC 29192-2. The decrypt operation validates this before calling decryptPRESENT and rejects any other key length. The most common cause is an encoding mismatch — the key string has the right character count but decodes to the wrong number of bytes.","triggerScenarios":"The key argument, after convertToByteArray with the selected encoding option, yields a byte array whose length is not 10 or 16. For example, a 20-character hex string (which is 10 bytes decoded) or a 32-character hex string (16 bytes) are correct; a 10-character hex string decodes to only 5 bytes and fails. A UTF8 key of 10 characters is 10 bytes, but a multibyte UTF8 key may have a different byte count.","commonSituations":"User enters a 16-character hex key but leaves the encoding option on 'UTF8', so it becomes 16 bytes instead of the intended 8. Or enters a key that is 8 bytes (for another cipher like DES) by mistake. Or provides an ASCII key where multibyte characters inflate the byte count.","solutions":["Provide exactly 10 or 16 bytes of key material","Double-check the key encoding option (Hex vs UTF8 vs Base64) matches the actual representation of your key","If your key is in hex, ensure it is 20 hex chars (10 bytes) or 32 hex chars (16 bytes)"],"exampleFix":"// Key encoding mismatch:\n// before: key=\"0123456789abcdef\", option=\"UTF8\" -> 16 bytes (OK for 128-bit)\n//         but key=\"0123456789abcdef\", option=\"Hex\" -> 8 bytes (FAILS)\n// after:  key=\"0123456789abcdef0123456789abcdef\", option=\"Hex\" -> 16 bytes (OK)","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":["Always verify the key encoding option (Hex/UTF8/Base64) matches the key string representation","For hex keys, use 20 hex chars (10 bytes) or 32 hex chars (16 bytes)","Remember that multibyte UTF8 characters increase the 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"}