【问题标题】:SecureString With SecureKey Issues具有 SecureKey 问题的 SecureString
【发布时间】:2016-03-31 17:28:59
【问题描述】:

我有下面的代码,没有任何错误。

  1. SaveKeyPass.ps1 存储安全密钥(加密)和密码(使用安全密钥加密)
  2. GetKeyPass.ps1 从文件中获取安全密钥和密码,然后解密安全密钥,最后使用解密的安全密钥解密密码。

SaveKeyPass.ps1

$key = "1234567891234567" 
$textPassword = "securekey-textpassword"
$securePassword = ConvertTo-SecureString $textPassword -AsPlainText -Force
$secureKey = ConvertTo-SecureString $Key -AsPlainText -Force
$encryptedKey = ConvertFrom-SecureString $SecureKey -Key (1..16)
$encryptedPassword = ConvertFrom-SecureString  $SecurePassword -SecureKey $decryptedSecureKeyFromFile
$encryptedKey | Out-File "C:\temp\securekey-enckey.txt"
$encryptedPassword | Out-File "C:\temp\securekey-encpass.txt"


Write-Host "Key: $Key"
Write-Host "Text Password: $textPassword"
Write-Host "Encrypted Password: $encryptedPassword"
Write-Host "Encrypted Key: $encryptedKey"

GetKeyPass.ps1

$key = ""
$textPassword = ""
$encryptedPasswordFromFile = ""
$encryptedKeyFromFile = ""
$secureDecryptedPassword = ""
$BSTR1= ""
$BSTR2= ""
$encryptedKeyFromFile = Get-Content "C:\temp\securekey-enckey.txt"
$encryptedPasswordFromFile = Get-Content "C:\temp\securekey-encpass.txt"
$secureDecryptedKey = ConvertTo-SecureString $encryptedKeyFromFile -Key (1..16)
$secureDecryptedPassword = ConvertTo-SecureString  $encryptedPasswordFromFile -SecureKey $secureDecryptedKey


$BSTR1 = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureDecryptedPassword)
$textPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR1)

$BSTR2 = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureDecryptedKey)
$key = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR2)


Write-Host "Key: $key"
Write-Host "Text Password: $textPassword"
Write-Host "Encrypted Password: $encryptedPasswordFromFile"
Write-Host "Encrypted Key: $encryptedKeyFromFile"

问题 1:

如果我将 SaveKeyPass.ps1 中的第一行(仅最后一位从 7 更改为 8)更改为并执行此脚本

$key = "1234567891234568"  

然后执行 GetKeyPass.ps1 我得到这个错误

ConvertTo-SecureString : Padding is invalid and cannot be removed.
At [**]:11 char:28

问题 2:

如果我将 SaveKeyPass.ps1 中的第一行(密钥长度从 16 字节更改为 32 字节)更改为并执行此脚本

$key = "12345678912345671234567891234567"  

然后执行 GetKeyPass.ps1 我得到这个错误

The specified key is not valid. Valid key length settings are either 128 bits, 192 bits, or 256 bits.
At [**]:11 char:28

我真的不知道发生了什么?在 issue 1 中仅更改了一位数字,因此不确定从何处引发填充异常。在问题 2中,我有 32 字节(256 位)的密钥,但异常是抱怨密钥长度不正确。

任何帮助将不胜感激。感谢阅读!

【问题讨论】:

  • $decryptedSecureKeyFromFile 是什么?
  • 正如 Mathias 指出的那样,您的问题在第 11 行,并且似乎源于您的代码中未引用的运行时变量。

标签: powershell encryption securestring


【解决方案1】:

感谢 Martin 和 Djarid 的现场,我已将 SaveKeyPass.ps1 中的第 11 行更正为

$encryptedPassword = ConvertFrom-SecureString  $SecurePassword -SecureKey $secureKey

已解决问题 1 完全问题 2 部分。 对于问题 2:

我注意到密钥中的 1 个字符/数字是 16 位(可能在我的 64 位机器上),这意味着“12345678912345671234567891234567”是 512 位而不是 256 位,我认为 1 个字符/数字是 8 个字节。因此,这违反了密钥的最大长度要求并且失败了。

这意味着如果我在密钥中提供 8、12、16 个字符,它们分别是 128 位、192 位和 256 位。

【讨论】:

  • 与你的机器无关。 PowerShell 中的字符串是 16 位 Unicode,是 .NET 的 System.String 类的实例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多