我有下面的代码,没有任何错误。
SaveKeyPass.ps1
]
GetKeyPass.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"
问题1:
如果我在SaveKeyPass.ps1中更改第一行(只有最后一位数字从7更改为8)并执行此脚本
$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"
然后执行GetKeyPass.ps1我收到此错误
$key = "1234567891234568"
问题2:
如果我在SaveKeyPass.ps1中将第一行(密钥长度从16字节更改为32字节)更改为并执行此脚本
ConvertTo-SecureString : Padding is invalid and cannot be removed. At [**]:11 char:28
然后执行GetKeyPass.ps1我收到此错误
$key = "12345678912345671234567891234567"
我对发生的事情一无所知?在问题1 中,只更改了一个数字,因此无法确定抛出填充异常的位置。在 issue 2 中,我有32个字节(256位)密钥,但异常是抱怨密钥长度不正确。
任何帮助将不胜感激。谢谢你的阅读!
答案 0 :(得分:0)
感谢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位。