Snippets

Alexander Hanel CryptoPals

Updated by Alexander Hanel

File solutions.md Modified

  • Ignore whitespace
  • Hide word diff
  - Classic approach - https://inventwithpython.com/hacking/chapter20.html
 
 ### Challenge 5  - Implement repeating-key XOR
+ - Super cool trick to get XOR key size. Works on encrypted Locky samples. 
  
 ```Python
  def xor_mb(message, key):
 ```
 
 ### Challenge 6  - Break repeating-key XOR
- 
- 
+
+```Python
+import base64
+import string
+import sys
+import collections
+
+from cStringIO import StringIO
+from collections import Counter
+from itertools import cycle 
+from itertools import product
+
+
+def xor_mb(message, key):
+    return''.join(chr(ord(m_byte)^ord(k_byte)) for m_byte,k_byte in zip(message, cycle(key)))
+
+
+def hamming_distance(bytes_a, bytes_b):
+    return sum(bin(i ^ j).count("1") for i, j in zip(bytearray(bytes_a), bytearray(bytes_b)))
+
+
+def key_len(message, key_size):
+    """"returns [(dist, key_size),(dist, key_size)]"""
+    avg = []
+    for k in xrange(2,key_size): 
+        hd = []
+        for n in xrange(len(message)/k-1):
+            hd.append(hamming_distance(message[k*n:k*(n+1)],message[k*(n+1):k*(n*2)])/k)
+        if hd:
+            avg.append((sum(hd) / float(len(hd)), k))
+    return sorted(avg)[:10]
+    
+data = open("6.txt",'rb').read()
+d_data = base64.b64decode(data)
+print key_len(d_data, 64)
+```
Updated by Alexander Hanel

File solutions.md Modified

  • Ignore whitespace
  • Hide word diff
 ```        
  - Classic approach - https://inventwithpython.com/hacking/chapter20.html
 
- ### Challenge 5  - Implement repeating-key XOR
+### Challenge 5  - Implement repeating-key XOR
  
 ```Python
  def xor_mb(message, key):
 xor_mb(plain, "ICE").encode('hex')
 ```
 
- ### Challenge 6  - Break repeating-key XOR
+### Challenge 6  - Break repeating-key XOR
  
  
Updated by Alexander Hanel

File solutions.md Modified

  • Ignore whitespace
  • Hide word diff
 
  ### Challenge 5  - Implement repeating-key XOR
  
- ```Python
+```Python
  def xor_mb(message, key):
         return''.join(chr(ord(m_byte)^ord(k_byte)) for m_byte,k_byte in zip(message, cycle(key)))
 
Updated by Alexander Hanel

File solutions.md Modified

  • Ignore whitespace
  • Hide word diff
 ```        
  - Classic approach - https://inventwithpython.com/hacking/chapter20.html
 
+ ### Challenge 5  - Implement repeating-key XOR
+ 
+ ```Python
+ def xor_mb(message, key):
+        return''.join(chr(ord(m_byte)^ord(k_byte)) for m_byte,k_byte in zip(message, cycle(key)))
+
+plain = """Burning 'em, if you ain't quick and nimble
+I go crazy when I hear a cymbal"""
+xor_mb(plain, "ICE").encode('hex')
+```
+
+ ### Challenge 6  - Break repeating-key XOR
+ 
  
Updated by Alexander Hanel

File solutions.md Modified

  • Ignore whitespace
  • Hide word diff
 ```
 
 
- - https://dbjergaard.github.io/posts/matasano_set_1.html 
-  - Recommends chi-squared. Cool concept but super slow in Python. Would not recommend it. 
+ - https://dbjergaard.github.io/posts/matasano_set_1.html Recommends chi-squared. Cool concept but super slow in Python. Would not recommend it. 
  - [Pearson's chi-squared test](https://en.wikipedia.org/wiki/Pearson%27s_chi-squared_test) 
  - [Chi-square distribution introduction | Probability and Statistics | Khan Academy](https://www.youtube.com/watch?v=dXB3cUGnaxQ)
  - [Pearson's chi square test (goodness of fit) | Probability and Statistics | Khan Academy ](https://www.youtube.com/watch?v=2QeDRsxSF9M)
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.