md5_encryption_1.py 572 B

12345678910111213141516171819202122232425262728
  1. def md5_encrypt(data, key):
  2. if len(data) % 16 != 0:
  3. data += (16 - (len(data) % 16)) * '\x00'
  4. hash_block = []
  5. output = ''
  6. last_hash = ''
  7. for c in data:
  8. md5_ctx = hashlib.md5()
  9. md5_ctx.update(c)
  10. md5_ctx.update(key)
  11. md5_ctx.update(last_hash)
  12. hash_block.append(md5_ctx.digest())
  13. last_hash = hash_block[-1]
  14. if len(hash_block) == 16:
  15. cur_block = ''
  16. for b in hash_block:
  17. cur_block += b
  18. md5_ctx = hashlib.md5()
  19. md5_ctx.update(cur_block)
  20. last_hash = md5_ctx.digest()
  21. output += cur_block + last_hash
  22. hash_block = []
  23. return output