解码System.Security.SecureString为可读密码



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

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

这个代码部分工作得很好,我可以使用$credentials对象。但是稍后在我的代码中,我需要可读格式的密码。因为方法需要可读字符串形式的密码。所以我必须把密码解码回来。

如何从$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 

给你:

$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 

P@ssw0rd

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

($credentials.GetNetworkCredential()).Password

详细说明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) ))

P@ssw0rd

由于某些原因,Rob上面的帖子对我不起作用。我在另一个网站上找到了答案。

多行代码版本:

$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))

相关内容

  • 没有找到相关文章

最新更新