我有下面的代码,工作没有任何错误。
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 --我得到此错误 转换到-SecureString:填充无效,不能删除。地址:**:11 char:28
第2期:
如果我将SaveKeyPass.ps1中的第一行(键长从16字节更改为32字节)改为并执行此脚本 $key =“1234567891234567123453458912345667”并随后执行GetKeyPass.ps1 I得到此错误 指定的键无效。有效的密钥长度设置为128位、192位或256位。地址:**:11 char:28
我真的不知道这是怎么回事?在问题1中,只更改了一位数,因此不确定从何处抛出填充异常。在问题2中,我有32字节(256位)密钥,但异常是抱怨密钥长度不正确。
任何帮助都将不胜感激。感谢您的阅读!
发布于 2016-03-31 09:28:59
谢谢马丁和贾里德的现场报道,我已经更正了SaveKeyPass.ps1中的第11行
$encryptedPassword = ConvertFrom-SecureString $SecurePassword -SecureKey $secureKey它解决了问题1、完全和问题2部分。关于第2期:
我注意到,1字符/数字的键是16位(可能在我的64位机器上),这意味着“123456712345345345678912345345345345345345345345667”是512位,而不是256位,我认为1字符/位是8字节。因此,这违反了键的最大长度要求,并失败了。
这意味着如果我提供8,12,16个字符,它们分别是128位,192位和256位。
https://stackoverflow.com/questions/36303829
复制相似问题