fork download
  1. from Crypto.Cipher import AES
  2. from Crypto.Hash import SHA256
  3. from Crypto import Random
  4. import base64
  5. import urllib
  6.  
  7. def encrypt_decrypt(action, string, apikey='{your_api_key}', secretkey='{your_secret_key}'):
  8. output = False
  9. encrypt_method = "AES-256-CBC"
  10. secret_key = apikey
  11. secret_iv = secretkey
  12.  
  13. # hash
  14. key = SHA256.new(secret_key.encode()).digest()[:32]
  15. iv = SHA256.new(secret_iv.encode()).digest()[:16]
  16.  
  17. if action == 'encrypt':
  18. cipher = AES.new(key, AES.MODE_CBC, iv)
  19. output = base64.b64encode(cipher.encrypt(string.encode())).decode()
  20. output = urllib.parse.quote(output)
  21. elif action == 'decrypt':
  22. cipher = AES.new(key, AES.MODE_CBC, iv)
  23. output = cipher.decrypt(base64.b64decode(urllib.parse.unquote(string))).decode()
  24.  
  25. return output
  26.  
  27.  
Success #stdin #stdout 0.03s 12392KB
stdin
Standard input is empty
stdout
Standard output is empty