TheAlgorithms/Go · error

failed encipher: %w

Error message

failed encipher: %w

What it means

Polybius.Encrypt wraps a per-character encipher failure (usually the character not present in the key) with fmt.Errorf %w, aborting the whole message and preserving the underlying error for errors.Is/As.

Source

Thrown at cipher/polybius/polybius.go:57

	for i, r := range chars {
		if strings.ContainsRune(chars[i+1:], r) {
			return nil, fmt.Errorf("%q contains same character %q", chars[i+1:], r)
		}
	}

	if len(key) != size*size {
		return nil, fmt.Errorf("len(key): %d must be as long as size squared: %d", len(key), size*size)
	}
	return &Polybius{size, chars, key}, nil
}

// Encrypt encrypts with polybius encryption
func (p *Polybius) Encrypt(text string) (string, error) {
	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

View on GitHub (pinned to 5ba447ec5f)

Solutions

  1. Unwrap with errors.Unwrap or errors.Is to reach the underlying cause
  2. Filter or map unsupported characters before encrypting
  3. Encrypt character-by-character to isolate and skip bad symbols
Defensive patterns

Strategy: fallback

When it happens

Trigger: Thrown at cipher/polybius/polybius.go:57 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/b06adafa48371881. Report an issue: GitHub.