PowerShell - 解码System.Security.SecureString为可读密码
问题描述:
我想解密从System.Security.SecureString到可读密码的密码。PowerShell - 解码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 '[email protected]' -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
答
的细节解释http://blogs.msdn.com/b/besidethepoint/archive/2010/09/21/decrypt-secure-strings-in-powershell.aspx
我还有另一个稍微不同的方法。
$pass=convertto-securestring "[email protected]" -asplaintext -force | ConvertFrom-SecureString
[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR((ConvertTo-SecureString $pass)))
2P @ ssw0rd
答
($credentials.GetNetworkCredential()).Password
答
对于 “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对象,从以获得更多信息,证书使用交互式命令
$credentials=Get-Credential
$credentials.GetNetworkCredential().UserName
TestUsername
$credentials.GetNetworkCredential().Domain
TestDomain
$credentials.GetNetworkCredential().Password
TestPassword
注:我在这个例子中使用了PS 4。
+0
这也适用于PS 3.0 – helb
一旦你拥有了'PSCredentials'对象,你就可以: '''$ credentials.GetNetworkCredential()。Password''' – CubanX