decrypt.py 778 B

1234567891011121314151617181920212223
  1. import hashlib
  2. import base64
  3. from Crypto.Cipher import AES
  4. class AESCipher(object):
  5. def __init__(self, key):
  6. self.bs = AES.block_size
  7. self.key=hashlib.sha256(AESCipher.str_to_bytes(key)).digest()
  8. @staticmethod
  9. def str_to_bytes(data):
  10. u_type = type(b"".decode('utf8'))
  11. if isinstance(data, u_type):
  12. return data.encode('utf8')
  13. return data
  14. @staticmethod
  15. def _unpad(s):
  16. return s[:-ord(s[len(s) - 1:])]
  17. def decrypt(self, enc):
  18. iv = enc[:AES.block_size]
  19. cipher = AES.new(self.key, AES.MODE_CBC, iv)
  20. return self._unpad(cipher.decrypt(enc[AES.block_size:]))
  21. def decrypt_string(self, enc):
  22. enc = base64.b64decode(enc)
  23. return self.decrypt(enc).decode('utf8')