PowerShell

PowerShell: Decrypt a Secure String

In this blog post I will carry out how to decrypt a secure string. A secure string is a string that is saved in a non-readable format to temporarily store passwords and strings. Let’s start.

For testing purposes, store a password as a secure string.

$password = ConvertTo-SecureString 'P@ssw0rd' -AsPlainText -Force

Next, we will use a somewhat cryptic code to decrypt this.

$Ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($password)
$result = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr)
[System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr)

Here is the entire code in action:

Fine that’s it.

Categories: PowerShell

Tagged as: , ,

2 replies »

Leave a comment

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