TheAlgorithms/Go · error

failed to Encrypt

Error message

failed to Encrypt

What it means

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.

Source

Thrown at cipher/rsa/rsa.go:24

// thus both the Encrypt and Decrypt are not a production
// ready implementation. The OpenSSL implementation of RSA
// also adds a padding which is not present in this algorithm.
// time complexity: O(n)
// space complexity: O(n)
// author(s) [Taj](https://github.com/tjgurwara99)
// see rsa_test.go

// Package rsa shows a simple implementation of RSA algorithm
package rsa

import (
	"errors"

	modular "github.com/TheAlgorithms/Go/math/modular"
)

// ErrorFailedToEncrypt Raised when Encrypt function fails to encrypt the message
var ErrorFailedToEncrypt = errors.New("failed to Encrypt")

// ErrorFailedToDecrypt Raised when Decrypt function fails to decrypt the encrypted message
var ErrorFailedToDecrypt = errors.New("failed to Decrypt")

// Encrypt encrypts based on the RSA algorithm - uses modular exponentitation in math directory
func Encrypt(message []rune, publicExponent, modulus int64) ([]rune, error) {
	var encrypted []rune

	for _, letter := range message {
		encryptedLetter, err := modular.Exponentiation(int64(letter), publicExponent, modulus)
		if err != nil {
			return nil, ErrorFailedToEncrypt
		}
		encrypted = append(encrypted, rune(encryptedLetter))
	}

	return encrypted, nil
}

View on GitHub (pinned to 5ba447ec5f)

Solutions

  1. 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)).
  2. Check argument order: Encrypt(message, publicExponent, modulus).
  3. Compare the returned error against cipher/rsa.ErrorFailedToEncrypt with errors.Is and re-derive keys if it fires.

Example fix

// before
encrypted, err := rsa.Encrypt(msg, 0, 0) // fails: invalid modulus/exponent
// after
if modulus > 1 && publicExponent > 0 {
    encrypted, err = rsa.Encrypt(msg, publicExponent, modulus)
}
Defensive patterns

Strategy: validation

Validate before calling

func canEncrypt(publicExponent, modulus int64) bool {
    return publicExponent > 0 && modulus > 1
}

Try / catch

encrypted, err := rsa.Encrypt(msg, e, n)
if err != nil {
    if errors.Is(err, rsa.ErrorFailedToEncrypt) {
        return fmt.Errorf("encrypt failed, check keys: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02). Data as JSON: /api/errors/9314d6610f9344d2. Report an issue: GitHub.