У меня есть код, который работает без ошибок.SecureString с ключами SecureKey
- SaveKeyPass.ps1 хранит в защищенном ключ (зашифрованный) и пароль (с шифрованием с использованием SecureKey)
- 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:
Если изменить первую строку (только последняя цифра изменяется от 7 до 8) в SaveKeyPass.ps1, чтобы и выполнить этот скрипт
$key = "1234567891234568"
, а затем выполнить GetKeyPass.ps1 я получаю эту ошибку
ConvertTo-SecureString : Padding is invalid and cannot be removed. At [**]:11 char:28
Выпуск 2:
Если изменить первая линия (длина ключа изменяется от 16 байт до 32 байт) в SaveKeyPass.ps1 чтобы и выполнить этот скрипт
$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 изменяется только одна цифра, поэтому не уверен, откуда выбрано исключение заполнения. В issue 2 У меня есть 32-байтовый (256 бит) ключ, но исключение жалуется на неправильную длину ключа.
Любая помощь будет оценена по достоинству. Спасибо за прочтение!
Что такое '$ decryptedSecureKeyFromFile'? –
Как указал Матиас, ваша проблема находится на 11-й строке и, похоже, связана с переменной времени выполнения, не указанной в вашем коде. – Djarid