[Cryptopals] How to Break repeating-key XOR

Posted by with No comments

For 3 weeks, It's time that I have solved this challenge! I though I would fail because my english was very bad and challenge, hints was writed by english.

Now, let's see what we have?
   I found some things good by google:
         1st:
The hardest exercise in the set by far, despite the problem description giving you a clear set of steps.
Use the length-normalised Hamming distance to guess the keysize. Contrary to the instructions which kinda indicate you need only consider the first and second blocks, I had to take the average normalised Hamming distance from the first block against all the others.
Break the input into blocks of keysize length, transpose them into a much smaller number (keysize) chunks (ByteString.transpose does the trick), crack each chunk using the code written for #3, concat the keys for each chunk. Done.
                2nd: Matasano Solution

            and so on...

Review the problem of challenge:
     
There's a file here. It's been base64'd after being encrypted with repeating-key XOR.
Decrypt it.

And hints:


Here's how:
  1. Let KEYSIZE be the guessed length of the key; try values from 2 to (say) 40.
  2. Write a function to compute the edit distance/Hamming distance between two strings. The Hamming distance is just the number of differing bits. The distance between:
    this is a test
    and
    wokka wokka!!!
    is 37. Make sure your code agrees before you proceed.
  3. For each KEYSIZE, take the first KEYSIZE worth of bytes, and the second KEYSIZE worth of bytes, and find the edit distance between them. Normalize this result by dividing by KEYSIZE.
  4. The KEYSIZE with the smallest normalized edit distance is probably the key. You could proceed perhaps with the smallest 2-3 KEYSIZE values. Or take 4 KEYSIZE blocks instead of 2 and average the distances.
  5. Now that you probably know the KEYSIZE: break the ciphertext into blocks of KEYSIZE length.
  6. Now transpose the blocks: make a block that is the first byte of every block, and a block that is the second byte of every block, and so on.
  7. Solve each block as if it was single-character XOR. You already have code to do this.
  8. For each block, the single-byte XOR key that produces the best looking histogram is the repeating-key XOR key byte for that block. Put them together and you have the key.
This code is going to turn out to be surprisingly useful later on. Breaking repeating-key XOR ("Vigenere") statistically is obviously an academic exercise, a "Crypto 101" thing. But more people "know how" to break it than can actually break it, and a similar technique breaks something much more important.
OK. Let's begin to break it!

Firstly, I try to find keylength of the key. Thank to Google.com.vn, I wrote a snippet to do it:

 This snippet I use to find hamming distance:
   
 and it is use to find normalize to determine which is keylength of key:
Run this snippet which random *keysizeMax*, I will get a probaly keysize.
      
2
2.25
-------
3
2.8333333333333335
-------
4
3.625
-------
5
2.6
29
2.8189655172413794
-------

Then, I just try one-by-one keysizes, I try to look up which keysize include english character, it's extractly keysize. In this case, the extractly keysize is 29.
 T
Ibhcl na ro
ncemmoiavano pt1dhgn an'  euY hbo.oxaa ef ygeyoar
hd
o e bc  n'ueoanteraayo yio
btdc,

Run `seq ` to brutefore key, we see T is match, next to second character of key.. we got
e
'eis adnnuuiM'a nelnsen'nol - yi
k shtrton ln mi nI rGihr  romSt YwiaYoaal  r,mry  yyy 
 emPohe,
repeat it to 29.. we will get full of key: "Terminator X: Bring the noise".

0 nhận xét:

Đăng nhận xét