{"record":{"id":"f562d998281c731c","repo":"TheAlgorithms/Python","slug":"determinant-modular-req-l-of-encryption-key-det","errorCode":null,"errorMessage":"determinant modular {req_l} of encryption key({det}) is not co prime w.r.t {req_l}.\nTry another key.","messagePattern":"determinant modular (.+?) of encryption key\\((.+?)\\) is not co prime w\\.r\\.t (.+?)\\.\nTry another key\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"ciphers/hill_cipher.py","lineNumber":102,"sourceCode":"        return self.key_string[int(num)]\n\n    def check_determinant(self) -> None:\n        \"\"\"\n        >>> hill_cipher = HillCipher(np.array([[2, 5], [1, 6]]))\n        >>> hill_cipher.check_determinant()\n        \"\"\"\n        det = round(np.linalg.det(self.encrypt_key))\n\n        if det < 0:\n            det = det % len(self.key_string)\n\n        req_l = len(self.key_string)\n        if greatest_common_divisor(det, len(self.key_string)) != 1:\n            msg = (\n                f\"determinant modular {req_l} of encryption key({det}) \"\n                f\"is not co prime w.r.t {req_l}.\\nTry another key.\"\n            )\n            raise ValueError(msg)\n\n    def process_text(self, text: str) -> str:\n        \"\"\"\n        >>> hill_cipher = HillCipher(np.array([[2, 5], [1, 6]]))\n        >>> hill_cipher.process_text('Testing Hill Cipher')\n        'TESTINGHILLCIPHERR'\n        >>> hill_cipher.process_text('hello')\n        'HELLOO'\n        \"\"\"\n        chars = [char for char in text.upper() if char in self.key_string]\n\n        last = chars[-1]\n        while len(chars) % self.break_key != 0:\n            chars.append(last)\n\n        return \"\".join(chars)\n\n    def encrypt(self, text: str) -> str:","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/ciphers/hill_cipher.py#L84-L120","documentation":"Raised by HillCipher's key validation (ciphers/hill_cipher.py) when det(encrypt_key) is not coprime with the alphabet length (len(key_string), 85 for the default charset). The matrix inverse needed for decryption only exists mod L when gcd(det, L) == 1, so such a key cannot decrypt anything it encrypts.","triggerScenarios":"HillCipher(np.array([[2, 4], [4, 8]])) — det 0; any integer matrix whose determinant shares a factor with 85 (5 or 17); random key matrices that fail the coprimality test with probability ~1 - phi(85)/85.","commonSituations":"Generating random keys without re-checking the determinant; scaling a valid key by an integer (multiplies det, usually breaks coprimality); switching alphabet/key_string without re-validating keys.","solutions":["Try a different key matrix, e.g. the doctest-valid [[2, 5], [1, 6]] (det 7, coprime with 85)","Generate random integer matrices and retry until gcd(round(det), 85) == 1","For custom key_string of length L, enforce gcd(det, L) == 1 in your key generator"],"exampleFix":"# before\nkey = np.array([[2, 4], [4, 8]])  # det = 0\nhc = HillCipher(key)\n\n# after\nimport numpy as np\nfrom maths.greatest_common_divisor import gcd_by_iterative as gcd\nwhile True:\n    key = np.random.randint(0, 85, (2, 2))\n    det = round(np.linalg.det(key))\n    if gcd(det, 85) == 1:\n        break\nhc = HillCipher(key)","handlingStrategy":"retry","validationCode":"import numpy as np\nfrom maths.greatest_common_divisor import gcd_by_iterative as gcd\nL = 85  # default key_string length\ndet = round(np.linalg.det(key))\nassert gcd(det, L) == 1, f'key determinant {det} not coprime with {L}'","typeGuard":null,"tryCatchPattern":"while True:\n    key = np.random.randint(0, 85, (n, n))\n    det = round(np.linalg.det(key))\n    if gcd(det, 85) == 1:\n        break\nhc = HillCipher(key)","preventionTips":["Check gcd(round(det), len(key_string)) == 1 before constructing HillCipher","Never scale a valid key matrix by an integer — it usually breaks coprimality","Use retry loops when generating random keys; many matrices fail the test"],"tags":["hill-cipher","matrix","linear-algebra","validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}