Golang : ROT47 (Caesar cipher by 47 characters) example
Here is an example of how to implement the ROT47 - Rotate 47 characters algorithm in Golang. ROT47 is a character substitution cipher that replaces a character within the ASCII range [33, 126] with the character 47. It is also an invertible algorithm where applying the algorithm twice will return the original text.
Here you go!
package main
import (
"fmt"
"strings"
)
func rot47(input string) string {
var result []string
for i := range input[:len(input)] {
j := int(input[i])
if (j >= 33) && (j <= 126) {
result = append(result, string(rune(33+((j+14)%94))))
} else {
result = append(result, string(input[i]))
}
}
return strings.Join(result, "")
}
func main() {
text := "ROT47 makes text unreadable and it is also invertible algorithm!"
fmt.Println("ROT47 : ")
fmt.Println(text)
fmt.Println(rot47(text))
// invertible algorithm, apply the same algorithm twice to get the original text
fmt.Println(rot47(rot47(text)))
}
Output:
ROT47 :
ROT47 makes text unreadable and it is also invertible algorithm!
#~%cf >2<6D E6IE F?C62523=6 2?5 :E :D 2=D@ :?G6CE:3=6 2=8@C:E9>P
ROT47 makes text unreadable and it is also invertible algorithm!
See also : Golang : Reverse text lines or flip line order 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
+4.8k Unix/Linux : How to pipe/save output of a command to file?
+10.8k Golang : Resolve domain name to IP4 and IP6 addresses.
+14.7k Android Studio : Use image as AlertDialog title with custom layout example
+10.3k Golang : How to get quoted string into another string?
+28.9k Golang : Detect (OS) Operating System
+9.6k Android Studio : Indicate progression with ProgressBar example
+10.8k Android Studio : Simple input textbox and intercept key example
+13.8k Golang : Tutorial on loading GOB and PEM files
+22.1k Golang : Use TLS version 1.2 and enforce server security configuration over client
+11.8k Golang : Find age or leap age from date of birth example
+7.1k Golang : constant 20013 overflows byte error message
+5.4k Golang : Generate Interleaved 2 inch by 5 inch barcode