TheAlgorithms/Go · error

%q does not exist in keys

Error message

%q does not exist in keys

What it means

encipher looks up the character with strings.IndexRune in the cipher key; index -1 means the character (e.g. a letter excluded from the square, or a digit/symbol) cannot be encoded, so it names the offending rune.

Source

Thrown at cipher/polybius/polybius.go:81

// 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) {
	if len(chars) != 2 {
		return "", fmt.Errorf("the size of \"chars\" must be even")
	}
	row := strings.IndexRune(p.characters, chars[0])
	if row < 0 {
		return "", fmt.Errorf("%c does not exist in characters", chars[0])
	}
	col := strings.IndexRune(p.characters, chars[1])
	if col < 0 {
		return "", fmt.Errorf("%c does not exist in characters", chars[1])

View on GitHub (pinned to 5ba447ec5f)

Solutions

  1. Remove or transliterate unsupported characters before encrypting
  2. Normalize text to uppercase and merge I/J to match the square's alphabet
  3. Extend the key alphabet to cover needed characters
Defensive patterns

Strategy: type-guard

When it happens

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