TheAlgorithms/Go · error
no text to encrypt
Error message
no text to encrypt
What it means
ErrNoTextToEncrypt is returned by the transposition cipher when there is no text to process or the text ends with the placeholder space character. It is used both by Encrypt (empty input, or input whose last char is ' ' which would corrupt the padding scheme) and by Decrypt (empty input). Errors.Is-compatible wrapping adds context via fmt.Errorf %w.
Source
Thrown at cipher/transposition/transposition.go:19
// transposition.go
// 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++View on GitHub (pinned to 5ba447ec5f)
Solutions
- Check len(strings.TrimSpace(text)) > 0 and text does not end with ' ' before calling Encrypt/Decrypt.
- Trim or normalize trailing whitespace from input data.
- Handle the error with errors.Is(err, transposition.ErrNoTextToEncrypt) and return a friendly 'empty input' message.
Example fix
// before
result, err := transposition.Encrypt(key, text) // panics-free but errors when text ends with ' '
// after
text = strings.TrimRight(text, " ")
if len(text) == 0 { return nil, errors.New("no input") }
result, err := transposition.Encrypt(key, text) Defensive patterns
Strategy: validation
Validate before calling
func safeTextForTransposition(text string) bool {
return len(text) > 0 && !strings.HasSuffix(text, " ")
} Try / catch
out, err := transposition.Encrypt(key, text)
if errors.Is(err, transposition.ErrNoTextToEncrypt) {
return fmt.Errorf("cannot process empty/trailing-space text: %w", err)
} Prevention
- TrimRight trailing spaces before encrypting.
- Reject empty user input at the UI layer.
- Remember ' ' is the padding placeholder and cannot end plaintext.
When it happens
Trigger: Encrypt with an empty string or textLength <= 0; Encrypt with text ending in ' ' (the placeholder); Decrypt with empty input text.
Common situations: Reading empty user input or an empty file/string before encrypting; data that naturally ends with a trailing space (e.g. sentence joined from split fields) gets rejected because ' ' is reserved as padding.
Related errors
AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02).
Data as JSON: /api/errors/1afe31ac816212c9.
Report an issue: GitHub.