md5_encryption_2.py 701 B

12345678910111213141516171819202122232425262728
  1. def md5_decrypt(data, key):
  2. reader = StringIO.StringIO(data)
  3. output = ''
  4. last_hash = ''
  5. while 1:
  6. cur_block = ''
  7. for i in range(16):
  8. cur_hash = reader.read(16)
  9. if len(cur_hash) != 16:
  10. return output
  11. cur_block += cur_hash
  12. cur_byte = '\x00'
  13. for i in range(0x100):
  14. md5_ctx = hashlib.md5()
  15. cur_byte = chr(i)
  16. md5_ctx.update(cur_byte)
  17. md5_ctx.update(key)
  18. md5_ctx.update(last_hash)
  19. if cur_hash == md5_ctx.digest():
  20. break
  21. output += cur_byte
  22. last_hash = cur_hash
  23. md5_ctx = hashlib.md5()
  24. md5_ctx.update(cur_block)
  25. last_hash = reader.read(16)
  26. if last_hash != md5_ctx.digest():
  27. raise Exception("Hash mismatch! Incorrect key or corrupt data")