util.py 807 B

1234567891011121314151617181920212223242526272829303132
  1. import base64
  2. import hashlib
  3. from Crypto.Cipher import AES
  4. def md5(text):
  5. text = text.encode(encoding='utf-8')
  6. m = hashlib.md5()
  7. m.update(text)
  8. return m.hexdigest()
  9. def aes_encrypt(key, data):
  10. try:
  11. mode = AES.MODE_ECB
  12. padding = lambda s: s + (16 - len(s) % 16) * chr(16 - len(s) % 16)
  13. cryptos = AES.new(key, mode)
  14. cipher_text = cryptos.encrypt(padding(data).encode("utf-8"))
  15. return base64.b64encode(cipher_text).decode("utf-8")
  16. except Exception as e:
  17. return ""
  18. def aes_decrypt(key, data):
  19. try:
  20. cryptos = AES.new(key, AES.MODE_ECB)
  21. decrpytBytes = base64.b64decode(data)
  22. meg = cryptos.decrypt(decrpytBytes).decode('utf-8')
  23. return meg[:-ord(meg[-1])]
  24. except Exception as e:
  25. return ""