func Test3(t *testing.T) { // Decode ciphertext ciphertext, err := encoding.DecodeHex([]byte(input3)) if err != nil { t.Error("Decoding", input3, "failed") } // Attempt to find byte most likely xor'd with ciphertext closestByte := xorbyte.FindByte(ciphertext, xorbyte.EnglishScorer) // Construct key from most likely byte key := make([]byte, len(ciphertext)) for i := 0; i < len(key); i++ { key[i] = closestByte } // Decrypt ciphertext plaintext, _ := bits.Xor(ciphertext, key) // output plaintext if string(plaintext) != output3 { t.Error("Incorrect key chosen:", string(closestByte), string(plaintext)) } }
func FindKey(ciphertext []byte) ([]byte, error) { keysize, err := FindKeysize(ciphertext, KEYSIZE_LOW, KEYSIZE_HIGH, SAMPLE_BLOCKS) if err != nil { return nil, err } fmt.Println(keysize) // TODO: How does padding affect single xor cracking? transposed, err := bits.Transpose(ciphertext, keysize) if err != nil { return nil, err } blocksTotal := len(transposed) / keysize key := make([]byte, blocksTotal) for i := 0; i < blocksTotal; i++ { block := transposed[i*keysize : (i+1)*keysize] key[i] = xorbyte.FindByte(block, xorbyte.EnglishScorer) } return key, nil }