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

问题描述 投票:1回答:1

我在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

我希望得到密码,但我只是回过头来胡说八道。我如何获得密码而不是乱码?

powershell
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.