def md5_encrypt(data, key): if len(data) % 16 != 0: data += (16 - (len(data) % 16)) * '\x00' hash_block = [] output = '' last_hash = '' for c in data: md5_ctx = hashlib.md5() md5_ctx.update(c) md5_ctx.update(key) md5_ctx.update(last_hash) hash_block.append(md5_ctx.digest()) last_hash = hash_block[-1] if len(hash_block) == 16: cur_block = '' for b in hash_block: cur_block += b md5_ctx = hashlib.md5() md5_ctx.update(cur_block) last_hash = md5_ctx.digest() output += cur_block + last_hash hash_block = [] return output