{"record":{"id":"f4cce33d271c83ea","repo":"trekhleb/javascript-algorithms","slug":"invalid-key-string-length-the-key-length-must-be","errorCode":null,"errorMessage":"Invalid key string length. The key length must be a square of message length","messagePattern":"Invalid key string length\\. The key length must be a square of message length","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/algorithms/cryptography/hill-cipher/hillCipher.js","lineNumber":71,"sourceCode":"  // keyString.length must equal to square of message.length\n  if (keyMatrix.length !== message.length) {\n    throw new Error('Invalid key string length. The key length must be a square of message length');\n  }","sourceCodeStart":53,"sourceCodeEnd":88,"githubUrl":"https://github.com/trekhleb/javascript-algorithms/blob/85293e3e2b88f4d2ce330d956b139cf628aa1e82/src/algorithms/cryptography/hill-cipher/hillCipher.js#L53-L88","documentation":"Error \"Invalid key string length. The key length must be a square of message length\" thrown in trekhleb/javascript-algorithms.","triggerScenarios":"hillCipherEncrypt is called with a key matrix size that does not match the message length (keyMatrix.length !== message.length), e.g. a 3x3 key from a 9-character keyString used with a 2-letter message.","commonSituations":"Encrypting a message whose length does not match the Hill cipher key matrix dimension, e.g. a 9-letter key (3 x 3 matrix) with a 5-letter message.","solutions":["Choose a key whose matrix size (square root of the key length) equals the message length.","Pad the message with filler letters (e.g. 'X') so its length matches the key matrix size.","Pick a different key: for a message of length n the key length must be n^2."],"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"}