LaP*_*Phi 21 powershell system.security powershell-2.0
我想将System.Security.SecureString中的密码解码为可读密码.
$password = convertto-securestring "TestPassword" -asplaintext -force
$credentials = New-Object System.Net.NetworkCredential("TestUsername", $password, "TestDomain")
Run Code Online (Sandbox Code Playgroud)
这段代码部分工作正常,我可以使用$ 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
Run Code Online (Sandbox Code Playgroud)
Sha*_*evy 28
干得好:
$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
Run Code Online (Sandbox Code Playgroud)
2P @ ssw0rd
Gre*_*tle 14
对于"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;}
Run Code Online (Sandbox Code Playgroud)
如果最终得到PSCredential对象,请使用Get-Credential使用的交互式命令
$credentials=Get-Credential
$credentials.GetNetworkCredential().UserName
TestUsername
$credentials.GetNetworkCredential().Domain
TestDomain
$credentials.GetNetworkCredential().Password
TestPassword
Run Code Online (Sandbox Code Playgroud)
注意:我在这个例子中使用了PS 4.
由于某种原因,罗布的上述帖子对我不起作用。我从另一个网站找到了答案。
多行代码版本:
$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
Run Code Online (Sandbox Code Playgroud)
单行版本(您可以将其保存到变量中):
# Outputs: P@ssw0rd
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
Run Code Online (Sandbox Code Playgroud)
详细说明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) ))
Run Code Online (Sandbox Code Playgroud)
P@ssw0rd
| 归档时间: |
|
| 查看次数: |
40470 次 |
| 最近记录: |