TheAlgorithms/Go · error

failed to Decrypt

Error message

failed to Decrypt

What it means

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.

Source

Thrown at cipher/rsa/rsa.go:27

// 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
}

// Decrypt decrypts encrypted rune slice based on the RSA algorithm
func Decrypt(encrypted []rune, privateExponent, modulus int64) (string, error) {

View on GitHub (pinned to 5ba447ec5f)

Solutions

  1. Ensure modulus > 1 and privateExponent > 0 before calling Decrypt.
  2. Use the matching key pair: private exponent must correspond to the public exponent/modulus used in Encrypt.
  3. Compare with errors.Is(rsa.ErrorFailedToDecrypt) and regenerate the RSA key pair if the exponent/modulus are inconsistent.

Example fix

// before
decrypted, err := rsa.Decrypt(cipher, 0, modulus) // fails: privateExponent=0
// after
if privateExponent > 0 && modulus > 1 {
    decrypted, err = rsa.Decrypt(cipher, privateExponent, modulus)
}
Defensive patterns

Strategy: validation

Validate before calling

func canDecrypt(privateExponent, modulus int64) bool {
    return privateExponent > 0 && modulus > 1
}

Try / catch

decrypted, err := rsa.Decrypt(ciphertext, d, n)
if err != nil {
    if errors.Is(err, rsa.ErrorFailedToDecrypt) {
        return fmt.Errorf("decrypt failed, verify key pair: %w", err)
    }
    return err
}

Prevention

When it happens

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

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

Related errors


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