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!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
#Generate a secret key. Function Set-SecretKey { [CmdletBinding()] Param ( [string]$Key ) #Get key length. $Length = $Key.Length; #Pad length. $Pad = 32-$Length; #If the length is less than 16 or more than 32. If(($Length -lt 16) -or ($Length -gt 32)) { #Throw exception. Throw "String must be between 16 and 32 characters"; } #Create a new ASCII encoding object. $Encoding = New-Object System.Text.ASCIIEncoding; #Get byte array. $Bytes = $Encoding.GetBytes($Key + "0" * $Pad); #Return byte array. Return $Bytes; } #Encrypt data with a secret key. Function Set-EncryptedData { [CmdletBinding()] Param ( $Key, [string]$TextInput ) #Create a new secure string object. $SecureString = New-Object System.Security.SecureString; #Convert the text input to a char array. $Chars = $TextInput.ToCharArray(); #Foreach char in the array. ForEach($Char in $Chars) { #Append the char to the secure string. $SecureString.AppendChar($Char); } #Encrypt the data from the secure string. $EncryptedData = ConvertFrom-SecureString -SecureString $SecureString -Key $Key; #Return the encrypted data. Return $EncryptedData; } #Decrypt data with a secret key. Function Get-EncryptedData { [CmdletBinding()] Param ( $Key, $TextInput ) #Decrypt the text input with the secret key. $Result = $TextInput | ConvertTo-SecureString -Key $Key | ForEach-Object {[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($_))}; #Return the decrypted data. Return $Result; } #Text you want to encrypt. $TextInput = "This is my secret text."; #Create a secret key (between 16 or 32 in length). $Key = Set-EncryptedKey -Key "<My secret key>"; #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); |
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?
#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);
To fix the Script Change the name of the function ‘Set-SecretKey’ to ‘Set-EncryptedKey’
this Fixed the error for me