Encrypt files and text
This article explains how to encrypt files and text data.
Encryption technology and things to keep in mind
Section titled “Encryption technology and things to keep in mind”The encryption technology used here is AES.
It’s still used in many environments today. A familiar example is that it’s also used for the connection between a Wi-Fi router and your devices (WPA2 / WPA3).
It’s hard to break by brute force, and with a key length of 256 bits, cracking it by brute force is considered impossible in any realistic amount of time.
It’s classified as symmetric-key encryption, which requires the same password for both encryption and decryption.
This means that if you lose the password, you can no longer decrypt the data back to its original state.
If you encrypt data with AES in Power Automate Desktop, be sure to keep a record of the password so you can decrypt it later.
Encrypt specific text data
Section titled “Encrypt specific text data”To encrypt specific string data that you got from on-premises software or the web, use the “Encrypt text with AES” action.
Details of the “Encrypt text with AES” action
Section titled “Details of the “Encrypt text with AES” action”The action has the following parameters.
| Parameter | Value |
|---|---|
| Encoding | The encoding (character encoding) of the text to encrypt |
| Text to encrypt | The text to encrypt |
| Encryption key | The password to use for encryption |

In the advanced settings, you can specify the padding (default: PKCS7) and the key size (128 / 192 / 256 bits, default: 256 bits). The default encoding is “Unicode.” When decrypting, you need to specify the same encoding, padding, and key size that you used for encryption.
Also, if you turn on “Use salt” and “Use initialization vector” in the advanced settings, a randomly generated salt and initialization vector are stored in variables as Base64 strings. If you encrypt with these, you also need to specify the same salt and initialization vector values when decrypting, so keep a record of them along with the password.
The encryption result is stored in the variable EncryptedText as a Base64 string.
Implement the sample flow by copying and pasting
Section titled “Implement the sample flow by copying and pasting”As a sample, I prepared a flow that encrypts text data and saves the result to a file.
Copy the following code and paste it into the Power Automate Desktop flow designer to add the actions.
The encryption password isn’t carried over between machines, so set the “Encryption key” field of the added action to a password of your choice.
SET TargetString TO $'''%'Text to encrypt'%'''Cryptography.EncryptText.EncryptTextWithAES Encoding: Cryptography.EncryptionEncoding.UTF8 TextToEncrypt: TargetString EncryptionKey: `` PaddingMode: Cryptography.PaddingMode.PKCS7 KeySize: Cryptography.AESKeySize.Bits256 EncryptedText=> EncryptedTextFile.WriteText File: $'''C:\\Users\\Public\\Documents\\encrypted.txt''' TextToWrite: EncryptedText AppendNewLine: True IfFileExists: File.IfFileExists.Overwrite Encoding: File.FileEncoding.UTF8When you run it, an encrypted text file is saved in the Public Documents folder.
Implement in Power Automate Desktop
Section titled “Implement in Power Automate Desktop”-
In “Search actions,” type “Encrypt text with AES.”
-
Set the parameters as follows.
%TargetString%is the variable created on the first line of the sample flow (the “Set variable” action).Parameter Value Encoding UTF-8 Text to encrypt %TargetString% Encryption key The password to use for encryption -
When you run the flow, the specified string is encrypted with AES.
Possible exceptions
Section titled “Possible exceptions”If the encryption settings aren’t correct or the text contains unsupported characters, an error may occur during encryption.
We recommend adding error handling with an “On block error” block.
Encrypt specific file data
Section titled “Encrypt specific file data”To get the contents of a specific file and encrypt them, use the “Encrypt from file with AES” action.
Details of the “Encrypt from file with AES” action
Section titled “Details of the “Encrypt from file with AES” action”The action has the following parameters.
| Parameter | Value |
|---|---|
| Encoding | The encoding (character encoding) of the file to encrypt |
| File to encrypt | The file to encrypt |
| Encryption key | The password to use for encryption |

The description reads
Encrypt the contents of a file with AES, using a key and a specified encoding format.
so it’s easy to misunderstand, but this action doesn’t overwrite the file it reads.
The encrypted result is stored in a variable.
Implement the sample flow by copying and pasting
Section titled “Implement the sample flow by copying and pasting”As a sample, I prepared a flow that encrypts a text file saved in the Public Documents folder and saves the resulting string as a separate text file.
Copy the following code and paste it into the Power Automate Desktop flow designer to add the actions.
The encryption password isn’t carried over between machines, so set the “Encryption key” field of the added action to a password of your choice.
Cryptography.EncryptFromFile.EncryptFromFileWithAES Encoding: Cryptography.EncryptionEncoding.UTF8 FileToEncrypt: $'''C:\\Users\\Public\\Documents\\document.txt''' EncryptionKey: `` PaddingMode: Cryptography.PaddingMode.PKCS7 KeySize: Cryptography.AESKeySize.Bits256 EncryptedText=> EncryptedTextFile.WriteText File: $'''C:\\Users\\Public\\Documents\\encrypted.txt''' TextToWrite: EncryptedText AppendNewLine: True IfFileExists: File.IfFileExists.Overwrite Encoding: File.FileEncoding.UTF8Implement in Power Automate Desktop
Section titled “Implement in Power Automate Desktop”-
In “Search actions,” type “Encrypt from file with AES.”
-
Set the parameters as follows. Match the encoding to the character encoding of the file you’re encrypting.
Parameter Value Encoding UTF-8 File to encrypt C:\Users\Public\Documents\document.txt Encryption key The password to use for encryption -
When you run the flow, the text data in the specified file is encrypted, and the result is stored in a variable.
Possible exceptions
Section titled “Possible exceptions”The action fails with an error if the specified file doesn’t exist or if a problem occurs during encryption.
We recommend adding error handling with an “On block error” block.