matasano-cryptopals-go

My attempt at solving the Matasano Cryptopals challenges in Go
Log | Files | Refs

commit 2f204cbab1b11048c8a2960daa147b930fed3030
parent 05175ba70ee2aa90dcd5147fbd69244c95b6382e
Author: Dionysis Grigoropoulos <dgrig@erethon.com>
Date:   Wed, 20 Jun 2018 04:01:11 +0300

set_1: Refactor keysize calc for repeating XOR

This is based on the method described here
https://trustedsignal.blogspot.com/2015/07/cracking-repeating-xor-key-crypto.html

Diffstat:
set_1.go | 10++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/set_1.go b/set_1.go @@ -114,9 +114,15 @@ func hammingDistanceBytes(a, b []byte) int { func findRepeatingXORKeySize(cipher []byte) int { score := float64(10000000) var size int + var tmpScore float64 for keySize := 2; keySize < 40; keySize++ { - f, s := cipher[:keySize*4], cipher[keySize*4:keySize*2*4] - tmpScore := float64(hammingDistanceBytes(f, s)) / float64(keySize*4) + tmpScore = 0 + iterations := len(cipher) / keySize + for i := 0; i < iterations; i++ { + f, s := cipher[i*keySize:(keySize*i)+keySize], cipher[keySize+(keySize*i):(keySize*i)+(keySize*2)] + tmpScore += float64(hammingDistanceBytes(f, s)) + } + tmpScore = tmpScore / float64(iterations) / float64(keySize) if tmpScore < score { score = tmpScore size = keySize