TheAlgorithms/Go · error

failed decipher: %w

Error message

failed decipher: %w

What it means

Polybius.Decrypt wraps a decipher failure for a rune pair (unknown characters or odd-length remainder) with fmt.Errorf %w, returning an empty string and a wrapped error identifying the failing symbol.

Source

Thrown at cipher/polybius/polybius.go:71

	encryptedText := ""
	for _, char := range strings.ToUpper(text) {
		encryptedChar, err := p.encipher(char)
		if err != nil {
			return "", fmt.Errorf("failed encipher: %w", err)
		}
		encryptedText += encryptedChar
	}
	return encryptedText, nil
}

// Decrypt decrypts with polybius encryption
func (p *Polybius) Decrypt(text string) (string, error) {
	chars := []rune(strings.ToUpper(text))
	decryptedText := ""
	for i := 0; i < len(chars); i += 2 {
		decryptedChar, err := p.decipher(chars[i:int(math.Min(float64(i+2), float64(len(chars))))])
		if err != nil {
			return "", fmt.Errorf("failed decipher: %w", err)
		}
		decryptedText += decryptedChar
	}
	return decryptedText, nil
}

func (p *Polybius) encipher(char rune) (string, error) {
	index := strings.IndexRune(p.key, char)
	if index < 0 {
		return "", fmt.Errorf("%q does not exist in keys", char)
	}
	row := index / p.size
	col := index % p.size
	chars := []rune(p.characters)
	return string([]rune{chars[row], chars[col]}), nil
}

func (p *Polybius) decipher(chars []rune) (string, error) {

View on GitHub (pinned to 5ba447ec5f)

Solutions

  1. Unwrap the cause with errors.Is/As before deciding recovery
  2. Validate ciphertext length is even and characters exist in the square before decrypting
  3. Decrypt in pairs to locate the exact bad position
Defensive patterns

Strategy: fallback

When it happens

Trigger: Thrown at cipher/polybius/polybius.go:71 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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