PowerShell - 将System.Security.SecureString解码为可读密码

时间:2011-09-19 08:47:00

标签: powershell powershell-v2.0 system.security

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

$password = convertto-securestring "TestPassword" -asplaintext -force
$credentials = New-Object System.Net.NetworkCredential("TestUsername", $password, "TestDomain")

这段代码部分工作正常,我可以使用$ credentials对象。 但后来在我的代码中,我需要以可读格式输入密码。因为methode需要可读字符串中的密码。所以我必须重新解密密码。

如何从$ credentials对象解码密码?

更新

不工作:

$password = convertto-securestring "TestPassword" -asplaintext -force
$credentials = New-Object System.Net.NetworkCredential("TestUsername", $password, "TestDomain")

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

5 个答案:

答案 0 :(得分:27)

你走了:

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

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

2P @ ssw0rd

答案 1 :(得分:13)

对于" System.Net.NetworkCredential"对象,您需要做的就是读取字符串密码。

$password = convertto-securestring "TestPassword" -asplaintext -force
$credentials = New-Object System.Net.NetworkCredential("TestUsername", $password, "TestDomain")
$credentials.Password
TestPassword

$credentials | gm

TypeName: System.Net.NetworkCredential

Name           MemberType Definition
----           ---------- ----------
Equals         Method     bool Equals(System.Object obj)
GetCredential  Method     System.Net.NetworkCredential GetCredential(uri uri, str
GetHashCode    Method     int GetHashCode()
GetType        Method     type GetType()
ToString       Method     string ToString()
Domain         Property   string Domain {get;set;}
Password       Property   string Password {get;set;}
SecurePassword Property   securestring SecurePassword {get;set;}
UserName       Property   string UserName {get;set;}

如果您最终使用PSCredential对象,请使用Get-Credential使用的交互式命令

$credentials=Get-Credential
$credentials.GetNetworkCredential().UserName
TestUsername
$credentials.GetNetworkCredential().Domain
TestDomain
$credentials.GetNetworkCredential().Password
TestPassword

有关详细信息,请参阅http://blogs.technet.com/b/heyscriptingguy/archive/2013/03/26/decrypt-powershell-secure-string-password.aspx

注意:我在这个例子中使用了PS 4。

答案 2 :(得分:4)

详细说明http://blogs.msdn.com/b/besidethepoint/archive/2010/09/21/decrypt-secure-strings-in-powershell.aspx

我还有另一种略有不同的做法。

$pass=convertto-securestring "P@ssw0rd" -asplaintext -force  | ConvertFrom-SecureString
[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR( (ConvertTo-SecureString $pass) ))

2P @ ssw0rd

答案 3 :(得分:4)

($credentials.GetNetworkCredential()).Password

答案 4 :(得分:0)

由于某种原因,

Rob 上面的帖子对我不起作用。我从 another site 找到了答案。

多行代码版本:

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

$decrypted = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)
$decryptedPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($decrypted)
$decryptedPassword    # Outputs: P@ssw0rd

单行版本(您可以将其保存为变量):

# Outputs: P@ssw0rd
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))