PowerShell – Symmetric Encryption

You can use this PowerShell function to encrypt/decrypt data with a secret key.

I re-wrote the functions from Travis Gan, for a better overview and also added comments to the code.

Use free of charge!

Experienced advanced operations engineer with a demonstrated history of working in the information technology and services industry. Skilled in Windows Server, Azure, Ethical Hacking, Office 365, Exchange, Jenkins, SCCM, Octopus Deploy and PowerShell to name a few. Strong engineering professional with a big passion for knowledge.


  1. Ramesh

    I am not able to run your script , it is like failing to pass write values to Key & Text.
    #Encrypt data with a secret key.

    ex: Set-EncryptedData 28122012 WelomeToPowerShellEncryption

    failed that

    ConvertFrom-SecureString : Cannot bind parameter ‘Key’. Cannot convert value “28122012” to type “System.Byte”. Error: “Value was either too large or too small for
    an unsigned byte.”
    At C:\Users\rkunamneni\Desktop\powershell\Vfs-Scripts.ps1:616 char:80
    + … ata = ConvertFrom-SecureString -SecureString $SecureString -Key $Key;
    + ~~~~
    + CategoryInfo : InvalidArgument: (:) [ConvertFrom-SecureString], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ConvertFromSecureStringCommand

    will you please give me an example how to run your script?

  2. Ramesh

    #Text you want to encrypt.
    $TextInput = “This is my secret text.”;
    should work now
    #Create a secret key (between 16 or 32 in length).
    $Key = Set-SecretKey -Key “28122012AnikaKunamneni”;

    #Encrypt data with the secret key.
    $EncryptedData = Set-EncryptedData -Key $Key -TextInput $TextInput;

    #Decrypt data with the secret key.
    $DecryptedData = Get-EncryptedData -Key $Key -TextInput $EncryptedData;

    #Show decrypted data.
    Write-Output ($DecryptedData);

  3. amin benkhaled

    To fix the Script Change the name of the function ‘Set-SecretKey’ to ‘Set-EncryptedKey’
    this Fixed the error for me

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.