def encrypt_decrypt_aes(should_encrypt, password, data): from Crypto.Cipher import AES data = data.encode(REMOTE_ENC) key = get_key_from_pw(password) PADDING = b'{' BLOCK_SIZE = 32 # create a cipher object using the random secret cipher = AES.new(key) if should_encrypt: data += (BLOCK_SIZE - len(data) % BLOCK_SIZE) * PADDING data = cipher.encrypt(data) data = base64.b64encode(data) else: data = base64.b64decode(data) data = cipher.decrypt(data) data = data.rstrip(PADDING) return data.decode(REMOTE_ENC)