gpt4 book ai didi

Powershell 文本框 SecureString

转载 作者:行者123 更新时间:2023-12-02 15:30:04 28 4
gpt4 key购买 nike

是否可以使用 System.Windows.Forms.TextBox(或任何其他方法)输入可随后转换为安全字符串的文本?我希望能够获取输入的值,将其转换为安全字符串并将其写入一个文件,然后可以在需要时调用该文件,如果我们需要识别该值,则安全字符串会转换回来。

我研究过类似的东西,但由于我使用的是 TextBox 表单,所以我不想依赖 Read-Host

$secstr = Read-Host -AsSecureString "Enter your text"

$secstr | ConvertFrom-SecureString | out-file C:\temp\test.txt

$secstr = get-content c:\temp\test.txt | ConvertTo-SecureString -AsPlaintText -Force

本质上,我想让一个文本框使用屏蔽/密码字符(我可以使用 $TextBox.PasswordChar = "*" 然后获取该输入并将其转储到安全字符串文本中文件。然后,我可以使用另一个脚本来调用该文件,并在最终用户需要知道当前值时以纯文本形式显示文本。

最佳答案

使用 MaskedTextBox如果您想将其嵌入到自定义 GUI 中,而不是常规的 TextBox:

Add-Type -Assembly 'System.Windows.Forms'

$form = New-Object Windows.Forms.Form

$password = New-Object Windows.Forms.MaskedTextBox
$password.PasswordChar = '*'
$password.Top = 100
$password.Left = 80

$form.Controls.Add($password)

$form.ShowDialog()

[ source ]

然后将文本转换为安全字符串:

$secstr = $password.Text | ConvertTo-SecureString -AsPlaintText -Force

如果您只想提示输入凭据,您可以使用 Get-Credential ,它已经将输入的密码存储为安全字符串:

PS C:\> $cred = Get-CredentialCmdlet Get-Credential an der Befehlspipelineposition 1Geben Sie Werte für die folgenden Parameter an:CredentialPS C:\> $cred.PasswordSystem.Security.SecureString

关于Powershell 文本框 SecureString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28071270/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com