TheAlgorithms/Go · error

missing Key

Error message

missing Key

What it means

ErrKeyMissing is returned by the transposition cipher when the key length is zero or negative, meaning no usable key was supplied. The key length drives the column permutation, so an empty key makes encryption/decryption impossible. It is returned before any text processing happens.

Source

Thrown at cipher/transposition/transposition.go:20

// description: Transposition cipher
// details:
// Implementation "Transposition cipher" is a method of encryption by which the positions held by units of plaintext (which are commonly characters or groups of characters) are shifted according to a regular system, so that the ciphertext constitutes a permutation of the plaintext [Transposition cipher](https://en.wikipedia.org/wiki/Transposition_cipher)
// time complexity: O(n)
// space complexity: O(n)
// author(s) [red_byte](https://github.com/i-redbyte)
// see transposition_test.go

package transposition

import (
	"errors"
	"fmt"
	"sort"
	"strings"
)

var ErrNoTextToEncrypt = errors.New("no text to encrypt")
var ErrKeyMissing = errors.New("missing Key")

const placeholder = ' '

func getKey(keyWord string) []int {
	keyWord = strings.ToLower(keyWord)
	word := []rune(keyWord)
	var sortedWord = make([]rune, len(word))
	copy(sortedWord, word)
	sort.Slice(sortedWord, func(i, j int) bool { return sortedWord[i] < sortedWord[j] })
	usedLettersMap := make(map[rune]int)
	wordLength := len(word)
	resultKey := make([]int, wordLength)
	for i := 0; i < wordLength; i++ {
		char := word[i]
		numberOfUsage := usedLettersMap[char]
		resultKey[i] = getIndex(sortedWord, char) + numberOfUsage + 1 //+1 -so that indexing does not start at 0
		numberOfUsage++
		usedLettersMap[char] = numberOfUsage

View on GitHub (pinned to 5ba447ec5f)

Solutions

  1. Validate the key is a non-empty, non-whitespace string before calling Encrypt/Decrypt.
  2. Load the key from config/env with an explicit empty check at startup.
  3. Compare with errors.Is(err, transposition.ErrKeyMissing) and fail fast with a 'key required' configuration error.

Example fix

// before
result, err := transposition.Encrypt(os.Getenv("CIPHER_KEY"), text) // empty env var -> ErrKeyMissing
// after
key := os.Getenv("CIPHER_KEY")
if len(strings.TrimSpace(key)) == 0 { return nil, errors.New("CIPHER_KEY must be set") }
result, err := transposition.Encrypt(key, text)
Defensive patterns

Strategy: validation

Validate before calling

func hasKey(key string) bool {
    return len(strings.TrimSpace(key)) > 0
}

Try / catch

out, err := transposition.Encrypt(key, text)
if errors.Is(err, transposition.ErrKeyMissing) {
    return fmt.Errorf("transposition key missing: %w", err)
}

Prevention

When it happens

Trigger: Encrypt(key, text) or Decrypt(key, text) with an empty string key (keyLength == 0); Decrypt also returns it when the derived numeric key slice is empty.

Common situations: Config value for the cipher key left empty (missing env var, blank YAML field), or a key that is only whitespace producing an empty key word.

Related errors


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