From 38348f978f928e7868cae567cbf9a938db3fac1d Mon Sep 17 00:00:00 2001 From: derek mcquay Date: Mon, 14 Mar 2016 12:50:45 -0700 Subject: [PATCH] implemented guess function for caesar cipher --- main.go | 26 +++++++++++++++++++++++--- psyfer/guess.go | 36 +++++++++++++++++++++++++----------- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 4a08bc9..fe434a8 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,21 @@ import ( "github.com/spf13/cobra" ) +func clean(input string) string { + input = strings.ToUpper(strings.Replace(input, ",", "", -1)) + input = strings.ToUpper(strings.Replace(input, ".", "", -1)) + input = strings.ToUpper(strings.Replace(input, "'", "", -1)) + input = strings.ToUpper(strings.Replace(input, "\"", "", -1)) + input = strings.ToUpper(strings.Replace(input, "(", "", -1)) + input = strings.ToUpper(strings.Replace(input, ")", "", -1)) + input = strings.ToUpper(strings.Replace(input, ";", "", -1)) + input = strings.ToUpper(strings.Replace(input, ":", "", -1)) + input = strings.ToUpper(strings.Replace(input, "!", "", -1)) + input = strings.ToUpper(strings.Replace(input, "?", "", -1)) + input = strings.ToUpper(strings.Replace(input, "$", "", -1)) + return input +} + func init() { rand.Seed(time.Now().UnixNano()) } @@ -115,6 +130,7 @@ func main() { os.Exit(1) } for _, arg := range args { + arg = clean(arg) arg = strings.ToUpper(strings.Replace(arg, " ", "", -1)) key = strings.ToUpper(strings.Replace(key, " ", "", -1)) fmt.Printf("%v\n", psyfer.VigenereCipher(arg, key, decrypt)) @@ -124,14 +140,18 @@ func main() { var guess = &cobra.Command{ Use: "guess", - Short: "guess cipher", - Long: `will try to guess ceasar cipher`, + Short: "guess caesar cipher", + Long: `will try to guess a caesar cipher`, Run: func(cmd *cobra.Command, args []string) { if len(args) < 1 { fmt.Println("missing input, see -h (--help) for more info") os.Exit(1) } - psyfer.Guess() + fmt.Println("Top 5 possibilities:") + for _, arg := range args { + arg = clean(arg) + psyfer.Guess(arg) + } }, } diff --git a/psyfer/guess.go b/psyfer/guess.go index 2eb1738..67b4d41 100644 --- a/psyfer/guess.go +++ b/psyfer/guess.go @@ -4,6 +4,7 @@ import ( "fmt" "math" "sort" + "strings" ) var charFreq = []float64{ @@ -58,22 +59,35 @@ func (p allPhi) Len() int { return len(p) } func (p allPhi) Swap(i, j int) { p[i], p[j] = p[j], p[i] } func (p allPhi) Less(i, j int) bool { return p[i].value < p[j].value } -func Guess() { - fmt.Println((6 - 7) % 26) +func CharacterFrequency(input string) map[rune]float64 { + cf := make(map[rune]int) + freq := make(map[rune]float64) + for _, c := range input { + cf[c]++ + } + l := len(input) + for c, n := range cf { + freq[c] = float64(n) / float64(l) + } + return freq +} + +func Guess(input string) { + alphabet := "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + input = strings.ToUpper(strings.Replace(input, " ", "", -1)) + freq := CharacterFrequency(input) total := []phi{} for i := 0; i < 26; i++ { a := 0.0 - a += 0.1 * charFreq[pc(6, i)] - a += 0.1 * charFreq[pc(7, i)] - a += 0.1 * charFreq[pc(10, i)] - a += 0.3 * charFreq[pc(14, i)] - a += 0.2 * charFreq[pc(17, i)] - a += 0.1 * charFreq[pc(20, i)] - a += 0.1 * charFreq[pc(25, i)] + for c, n := range freq { + index := int(c - 'A') + a += n * charFreq[pc(index, i)] + } p := phi{i, a} total = append(total, p) } - fmt.Println(total) sort.Sort(allPhi(total)) - fmt.Println(total) + for i := len(total) - 1; i > len(total)-6; i-- { + fmt.Println(VigenereCipher(input, string(alphabet[total[i].position]), true)) + } }