powershell - Powershell密码解密
问题描述
我读过这个博客:
https://gallery.technet.microsoft.com/scriptcenter/Encrypt-Password-and-use-dd07f253
我已经测试了这行代码:
$encrypted = "01000000d08c9ddf0115d1118c7a00c04fc297eb010000000ffb51b2e604034a982598093219de570000000002000000000003660000c0000000100000004387d7a6f294bdcf25725159f7edaa390000000004800000a000000010000000efac6eb2cb924bf1cf893a9ddfdeeff71800000079b98b725901857051e84b78ba6e20da94752516a02833c114000000dbf49438a6ea07c06c3846fc23e725a081792782"
$password = ConvertTo-SecureString -string $encrypted
我收到此错误:
ConvertTo-SecureString : Clé non valide pour l'utilisation dans l'état spécifié。
任何想法 ?为什么我会收到此错误?
谢谢
解决方案
您将无法轻松解密计算机上的示例字符串,因为Windows 使用本地用户和计算机帐户来“加密”密码。此过程使用 Windows数据保护 API-DPAPI。如果您需要使用ConvertFrom-SecureString
或ConvertTo-SecureString
跨多个机器/帐户,我不建议这样做,那么您必须指定一个键(请参阅函数的参数)。然后你在哪里保护密钥?出色地...
这篇文章只是一个如何使用它的例子。您可以复制并粘贴以下两个代码块来对其进行采样,而无需进行任何文件检查或进一步的复制和粘贴。
$SecureString = Read-Host -Prompt "Enter your Password" -AsSecureString
$EncryptedString = ConvertFrom-SecureString $SecureString
$EncryptedString | Out-File .\Clowns.txt # Because who would look inside, right?
要将密码重新输入到文章中的可用凭证变量中,您可以这样做。
$EncryptedString = Get-Content .\Clowns.txt
$SecureString = ConvertTo-SecureString $EncryptedString
$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList "username",$SecureString
另一个选项,这里是用来Export-Clixml
保存凭据文件,它对一组凭据执行相同的 DPAPI 操作。
$SecureString = Read-Host -Prompt "Enter your Password" -AsSecureString
$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList "username",$SecureString
$Credential | Export-Clixml -Path .\Dolphins.xml # Horrible, horrible beings.
然后,您可以使用在同一台计算机上“加密”它们的相同用户帐户Import-Clixml
来恢复凭据。
$Credential = Import-Clixml -Path .\Dolphins.xml
如果这只是为了在本地为自己保留凭据,那么这不是一个糟糕的选择。如果是其他用途,我更喜欢您可以通过代码安全使用的密码保险库。
推荐阅读
- java - 添加“为空”条件时,LocalDateTime 停止工作
- r - 日期在一个区间内并拉取字段
- sql - 如何从不包括子树的嵌套集中选择路径
- ruby - 如何只等待数组中的第一个线程在 Ruby 中完成?
- c# - 在有父子关系的表中批量插入数据
- hacklang - 在 Hack 中,这些叫什么/它们是如何工作的:<
> 方法或类? - c++ - 如何防止程序使用 C++ 运行 IF 和 ELSE 语句?
- ionic-framework - Ionic Pro - 离子链接失败
- python - 如何在 Python 中使用 .format() 在“for”循环中打印列表?
- excel - 错误转到/如果没有错误如何跳过?