diff options
-rw-r--r-- | challenge/c05.go | 12 | ||||
-rw-r--r-- | challenge/c06.go | 8 | ||||
-rw-r--r-- | lib/xor.go | 16 |
3 files changed, 15 insertions, 21 deletions
diff --git a/challenge/c05.go b/challenge/c05.go index a9b1682..a1413ef 100644 --- a/challenge/c05.go +++ b/challenge/c05.go @@ -10,12 +10,12 @@ import ( ) func C5() { - icebaby := `Burning 'em, if you ain't quick and nimble -I go crazy when I hear a cymbal` - key := "ICE" - es := lib.RepeatingXOR(icebaby, key) - hs := lib.AsciiStrToHexStr(es) - fmt.Printf("RepeatingXOR('%v', '%v') = %v\n", icebaby, key, hs) + icebaby := lib.StrToBytes(`Burning 'em, if you ain't quick and nimble +I go crazy when I hear a cymbal`) + key := lib.StrToBytes("ICE") + eb := lib.RepeatingXOR(icebaby, key) + hs := lib.AsciiStrToHexStr(lib.BytesToStr(eb)) + fmt.Printf("RepeatingXOR('%s', '%s') = %v\n", icebaby, key, hs) } // Output: diff --git a/challenge/c06.go b/challenge/c06.go index 589236b..5d19146 100644 --- a/challenge/c06.go +++ b/challenge/c06.go @@ -94,17 +94,17 @@ Jk8DCkkcC3hFMQIEC0EbAVIqCFZBO1IdBgZUVA4QTgUWSR4QJwwRTWM=` } // 'bs' is the data to decrypt. // 'ks' is the key size. - crack := func(bs []byte, ks int) (string, string) { + crack := func(bs []byte, ks int) ([]byte, []byte) { blocks := lib.BreakIntoBlocks(bs, ks) blocks = lib.TransposeBlocks(blocks, ks) - key := "" + key := make([]byte, 0) for i := 0; i < len(blocks); i++ { k := findKey(blocks[i]) - key += string(k) + key = append(key, k) } - return key, lib.RepeatingXOR(string(bs), key) + return key, lib.RepeatingXOR(bs, key) } // Compute keysize. @@ -35,29 +35,23 @@ func FixedXORBytes(as, bs []byte) []byte { } // Both 'data' and 'key' need to be plain ascii string. -func RepeatingXOR(data, key string) string { - xs := "" +func RepeatingXOR(data, key []byte) []byte { + xs := make([]byte, 0) if len(data) < 1 || len(key) < 1 { return xs } - // data in bytes - db := []byte(data) - - // key in bytes - dk := []byte(key) - lk := len(key) - for i, ki := 0, 0; i < len(db); i++ { + for i, ki := 0, 0; i < len(data); i++ { if ki == lk { ki = 0 } // xor a byte - eb := db[i] ^ dk[ki] + eb := data[i] ^ key[ki] // append to result - xs += string(eb) + xs = append(xs, eb) ki += 1 } |