cryptopals

https://cryptopals.com/
Log | Files | Refs

break_fixed_nonce_ctr_using_substitutions.py (821B)


      1 from Crypto.Cipher import AES
      2 from Crypto import Random
      3 from Crypto.Util import Counter
      4 
      5 key = Random.new().read(AES.block_size)
      6 
      7 def encrypt(plaintext):
      8     return AES.new(key, AES.MODE_CTR, counter=Counter.new(128)).encrypt(plaintext)
      9 
     10 def xor(a, b):
     11     return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a, b)])
     12 
     13 with open('files/19.txt') as f:
     14     ciphertexts = [encrypt(line.decode("base64")) for line in f.read().splitlines()]
     15 
     16 def drag_crib(crib, a_xor_b):
     17     derp = []
     18     for n in range(len(a_xor_b) - len(crib) + 1):
     19         potential = xor(crib, a_xor_b[n : n + len(crib)])
     20         derp.append(potential)
     21 
     22     print derp
     23 
     24 for i in range(40):
     25         #the string below was the result of half an hour of manual crib dragging
     26         drag_crib("or polite meaningless ", xor(ciphertexts[i], ciphertexts[5]))