Microsoft Powershell – convert a string into a securestring and back to plaintext

.Net and therefore the powershell has the ability to define a secure string this means that the string is immediately deleted from memory if it is no longer needed

PS D:\> $MySecurePassword=read-host -assecurestring „Enter password“
Enter password: ******
PS D:\> $pPassPointer = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($MySecurePassword)
PS D:\> $DecryptedPass = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($pPassPointer)
PS D:\> # Imported: free memory
PS D:\> [System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr)
PS D:\> write-host „PASS:“ $DecryptedPass

Leave a Reply

You must be logged in to post a comment.