Snippets

Alexander Hanel CryptoPals

Updated by Alexander Hanel

File solutions.md Modified

  • Ignore whitespace
  • Hide word diff
 
 print pad_pkcs_7("YELLOW SUBMARINE")
 ```
+
+### Challenge 10 - Implement CBC mode
Updated by Alexander Hanel

File solutions.md Modified

  • Ignore whitespace
  • Hide word diff
 print key_len(d_data, 64)
 ```
 
-## Challenge 7 - AES in ECB mode
+### Challenge 7 - AES in ECB mode
 
 ```Python
 import base64
 print plain
 ```
 
-## Challenge 8 - Detect AES in ECB mode
+### Challenge 8 - Detect AES in ECB mode
 
 ```Python
 from collections import Counter
         if count > 3:
             print key, data
 ```
+
+###  Challenge 9 - Implement PKCS#7 padding
+
+```Python
+BLOCK_SIZE = 20 # not correct standard size
+
+def pad_pkcs_7(plain_text):
+    rem = BLOCK_SIZE % len(plain_text)
+    pad = (('%02x' % rem)* rem).decode('hex')
+    return plain_text + pad
+
+print pad_pkcs_7("YELLOW SUBMARINE")
+```
Updated by Alexander Hanel

File solutions.md Modified

  • Ignore whitespace
  • Hide word diff
 
 ## Challenge 8 - Detect AES in ECB mode
 
+```Python
+from collections import Counter
+
+KEY_SIZE = 16
+data = open("8.txt", "r").readlines()
+for line in data:
+    data = line.rstrip().decode('hex')
+    substr_counter = Counter(data[i: i+ KEY_SIZE] for i in range(len(data) - KEY_SIZE))
+    sub_count = substr_counter.most_common(5)
+    for temp in sub_count:
+        key, count = temp
+        if count > 3:
+            print key, data
+```
Updated by Alexander Hanel

File solutions.md Modified

  • Ignore whitespace
  • Hide word diff
 data = open("6.txt",'rb').read()
 d_data = base64.b64decode(data)
 print key_len(d_data, 64)
-```
+```
+
+## Challenge 7 - AES in ECB mode
+
+```Python
+import base64
+from Crypto.Cipher import AES
+
+key = "YELLOW SUBMARINE"
+IV = 24 * "\x00"
+mode = AES.MODE_ECB
+message =  base64.b64decode(open("7.txt", "rb").read())
+encryptor = AES.new(key, mode, IV=IV)
+plain = encryptor.decrypt(message)
+print plain
+```
+
+## Challenge 8 - Detect AES in ECB mode
+
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
-
+ - First part uses Hamming distance to calcualte the XOR key size. 
+ - I think this is pretty slick. The code below can be used to crack locky encoded executables.  
+ 
 ```Python
 import base64
 import string
  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.