从文件中的安全文本获取密码到纯文本

时间:2018-01-03 18:57:37

标签: powershell

我在powershell中有一个名为SecureText.ps1的脚本。它的作用是提示用户输入密码,然后将其转换成乱码,以便密码安全。 SecuredText.ps1

param([string] $FilePath = (([Environment]::GetFolderPath("myDocuments")+"\SecuredText.txt")))
#SecureText.ps1
#Program the secures text that is only accessable to the user. Defaults to the person "MyDoucments"
$SecurePassword = Read-Host -Prompt "Enter password" -AsSecureString | ConvertTo-SecureString -AsPlainText -Force
$SecurePassword | ConvertFrom-SecureString  | Out-File -FilePath $FilePath
$SecurePassword = $null

然后我尝试使用这些命令将其转换为纯文本

$Password = Get-Content C:\Users\fpettigrosso\Documents\SecuredText.txt | ConvertTo-SecureString -AsPlainText -Force
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password)
$PlainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
Write-Host $PlainPassword

我希望能够恢复密码,但我只是回过头来看。我如何获得密码而不是胡言乱语?

1 个答案:

答案 0 :(得分:1)

您错误地生成并解密了密码。

## Generation
Read-Host -Prompt 'Enter password' -AsSecureString |
    ConvertFrom-SecureString |
    Out-File -FilePath $Path
## Decryption
(New-Object -TypeName PSCredential -ArgumentList @(
    'user',
    ((Get-Content -Path $Path) | ConvertTo-SecureString)
)).GetNetworkCredential().Password