Encrypt and Decrypt Messages With a Code Activity

This tutorial shows the older code-activity approach for encrypting and decrypting message content. If you prefer a packaged activity, use the Encryption Activities Extension Library instead.

Using It In A Workflow

1. Open your workflow in the Workflow Designer.
2. Add a Run Code activity where you want to encrypt or decrypt the current message text.
3. Right click in the code activity message template and choose Insert Activity Message so the input text flows in from the previous step.
4. Paste one of the code examples below into the code activity.
5. Use the output variable created by the code in later activities by right clicking and choosing Insert Variable.

Encryption Code Activity Example

This example reads the incoming activity message, encrypts it, and stores the result in a variable called Encrypted Message.

string EncryptionKey = "myPassword";
string contentToEncrypt = activityInstance.Message.Text;
byte[] clearBytes = Encoding.Unicode.GetBytes(contentToEncrypt);
using (System.Security.Cryptography.Aes encryptor = System.Security.Cryptography.Aes.Create())
{
    System.Security.Cryptography.Rfc2898DeriveBytes pdb =
        new System.Security.Cryptography.Rfc2898DeriveBytes(
            EncryptionKey,
            new byte[] { 0x51, 0x69, 0x52, 0x7e, 0x20, 0x4d, 0x65, 0x94, 0x46, 0x65, 0x74, 0x65, 0x76 });

    encryptor.Key = pdb.GetBytes(32);
    encryptor.IV = pdb.GetBytes(16);

    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
    {
        using (System.Security.Cryptography.CryptoStream cs =
            new System.Security.Cryptography.CryptoStream(ms, encryptor.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write))
        {
            cs.Write(clearBytes, 0, clearBytes.Length);
            cs.Close();
        }

        var encrypted = Convert.ToBase64String(ms.ToArray());
        workflowInstance.SetVariable("Encrypted Message", encrypted);
    }
}

Decryption Code Activity Example

This example reads encrypted text from the activity message, decrypts it, and stores the result in a variable called Decrypted Text.

string EncryptionKey = "myPassword";
string encryptedText = activityInstance.Message.Text;
byte[] cipherBytes = Convert.FromBase64String(encryptedText);
using (System.Security.Cryptography.Aes encryptor = System.Security.Cryptography.Aes.Create())
{
    System.Security.Cryptography.Rfc2898DeriveBytes pdb =
        new System.Security.Cryptography.Rfc2898DeriveBytes(
            EncryptionKey,
            new byte[] { 0x51, 0x69, 0x52, 0x7e, 0x20, 0x4d, 0x65, 0x94, 0x46, 0x65, 0x74, 0x65, 0x76 });

    encryptor.Key = pdb.GetBytes(32);
    encryptor.IV = pdb.GetBytes(16);

    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
    {
        using (System.Security.Cryptography.CryptoStream cs =
            new System.Security.Cryptography.CryptoStream(ms, encryptor.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write))
        {
            cs.Write(cipherBytes, 0, cipherBytes.Length);
            cs.Close();
        }

        var decryptedText = Encoding.Unicode.GetString(ms.ToArray());
        workflowInstance.SetVariable("Decrypted Text", decryptedText);
    }
}

Helpful Notes

It is usually better to store the encryption key in a workflow or global variable than to leave it hard-coded in the script. You can then replace "myPassword" with a call to workflowInstance.GetVariable("EncryptionKey").

This approach is still useful if you want to keep the logic in code rather than install an extension library, but the installer-based activity is easier to deploy and maintain for most workflows.

Encryption Activities Extension Library

Return to the Tutorials Directory