s3_helpers_22.py 540 B

1234567891011121314151617181920212223
  1. def encrypt_decrypt_aes(should_encrypt, password, data):
  2. from Crypto.Cipher import AES
  3. data = data.encode(REMOTE_ENC)
  4. key = get_key_from_pw(password)
  5. PADDING = b'{'
  6. BLOCK_SIZE = 32
  7. # create a cipher object using the random secret
  8. cipher = AES.new(key)
  9. if should_encrypt:
  10. data += (BLOCK_SIZE - len(data) % BLOCK_SIZE) * PADDING
  11. data = cipher.encrypt(data)
  12. data = base64.b64encode(data)
  13. else:
  14. data = base64.b64decode(data)
  15. data = cipher.decrypt(data)
  16. data = data.rstrip(PADDING)
  17. return data.decode(REMOTE_ENC)