{"record":{"id":"1afe31ac816212c9","repo":"TheAlgorithms/Go","slug":"no-text-to-encrypt","errorCode":null,"errorMessage":"no text to encrypt","messagePattern":"no text to encrypt","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cipher/transposition/transposition.go","lineNumber":19,"sourceCode":"// transposition.go\n// description: Transposition cipher\n// details:\n// 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)\n// time complexity: O(n)\n// space complexity: O(n)\n// author(s) [red_byte](https://github.com/i-redbyte)\n// see transposition_test.go\n\npackage transposition\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\t\"sort\"\n\t\"strings\"\n)\n\nvar ErrNoTextToEncrypt = errors.New(\"no text to encrypt\")\nvar ErrKeyMissing = errors.New(\"missing Key\")\n\nconst placeholder = ' '\n\nfunc getKey(keyWord string) []int {\n\tkeyWord = strings.ToLower(keyWord)\n\tword := []rune(keyWord)\n\tvar sortedWord = make([]rune, len(word))\n\tcopy(sortedWord, word)\n\tsort.Slice(sortedWord, func(i, j int) bool { return sortedWord[i] < sortedWord[j] })\n\tusedLettersMap := make(map[rune]int)\n\twordLength := len(word)\n\tresultKey := make([]int, wordLength)\n\tfor i := 0; i < wordLength; i++ {\n\t\tchar := word[i]\n\t\tnumberOfUsage := usedLettersMap[char]\n\t\tresultKey[i] = getIndex(sortedWord, char) + numberOfUsage + 1 //+1 -so that indexing does not start at 0\n\t\tnumberOfUsage++","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/cipher/transposition/transposition.go#L1-L37","documentation":"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.","triggerScenarios":"Encrypt with an empty string or textLength <= 0; Encrypt with text ending in ' ' (the placeholder); Decrypt with empty input text.","commonSituations":"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.","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."],"exampleFix":"// before\nresult, err := transposition.Encrypt(key, text) // panics-free but errors when text ends with ' '\n// after\ntext = strings.TrimRight(text, \" \")\nif len(text) == 0 { return nil, errors.New(\"no input\") }\nresult, err := transposition.Encrypt(key, text)","handlingStrategy":"validation","validationCode":"func safeTextForTransposition(text string) bool {\n    return len(text) > 0 && !strings.HasSuffix(text, \" \")\n}","typeGuard":null,"tryCatchPattern":"out, err := transposition.Encrypt(key, text)\nif errors.Is(err, transposition.ErrNoTextToEncrypt) {\n    return fmt.Errorf(\"cannot process empty/trailing-space text: %w\", err)\n}","preventionTips":["TrimRight trailing spaces before encrypting.","Reject empty user input at the UI layer.","Remember ' ' is the padding placeholder and cannot end plaintext."],"tags":["transposition","cipher","go","empty-input"],"backgroundTag":"empty-input-rejected","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}