{"record":{"id":"7251b9bc555a2217","repo":"TheAlgorithms/Go","slug":"failed-to-decrypt","errorCode":null,"errorMessage":"failed to Decrypt","messagePattern":"failed to Decrypt","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cipher/rsa/rsa.go","lineNumber":27,"sourceCode":"// time complexity: O(n)\n// space complexity: O(n)\n// author(s) [Taj](https://github.com/tjgurwara99)\n// see rsa_test.go\n\n// Package rsa shows a simple implementation of RSA algorithm\npackage rsa\n\nimport (\n\t\"errors\"\n\n\tmodular \"github.com/TheAlgorithms/Go/math/modular\"\n)\n\n// ErrorFailedToEncrypt Raised when Encrypt function fails to encrypt the message\nvar ErrorFailedToEncrypt = errors.New(\"failed to Encrypt\")\n\n// ErrorFailedToDecrypt Raised when Decrypt function fails to decrypt the encrypted message\nvar ErrorFailedToDecrypt = errors.New(\"failed to Decrypt\")\n\n// Encrypt encrypts based on the RSA algorithm - uses modular exponentitation in math directory\nfunc Encrypt(message []rune, publicExponent, modulus int64) ([]rune, error) {\n\tvar encrypted []rune\n\n\tfor _, letter := range message {\n\t\tencryptedLetter, err := modular.Exponentiation(int64(letter), publicExponent, modulus)\n\t\tif err != nil {\n\t\t\treturn nil, ErrorFailedToEncrypt\n\t\t}\n\t\tencrypted = append(encrypted, rune(encryptedLetter))\n\t}\n\n\treturn encrypted, nil\n}\n\n// Decrypt decrypts encrypted rune slice based on the RSA algorithm\nfunc Decrypt(encrypted []rune, privateExponent, modulus int64) (string, error) {","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/cipher/rsa/rsa.go#L9-L45","documentation":"ErrorFailedToDecrypt is a sentinel error returned by cipher/rsa/rsa.go Decrypt when modular.Exponentiation fails while decrypting a character. Like its Encrypt counterpart, it discards the cause and signals only that decryption failed. It is raised per-character inside the decryption loop and aborts the whole call.","triggerScenarios":"Calling Decrypt(ciphertext, privateExponent, modulus) where modular.Exponentiation errors — typically modulus <= 0, modulus == 1, or a privateExponent of 0/negative for some rune in the input.","commonSituations":"Mismatched key material (private exponent not the inverse of e mod (p-1)(q-1)), modulus below 2, or ciphertext produced with a different modulus than the one passed to Decrypt.","solutions":["Ensure modulus > 1 and privateExponent > 0 before calling Decrypt.","Use the matching key pair: private exponent must correspond to the public exponent/modulus used in Encrypt.","Compare with errors.Is(rsa.ErrorFailedToDecrypt) and regenerate the RSA key pair if the exponent/modulus are inconsistent."],"exampleFix":"// before\ndecrypted, err := rsa.Decrypt(cipher, 0, modulus) // fails: privateExponent=0\n// after\nif privateExponent > 0 && modulus > 1 {\n    decrypted, err = rsa.Decrypt(cipher, privateExponent, modulus)\n}","handlingStrategy":"validation","validationCode":"func canDecrypt(privateExponent, modulus int64) bool {\n    return privateExponent > 0 && modulus > 1\n}","typeGuard":null,"tryCatchPattern":"decrypted, err := rsa.Decrypt(ciphertext, d, n)\nif err != nil {\n    if errors.Is(err, rsa.ErrorFailedToDecrypt) {\n        return fmt.Errorf(\"decrypt failed, verify key pair: %w\", err)\n    }\n    return err\n}","preventionTips":["Ensure private exponent d is derived from the same p, q, e used for encryption.","Check modulus > 1 and d > 0 before Decrypt.","Store key material atomically so exponent and modulus never mismatch."],"tags":["rsa","decryption","go"],"backgroundTag":"rsa-decryption-failed","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}