{"record":{"id":"9314d6610f9344d2","repo":"TheAlgorithms/Go","slug":"failed-to-encrypt","errorCode":null,"errorMessage":"failed to Encrypt","messagePattern":"failed to Encrypt","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cipher/rsa/rsa.go","lineNumber":24,"sourceCode":"// thus both the Encrypt and Decrypt are not a production\n// ready implementation. The OpenSSL implementation of RSA\n// also adds a padding which is not present in this algorithm.\n// 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}","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/cipher/rsa/rsa.go#L6-L42","documentation":"ErrorFailedToEncrypt is a sentinel error returned by cipher/rsa/rsa.go Encrypt when the underlying modular exponentiation (math/modular.Exponentiation) fails for any character of the message. The library aborts the whole encryption and returns this opaque sentinel, discarding the original error. It exists to signal RSA encryption failure without exposing internal math details.","triggerScenarios":"Calling Encrypt(message, publicExponent, modulus) where modular.Exponentiation fails for a letter — e.g. modulus <= 0, modulus == 1, or invalid exponent/modulus values passed by the caller.","commonSituations":"Passing a zero or negative modulus (p*q when a prime was computed as 0), swapping argument order (exponent and modulus), or feeding a public exponent of 0 — all make Exponentiation return an error for the first character.","solutions":["Verify modulus > 1 and publicExponent > 0 before calling Encrypt (RSA needs n=p*q with p,q primes, e coprime to (p-1)(q-1)).","Check argument order: Encrypt(message, publicExponent, modulus).","Compare the returned error against cipher/rsa.ErrorFailedToEncrypt with errors.Is and re-derive keys if it fires."],"exampleFix":"// before\nencrypted, err := rsa.Encrypt(msg, 0, 0) // fails: invalid modulus/exponent\n// after\nif modulus > 1 && publicExponent > 0 {\n    encrypted, err = rsa.Encrypt(msg, publicExponent, modulus)\n}","handlingStrategy":"validation","validationCode":"func canEncrypt(publicExponent, modulus int64) bool {\n    return publicExponent > 0 && modulus > 1\n}","typeGuard":null,"tryCatchPattern":"encrypted, err := rsa.Encrypt(msg, e, n)\nif err != nil {\n    if errors.Is(err, rsa.ErrorFailedToEncrypt) {\n        return fmt.Errorf(\"encrypt failed, check keys: %w\", err)\n    }\n    return err\n}","preventionTips":["Validate modulus > 1 and exponent > 0 before each Encrypt call.","Keep p and q as verified primes and recompute n = p*q.","Never reorder the (publicExponent, modulus) arguments."],"tags":["rsa","encryption","go"],"backgroundTag":"rsa-encryption-failed","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}