r/PowerShell • u/gblang • Nov 13 '25
Question Encrypting and decrypting a string with Powershell using a text password
Hi all,
what is the best way to perform password based encryption and decryption with Powershell?
Here's some context:
I have a powershell utility script that performs some API call to a server. These calls include a token, which at the moment is for convenience stored in plaintext inside the script. Since I need to share this script with other possibly untrusted users, I would like to store this token encrypted, so that the user launching the script needs to insert the right key (password) to decrypt the token and successfully execute the API calls.
In short, I would like to:
- Take a plaintext string and encrypt it using a text password
- Make the inverse operation
- All of this using only Powershell v 5.1
I think it shouldn't be hard to do it, but I couldn't find a way on my own looking on the web, can anyone help me with this? Does it even make sense or is there a better way to obfuscate the token and request authorization for launching the script?
Much appreciate anyone taking the time!
17
u/Th3Sh4d0wKn0ws Nov 13 '25
Oddly enough I went down this rabbit hole a few years ago.
What I landed on was using the .NET AesCryptoServiceProvider to encrypt/decrypt strings with a 256bit key. To get the key from a provided password I used the .NET Security.Cryptography.Rfc2898DeriveBytes, otherwise known as PBKDF2.
You're welcome to take a look at what I did: https://github.com/grey0ut/ProtectStrings
you could simplify what I wrote and create 3 functions:
1 to convert a password to a 256 bit key
1 to encrypt string text using a 256 bit key
1 to decrypt cipher text using a 256 bit key
7
u/rmbolger Nov 13 '25
What is the goal of encrypting the token? Is it to prevent these "untrusted" users from being able to obtain a cleartext copy of it which would allow them to use the same token to do other unsanctioned stuff?
If so, how will you prevent the users from looking at the script source code, copying out the portion that decrypts the token, and just decrypting the token manually using the password you've given them? Ultimately, all you're doing is hiding the token and hoping no one bothers to find it. There's no surefire way to prevent the user from finding a secret when you've given them a file containing the secret and all the tools to find it (see also, DRM futility).
Here are some alternative strategies:
4
u/Nu11u5 Nov 13 '25 edited Nov 13 '25
*-SecureString cmdlets can take an AES key. I generate a key and encrypted data to PS1 files, then dot-source them in my script.
Generate key
Encrypt data
Substitute "Password" with your token string/json.
Load encrypted data
Why save the key and data to separate files? So I can exclude them in a .gitignore file (avoid committing secrets).
Why dot-source them as PS1 data instead of using Get-Content, etc with straight files? Because most PS1-to-EXE builders or code-minimizers handle dot-sourcing by embedding the content so you don't need to manage the files as bundled resources.
It's far from being as secure as using a vault service, but sometimes that doesn't fit the use-case.
2
u/every-day_throw-away Nov 13 '25
I use DPAPI for this reason
2
u/ukelelealien Nov 14 '25
We have written our own custom module to handle this. We have a certificate loaded on our autoamtion server. This server is used to encrypt and decrypt our passwords. We store the encrypted passwords on a SQL server.
Works well. The encrypted password can only be decrypted on the automation server that has the cert, and all passwords decrypted are stored in variables so are not visible to apps like splunk that record scripts being run