更新时间:2023-01-02 11:26:05
通过将密码作为安全字符串并将其作为加密标准字符串"保存到名为类似名称的文件中,我设法解决了类似的问题"Account.User.pwd" 其中 Account 是与密码关联的帐户名称,User 是生成安全字符串的用户(只有该用户才能解密该文件).
I have managed to get around similar problems by taking the password as a secure string and saving it, as an "encrypted standard string" to a file named something like "Account.User.pwd" where Account is the name of the account associated with the password and User is the user who generated the secure string (only this user will be able to decrypt that file).
这与@Keith 使用的方法非常相似,如果您点击他对上述问题本身的评论中的链接.下面的方法更容易遵循,因为它不直接使用 [System.Runtime.InteropServices.Marshal]
类.
This is pretty similar to the approach used by @Keith if you follow the link in his comment on the question itself above. The approach below is a little easier to follow as it doesn't play directly with the [System.Runtime.InteropServices.Marshal]
class.
将密码从文件内容转换回更复杂,但这里是一个逐步完成该过程的示例:
Converting the password back from the contents of the file is more involved, but here is an example that steps through the process:
# Create credential object by prompting user for data
$Credential = Get-Credential
# Encrypt the password to disk
$Credential.Password | ConvertFrom-SecureString | Out-File Account.User.pwd
# Now to read it back again...
$SecureString = Get-Content Account.User.pwd | ConvertTo-SecureString
# Create credential object programmatically
$NewCred = New-Object System.Management.Automation.PSCredential("Account",$SecureString)
# Reveal the password!
$NewCred.GetNetworkCredential().Password
关于这种方法的想法:
更多信息: