PowerShell: 隐藏明文密码

本文描述如何加密 PowerShell 中的敏感信息,从而避免敏感信息(诸如密码)在代码中被显式的展现出来。这里我们使用 ConvertFrom-SecureString 命令来完成加密操作,需要注意的是,如果我们没有指定Key,那么,将采用基于 Windows 内置的 DPAPI 进行数据的加密。这种情况下,原文的加密和解密必须在同一台机器上,且基于同一个User去进行。

ConvertFrom-SecureString [-SecureString] <SecureString> [[-SecureKey] <SecureString> ] [ <CommonParameters>]

你可以从这里了解该命令的更多信息。

* If no key is specified, the Windows Data Protection API (DPAPI) is used to encrypt the standard string representation.
Your secret was automatically encrypted by the built-in Windows data protection API (DPAPI), using your identity and your machine as encryption key. So only you (or any process that runs on your behalf) can decipher the secret again, and only on the machine where it was encrypted.

加密方式:

[ps]
write-host "Please enter your plain text. (Run this script as Administrator)"
$plainText = Read-Host | ConvertTo-SecureString -AsPlainText -force
$encryptedText = $plainText | ConvertFrom-SecureString
write-host $encryptedText
[/ps]

解密方式:

[ps]
$password = $encryptedText | ConvertTo-SecureString
$cred = New-Object -TypeName System.Management.Automation.PSCredential("JustGiveAName", $password)
$cred.GetNetworkCredential().Password
[/ps]

PowerShell Encrypted

查看原文:http://nap7.com/me/powershell-hide-plain-password/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值