decrypt.py 902 B

123456789101112131415161718192021222324252627
  1. from base64 import b64encode, b64decode
  2. from Crypto.Cipher import AES
  3. from Crypto.Util.Padding import pad, unpad
  4. class ShanHuZhuFuAes:
  5. def __init__(self):
  6. self.key = 'xlc2ze7qnqg8xi1d'.encode('utf-8') # 需要一个bytes类型的key
  7. self.iv = self.key # 在这个例子中,key和iv是相同的
  8. def encrypt(self, data):
  9. cipher = AES.new(self.key, AES.MODE_CBC, self.iv)
  10. ct_bytes = cipher.encrypt(pad(data.encode('utf-8'), AES.block_size))
  11. ct = b64encode(ct_bytes).decode('utf-8')
  12. return ct
  13. def decrypt(self, data):
  14. try:
  15. ct = b64decode(data.encode('utf-8'))
  16. cipher = AES.new(self.key, AES.MODE_CBC, self.iv)
  17. pt = unpad(cipher.decrypt(ct), AES.block_size)
  18. return pt.decode('utf-8')
  19. except Exception as e:
  20. print("Incorrect decryption")
  21. return None