首页 > 解决方案 > 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é。

任何想法 ?为什么我会收到此错误?

谢谢

标签: powershell

解决方案


您将无法轻松解密计算机上的示例字符串,因为Windows 使用本地用户和计算机帐户来“加密”密码。此过程使用 Windows数据保护 API-DPAPI。如果您需要使用ConvertFrom-SecureStringConvertTo-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

如果这只是为了在本地为自己保留凭据,那么这不是一个糟糕的选择。如果是其他用途,我更喜欢您可以通过代码安全使用的密码保险库。


推荐阅读