解码System.Security.SecureString

时间:2015-07-31 11:01:09

标签: powershell

我想将System.Security.SecureString解密为可读密码。

$mycrdentials = Get-Credential
$abc = $mycrdentials.Password
$Ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($abc)
$result = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr)
[System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr)
$result

但是当我打印结果时,我得到的只是一个空白区域。我究竟做错了什么?我怎样才能获得可读的密码?

1 个答案:

答案 0 :(得分:1)

您发布的代码适合我。但是有一种更简单的方法:

PS C:\> $cred = Get-Credential   # entered "somepassword" as the password here

cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential
PS C:\> $cred.Password
System.Security.SecureString
PS C:\> $cred.GetNetworkCredential().Password
somepassword