I want to decode the password from a System.Security.SecureString to a readable password.
我想将System.Security.SecureString中的密码解码为可读密码。
$password = convertto-securestring "TestPassword" -asplaintext -force
$credentials = New-Object System.Net.NetworkCredential("TestUsername", $password, "TestDomain")
This code part works fine, I can use the $credentials object. But later in my code I need the password in a readable format. Because a methode needs the password in readable string. So I must decode the password back.
这段代码部分工作正常,我可以使用$ credentials对象。但后来在我的代码中,我需要以可读格式输入密码。因为方法需要可读字符串中的密码。所以我必须重新解密密码。
How it is possible to decode the password from the $credentials object?
如何解码$ credentials对象的密码?
Update
更新
Not working:
不工作:
$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
4 个解决方案
#1
21
Here you go:
干得好:
$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
2P @ ssw0rd
#2
13
For a "System.Net.NetworkCredential" object, all you need to do is read the String password.
对于“System.Net.NetworkCredential”对象,您需要做的就是读取String密码。
$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;}
If you end up with a PSCredential object, from an interactive command like Get-Credential use
如果您最终得到PSCredential对象,请使用Get-Credential使用的交互式命令
$credentials=Get-Credential
$credentials.GetNetworkCredential().UserName
TestUsername
$credentials.GetNetworkCredential().Domain
TestDomain
$credentials.GetNetworkCredential().Password
TestPassword
See http://blogs.technet.com/b/heyscriptingguy/archive/2013/03/26/decrypt-powershell-secure-string-password.aspx for details.
有关详细信息,请参阅http://blogs.technet.com/b/heyscriptingguy/archive/2013/03/26/decrypt-powershell-secure-string-password.aspx。
Note: I used PS 4 for this example.
注意:我在这个例子中使用了PS 4。
#3
3
The details are explained http://blogs.msdn.com/b/besidethepoint/archive/2010/09/21/decrypt-secure-strings-in-powershell.aspx
详情请参阅http://blogs.msdn.com/b/besidethepoint/archive/2010/09/21/decrypt-secure-strings-in-powershell.aspx
and I have yet another slightly different way of doing it.
我还有另一种略微不同的做法。
$pass=convertto-securestring "P@ssw0rd" -asplaintext -force | ConvertFrom-SecureString
[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR( (ConvertTo-SecureString $pass) ))
P@ssw0rd
2P @ ssw0rd
#4
3
($credentials.GetNetworkCredential()).Password
#1
21
Here you go:
干得好:
$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
2P @ ssw0rd
#2
13
For a "System.Net.NetworkCredential" object, all you need to do is read the String password.
对于“System.Net.NetworkCredential”对象,您需要做的就是读取String密码。
$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;}
If you end up with a PSCredential object, from an interactive command like Get-Credential use
如果您最终得到PSCredential对象,请使用Get-Credential使用的交互式命令
$credentials=Get-Credential
$credentials.GetNetworkCredential().UserName
TestUsername
$credentials.GetNetworkCredential().Domain
TestDomain
$credentials.GetNetworkCredential().Password
TestPassword
See http://blogs.technet.com/b/heyscriptingguy/archive/2013/03/26/decrypt-powershell-secure-string-password.aspx for details.
有关详细信息,请参阅http://blogs.technet.com/b/heyscriptingguy/archive/2013/03/26/decrypt-powershell-secure-string-password.aspx。
Note: I used PS 4 for this example.
注意:我在这个例子中使用了PS 4。
#3
3
The details are explained http://blogs.msdn.com/b/besidethepoint/archive/2010/09/21/decrypt-secure-strings-in-powershell.aspx
详情请参阅http://blogs.msdn.com/b/besidethepoint/archive/2010/09/21/decrypt-secure-strings-in-powershell.aspx
and I have yet another slightly different way of doing it.
我还有另一种略微不同的做法。
$pass=convertto-securestring "P@ssw0rd" -asplaintext -force | ConvertFrom-SecureString
[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR( (ConvertTo-SecureString $pass) ))
P@ssw0rd
2P @ ssw0rd
#4
3
($credentials.GetNetworkCredential()).Password