{"record":{"id":"1285b70c04e226f3","repo":"trekhleb/javascript-algorithms","slug":"invalid-key-string-length-the-square-root-of-the","errorCode":null,"errorMessage":"Invalid key string length. The square root of the key string must be an integer","messagePattern":"Invalid key string length\\. The square root of the key string must be an integer","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/algorithms/cryptography/hill-cipher/hillCipher.js","lineNumber":16,"sourceCode":"const generateKeyMatrix = (keyString) => {\n  const matrixSize = Math.sqrt(keyString.length);\n  if (!Number.isInteger(matrixSize)) {\n    throw new Error(\n      'Invalid key string length. The square root of the key string must be an integer',\n    );\n  }}","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/trekhleb/javascript-algorithms/blob/85293e3e2b88f4d2ce330d956b139cf628aa1e82/src/algorithms/cryptography/hill-cipher/hillCipher.js#L1-L34","documentation":"Error \"Invalid key string length. The square root of the key string must be an integer\" thrown in trekhleb/javascript-algorithms.","triggerScenarios":"hillCipherEncrypt is called with a keyString whose length is not a perfect square (e.g. 5 or 10 characters), so Math.sqrt(keyString.length) is not an integer and no square key matrix can be generated.","commonSituations":"Calling hillCipherEncrypt with a key that cannot form a square matrix; the Hill cipher needs an n x n key matrix, so the key length must be n^2.","solutions":["Pass a keyString whose length is a perfect square (4, 9, 16, 25, ...) so it can fill an n x n key matrix.","Pad or trim the key to the nearest perfect-square length before calling hillCipherEncrypt.","Validate the key up front with Number.isInteger(Math.sqrt(keyString.length)) and reject invalid keys with a clear message."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"85293e3e2b88f4d2ce330d956b139cf628aa1e82","analyzedAt":"2026-08-24T05:59:10.417Z","schemaVersion":2},"datasetVersion":"2026-08-24T07:17:09.176Z"}