Encrypting and decrypting strings using PowerShell
One of the challenges that I overcame when I was creating a Server 2008 hardening script was how to securely encrypt AD password changes that were made on the fly. Once that was accomplished how do I store only ciphertext to be read as a password of a user by an authorized user if necessary?
Windows PowerShell comes with a cryptography framework that for example allows the programmer to read an input string from the user as a secure object. The secure string can be used by the PowerShell program however the means to read that object back to the user in plaintext was, I found, expectedly, not as well documented.
A topic that I found interesting and that I first came across as I researched how to access plaintext from a secure object is a concept in computer science called marshaling. Briefly from Wikipidia "marshaling is the process of transforming the memory representation of an object to a data format suitable for storage or transmission".
So to read plaintext from a secure object PowerShell uses the Marshal class and I found that fascinating.
Below is a URL to a great read on this.
https://docs.microsoft.com/en-us/archive/blogs/besidethepoint/decrypt-secure-strings-in-powershell
Windows PowerShell comes with a cryptography framework that for example allows the programmer to read an input string from the user as a secure object. The secure string can be used by the PowerShell program however the means to read that object back to the user in plaintext was, I found, expectedly, not as well documented.
A topic that I found interesting and that I first came across as I researched how to access plaintext from a secure object is a concept in computer science called marshaling. Briefly from Wikipidia "marshaling is the process of transforming the memory representation of an object to a data format suitable for storage or transmission".
So to read plaintext from a secure object PowerShell uses the Marshal class and I found that fascinating.
Below is a URL to a great read on this.
https://docs.microsoft.com/en-us/archive/blogs/besidethepoint/decrypt-secure-strings-in-powershell
I'm not too well versed with writing scripts but I will definitely keep this handy if I every run into a similar issue. -Toufue
ReplyDeleteNice post Caleb! I love Powershell it is such a power full tool, I use it all the time at work. I especially enjoyed the discussion of Powershell as a security tool. I never heard of Marshalling and found this to be quite interesting. This is something I will definitely be investigating more. I wonder if this technique can be integrated in Exchange online powershell for 0365 environment configured with dirsync? - Josh S
ReplyDeleteThis looks like it could be helpful in the future, thanks Caleb
ReplyDelete