Golang : Rot13 and Rot5 algorithms example
Continuing from our previous ROT47 tutorial, we will now learn how to implement the ROT13 + ROT5 algorithms. ROT13 basically rotates a character by 13 places, ‘A’ to ‘N’, ‘B’ to ‘M’ and so on. The ROT5, rotates the digits: ‘0’ to ‘5’, ‘1’ to ‘6’ and so on.
ROT13 is often combined with ROT5, which is used to encode and decode digits i.e. 0-9 and sometimes is also known as ROT18. The following is an implementation done in Golang.
Here you go!
package main
import (
"fmt"
"unicode"
)
// rot13(alphabets) + rot5(numeric)
func rot13rot5(input string) string {
var result []rune
rot5map := map[rune]rune{'0': '5', '1': '6', '2': '7', '3': '8', '4': '9', '5': '0', '6': '1', '7': '2', '8': '3', '9': '4'}
for _, i := range input {
switch {
case !unicode.IsLetter(i) && !unicode.IsNumber(i):
result = append(result, i)
case i >= 'A' && i <= 'Z':
result = append(result, 'A'+(i-'A'+13)%26)
case i >= 'a' && i <= 'z':
result = append(result, 'a'+(i-'a'+13)%26)
case i >= '0' && i <= '9':
result = append(result, rot5map[i])
case unicode.IsSpace(i):
result = append(result, ' ')
}
}
return fmt.Sprintf(string(result[:]))
}
func main() {
text := "ROT18 = ROT13+ROT5. The ROT13 (Caesar cipher by 13 chars) is often combined with ROT5. ROT13 to handle alphabets and ROT5 to handle digits."
fmt.Println(text)
fmt.Println(rot13rot5(text))
fmt.Println("Invertible test:")
fmt.Println(rot13rot5(rot13rot5(text)))
}
Output:
ROT18 = ROT13+ROT5. The ROT13 (Caesar cipher by 13 chars) is often combined with ROT5. ROT13 to handle alphabets and ROT5 to handle digits.
EBG63 = EBG68+EBG0. Gur EBG68 (Pnrfne pvcure ol 68 punef) vf bsgra pbzovarq jvgu EBG0. EBG68 gb unaqyr nycunorgf naq EBG0 gb unaqyr qvtvgf.
Invertible test:
ROT18 = ROT13+ROT5. The ROT13 (Caesar cipher by 13 chars) is often combined with ROT5. ROT13 to handle alphabets and ROT5 to handle digits.
Happy coding!
See also : Golang : ROT47 (Caesar cipher by 47 characters) example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9k Golang : Serving HTTP and Websocket from different ports in a program example
+4.4k Javascript : Detect when console is activated and do something about it
+79k Golang : How to return HTTP status code?
+25.1k Golang : Generate MD5 checksum of a file
+4.9k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+19.9k Golang : Determine if directory is empty with os.File.Readdir() function
+6.7k Golang : Pat multiplexer routing example
+8.4k Python : Fix SyntaxError: Non-ASCII character in file, but no encoding declared
+8.1k Golang : Qt splash screen with delay example
+7.8k Golang : What fmt.Println() can do and println() cannot do
+19.7k Golang : Count JSON objects and convert to slice/array
+8.2k Golang : Implementing class(object-oriented programming style)