implemented guess function for caesar cipher
This commit is contained in:
parent
757c1483b4
commit
38348f978f
26
main.go
26
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)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user