Skip to content

Instantly share code, notes, and snippets.

@aclissold
Created October 11, 2013 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aclissold/6941399 to your computer and use it in GitHub Desktop.
Save aclissold/6941399 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"math/rand"
"runtime"
"strconv"
"time"
)
/*
func reversed(a string) string {
// Get Unicode code points.
n := 0
rune := make([]rune, len(a))
for _, r := range a {
rune[n] = r
n++
}
rune = rune[0:n]
// Reverse
for i := 0; i < n/2; i++ {
rune[i], rune[n-1-i] = rune[n-1-i], rune[i]
}
// Convert back to UTF-8.
output := string(rune)
return output
}
*/
var message string = `012222 1114142503 0313012513 03141418192102 0113 2419182119021713 06131715070119`
var keyedAlphabet string = "BHISOECRTMGWYVALUZDNFJKPQX"
func main() {
// Determine which characters are most common and are therefore vowels.
/*
counts := make([]int, 26)
for i := 0; i < len(message); i+=2 {
if string(message[i]) == " " {
i -= 1
continue
}
num, err := strconv.Atoi(message[i:i+2]); if err != nil {
log.Fatal(err)
}
counts[num-1]++
}
fmt.Println(counts)
*/
// Since 5 and 4 appear next to each other, they are most likely O and E and this is a ROT
// cipher. 5 appears at index 13 (zero-indexed), and O appears at index 4, so we rotate the
// alphabet 13 - 4 = 9 times.
/*
unkeyedAlphabet := "ZDNFJKPQXBHISOECRTMGWYVALU"
for i := 0; i < len(message); i+=2 {
if string(message[i]) == " " {
i -= 1
fmt.Print(" ")
continue
}
num, err := strconv.Atoi(message[i:i+2]); if err != nil {
log.Fatal(err)
}
fmt.Print(string(unkeyedAlphabet[num]))
}
*/
runtime.GOMAXPROCS(2)
go permute()
permute()
time.Sleep(100 * time.Second)
// Try every rotation of the alphabet
/*
for i := 0; i < 25; i++ {
rotatedAlphabet := keyedAlphabet[25-i:] + keyedAlphabet[:25-i]
// fmt.Println(rotatedAlphabet)
for i := 0; i < len(message); i += 2 {
if string(message[i]) == " " {
i -= 1
fmt.Print(" ")
continue
}
num, err := strconv.Atoi(message[i : i+2])
if err != nil {
log.Fatal(err)
}
fmt.Print(string(rotatedAlphabet[num]))
}
fmt.Println()
}
*/
}
func permute() {
for i := 0; ; i++ {
rand.Seed(int64(time.Now().Nanosecond()))
randValues := rand.Perm(26)
var newAlphabet string
for i := range randValues {
newAlphabet += string(keyedAlphabet[randValues[i]])
}
// Print the alphabet using newAlphabet
output := ""
for i := 0; i < len(message); i += 2 {
if string(message[i]) == " " {
i -= 1
output += " "
continue
}
num, err := strconv.Atoi(message[i : i+2])
if err != nil {
log.Fatal(err)
}
output += string(newAlphabet[num])
}
//time.Sleep(40*time.Millisecond)
fmt.Println(output)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment