Snippets

Alexander Hanel CryptoPals

Updated by Alexander Hanel

File solutions.md Modified

  • Ignore whitespace
  • Hide word diff
 		print key, xor_mb(message, key)
 ```
 ### Challenge 4  - Detect single-character XOR
+ - Solution non-frequency analysis approach
+```Python
+import string
+from collections import Counter
+from itertools import cycle
+
+def xor_mb(message, key):
+        return''.join(chr(ord(m_byte)^ord(k_byte)) for m_byte,k_byte in zip(message, cycle(key)))
+
+printset = set(string.printable)
+messages = open("4.txt", 'r').readlines()
+
+for line in messages:
+    message = line.rstrip().decode('hex')
+    cnt = Counter(message)
+    for vv in cnt.most_common(3):
+            for char in "ETAOINSHRDLU".lower():
+                key = chr(ord(vv[0]) ^ ord(char))
+                temp = xor_mb(message, key)
+                if set(temp).issubset(printset) and temp[-1] == "\n":
+                    print key, temp
+```
+
+
  - https://dbjergaard.github.io/posts/matasano_set_1.html
  - [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)
Updated by Alexander Hanel

File solutions.md Modified

  • Ignore whitespace
  • Hide word diff
 ## Set 1
 
-### Reads (no cheats)
- - https://dbjergaard.github.io/posts/matasano_set_1.html
-
-
 ### Challenge 1 - Convert hex to base64
 
 ```Python
 		print key, xor_mb(message, key)
 ```
 ### Challenge 4  - Detect single-character XOR
- - [Pearson's chi-squared test](https://en.wikipedia.org/wiki/Pearson%27s_chi-squared_test) 
+ - https://dbjergaard.github.io/posts/matasano_set_1.html
+ - [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)
Updated by Alexander Hanel

File solutions.md Modified

  • Ignore whitespace
  • Hide word diff
 ## Set 1
 
+### Reads (no cheats)
+ - https://dbjergaard.github.io/posts/matasano_set_1.html
+
+
 ### Challenge 1 - Convert hex to base64
 
 ```Python
 		key = chr(ord(vv[0]) ^ ord(char))
 		print key, xor_mb(message, key)
 ```
+### Challenge 4  - Detect single-character XOR
+ - [Pearson's chi-squared test](https://en.wikipedia.org/wiki/Pearson%27s_chi-squared_test) 
Updated by Alexander Hanel

File solutions.md Modified

  • Ignore whitespace
  • Hide word diff
 ```
 
 ### Challenge 3 - Single-byte XOR cipher
-
+```Python 
+from collections import Counter
+message = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736".decode('hex')
+cnt = Counter(message)
+cnt.most_common(3)
+for vv in cnt.most_common(7):
+	for char in "ETAOIN SHRDLU".lower():
+		key = chr(ord(vv[0]) ^ ord(char))
+		print key, xor_mb(message, key)
+```
Updated by Alexander Hanel

File solutions.md Modified

  • Ignore whitespace
  • Hide word diff
 from itertools import cycle
 
 def xor_mb(message, key):
-    '''Multi-byte XOR of a string message and string key'''
     return''.join(chr(ord(m_byte)^ord(k_byte)) for m_byte,k_byte in zip(message, cycle(key)))
 
 xor_mb("1c0111001f010100061a024b53535009181c".decode('hex'), "686974207468652062756c6c277320657965".decode('hex')).encode('hex')
  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.