def md5_decrypt(data, key): reader = StringIO.StringIO(data) output = '' last_hash = '' while 1: cur_block = '' for i in range(16): cur_hash = reader.read(16) if len(cur_hash) != 16: return output cur_block += cur_hash cur_byte = '\x00' for i in range(0x100): md5_ctx = hashlib.md5() cur_byte = chr(i) md5_ctx.update(cur_byte) md5_ctx.update(key) md5_ctx.update(last_hash) if cur_hash == md5_ctx.digest(): break output += cur_byte last_hash = cur_hash md5_ctx = hashlib.md5() md5_ctx.update(cur_block) last_hash = reader.read(16) if last_hash != md5_ctx.digest(): raise Exception("Hash mismatch! Incorrect key or corrupt data")