Golang : Scramble and unscramble text message by randomly replacing words




For this tutorial, we will learn how to make a text string unreadable by replacing some of the words inside the text randomly; thus, rendering the text string unreadable. Only those with the translation map can unscramble back the replacements and make the text string readable again. In the code example below, the map(hash table) resides in the memory and only available during runtime.

IF you plan to scramble the text inside a file, make sure you serialize the map(hash table) as well into another file via GOB - see https://www.socketloop.com/tutorials/golang-saving-and-reading-file-with-gob on how to do that. You can pass the scrambled text file around and only those with the map GOB file can unscramble it back.

Here you go!


 package main

 import (
 "fmt"
 "math/rand"
 "strings"
 "time"
 )

 func randStr(strSize int, randType string) string {

 var dictionary string

 if randType == "alphanum" {
 dictionary = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
 }

 if randType == "alpha" {
 dictionary = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
 }

 if randType == "number" {
 dictionary = "0123456789"
 }

 var bytes = make([]byte, strSize)
 rand.Read(bytes)
 for k, v := range bytes {
 bytes[k] = dictionary[v%byte(len(dictionary))]
 }
 return string(bytes)
 }

 func main() {
 originalText := `Psssst! Hey you! Yes! You there! This is my secret message to you`

 fmt.Println("Original: ", originalText)

 // now we want to randomly mark some words to be replaced
 var scrambledText, unscrambledText []string

 // convert text to slice
 textSlice := strings.Split(originalText, " ")
 //fmt.Println(textSlice)

 // prepare the dice
 dice := []int{1, 2, 3, 4, 5, 6}
 rand.Seed(time.Now().UnixNano())

 // prepare the translation map
 translateBackMap := map[string]string{}
 var randomStringLength = 8

 for _, v := range textSlice {

 // roll our dice and if less than 3(or any number that you want)
 // mark the word. This is to ensure the words will be marked randomly
 rollTheDice := dice[rand.Intn(len(dice))]
 //fmt.Println("Rolled: ", rollTheDice)

 if rollTheDice <= 3 {
 // replace the words and insert into a translation map
 // so that we can translate back the cryptic replacement words back later
 replacement := randStr(randomStringLength, "alphanum")
 translateBackMap[replacement] = v
 scrambledText = append(scrambledText, replacement)
 } else {
 scrambledText = append(scrambledText, v)
 }
 }

 result := strings.Join(scrambledText, " ")

 fmt.Println("Scrambled: ", result)

 // now we want to translate back those cryptic replacement words to something readable
 // the idea here is to look for words with the size of 8 - the variable randomStringLength
 // if the length matches randomStringLength, then look for the replacement words in the
 // translateBackMap

 for _, v := range scrambledText {
 if len(v) == 8 {
 unscrambledText = append(unscrambledText, translateBackMap[v])
 } else {
 unscrambledText = append(unscrambledText, v)
 }
 }

 final := strings.Join(unscrambledText, " ")
 fmt.Println("Unscrambled: ", final)
 }

Notice that the scrambled texts have different version every time because of the random dice rollings.

Few different output:

Original: Psssst! Hey you! Yes! You there! This is my secret message to you

Scrambled: Psssst! Hey you! Yes! You there! This p9d6dS8G RvnHoPXp u23DQX56 jRGqOavM to KXHFzMDV

Unscrambled: Psssst! Hey you! Yes! You there! This is my secret message to you

Original: Psssst! Hey you! Yes! You there! This is my secret message to you

Scrambled: yQe72qOm 4w2LG8RE you! n0NTq8dl 0D2dRJ3p C55MP90M JXB6p4Hv is dhs4PPbG secret message to you

Unscrambled: Psssst! Hey you! Yes! You there! This is my secret message to you

Original: Psssst! Hey you! Yes! You there! This is my secret message to you

Scrambled: jvbTvSYk eDxBcPjQ you! ZgnS1olC BEbyBqxU g820m9ff DkyI8xED RrBma0sL PP5oH22w secret message to xKY5m4Zj

Unscrambled: Psssst! Hey you! Yes! You there! This is my secret message to you

Hope this helps! Happy coding!

  See also : Golang : Saving(serializing) and reading file with GOB





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