【问题标题】:Powershell - Password encryption/decryption with keyPowershell - 使用密钥加密/解密密码
【发布时间】:2021-06-08 19:21:45
【问题描述】:

我正在尝试使用 powershell 加密和解密密码。但是无法成功解密

我的代码

#Encryption
$KeyStoragePath="C:\Temp\Password"
$KeyFileName="AESKey.AES.Key"
$CreateKey = New-Object Byte[] 32
[Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($CreateKey)
$CreateKey | out-file ".\$KeyFileName"

$GetKey = Get-Content "$KeyStoragePath\$KeyFileName"
$CredentialsStoragePath = "C:\Temp\Password\"
$CredentialsFileName = "sec-string"
$PasswordSecureString = Read-Host -AsSecureString
$PasswordSecureString | ConvertFrom-SecureString -key $GetKey | Out-File -FilePath "$CredentialsStoragePath\$CredentialsFileName"

这成功创建了一个包含安全字符串的安全文件。但是,不能用密钥解密。

#Decrypt

$MyPasswordFile = "C:\Temp\Password\sec-string"

$MyPassword = Get-Content $PasswordFile | ConvertTo-SecureString -Key $CreateKey

错误

$MyPassword = Get-Content $PasswordFile | ConvertTo-SecureString -Key $CreateKey
ConvertTo-SecureString : Padding is invalid and cannot be removed.
At line:1 char:43
+ $MyPassword = Get-Content $PasswordFile | ConvertTo-SecureString -Key $CreateKey
+                                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [ConvertTo-SecureString], CryptographicException
    + FullyQualifiedErrorId : ImportSecureString_InvalidArgument_CryptographicError,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand

【问题讨论】:

    标签: powershell encryption


    【解决方案1】:

    在正确的地方使用正确的东西。您应该使用 key

    解密 password
    #Encryption
    $KeyStoragePath="C:\Temp\Password"
    $KeyFileName="AESKey.AES.Key"
    $CreateKey = New-Object Byte[] 32
    [Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($CreateKey)
    $CreateKey | out-file "$KeyStoragePath\$KeyFileName"
    
    $GetKey = Get-Content "$KeyStoragePath\$KeyFileName"
    $CredentialsStoragePath = "C:\Temp\Password\"
    $CredentialsFileName = "sec-string"
    $PasswordSecureString = Read-Host -AsSecureString
    $PasswordSecureString | ConvertFrom-SecureString -key $GetKey | Out-File -FilePath "$CredentialsStoragePath\$CredentialsFileName"
    
    #Decrypt
    
    $MyPasswordFile = "C:\Temp\Password\sec-string"
    
    
    $MyPassword = Get-Content $MyPasswordFile | ConvertTo-SecureString -Key $GetKey
    
    
    # Additional code below will change to plain text but this is bad practice. You should not need the plaintext password. You can create a credential from the secure string
    
    $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($MyPassword)
    $UnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
    

    【讨论】:

    • 谢谢@Sceptalist。我试过你的代码块,我没有看到我最初发布的错误。但是,它只返回这个 System.Security.SecureString
    • 这就是您在代码中要求的内容:$MyPassword = Get-Content $MyPasswordFile | ConvertTo-SecureString -Key $GetKey。如果你想要纯文本,你需要转换安全字符串。
    • 见上面的附加代码。但这是不好的做法。您不应该需要明文密码。您可以从安全字符串创建凭据
    猜你喜欢
    • 2011-05-09
    • 2016-10-15
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 2014-12-16
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多