1

появилась задача зашифровать текст на сервере (php) и расшифровать его скриптом powershell. Пытаюсь найти общие функции для шифрования/дешифрования по ключу. Ничего рабочего найти не могу. Различные костыли кода с Rijndael256ECB в PS и mcrypt_encrypt в php дают разный результат.

Подскажите пожалуйста, что можно использовать? Мой не рабочий код (найдено на просторах интернета):

powershell( PS v.4 Win7)

function Encrypt-Rijndael256ECB {
    param(
        [byte[]]$Key,
        [string]$Plaintext
    )

    $RijndaelProvider = New-Object -TypeName System.Security.Cryptography.RijndaelManaged

    # Set block size to 256 to imitate MCRYPT_RIJNDAEL_256
    $RijndaelProvider.BlockSize = 256
    # Make sure we use ECB mode, or the generated IV will fuck up the first block upon decryption
    $RijndaelProvider.Mode      = [System.Security.Cryptography.CipherMode]::ECB
    $RijndaelProvider.Key       = $key

    # This object will take care of the actual cryptographic transformation
    $Encryptor = $RijndaelProvider.CreateEncryptor()

    # Set up a memorystream that we can write encrypted data back to
    $EncMemoryStream = New-Object System.IO.MemoryStream
    $EncCryptoStream = New-Object System.Security.Cryptography.CryptoStream -ArgumentList $EncMemoryStream,$Encryptor,"Write"
    $EncStreamWriter = New-Object System.IO.StreamWriter -ArgumentList $EncCryptoStream

    # When we write data back to the CryptoStream, it'll get encrypted and written back to the MemoryStream
    $EncStreamWriter.Write($Plaintext)

    # Close the writer
    $EncStreamWriter.Close()
    # Close the CryptoStream (pads and flushes any data still left in the buffer)
    $EncCryptoStream.Close()
    $EncMemoryStream.Close()

    # Read the encrypted message from the memory stream
    $Cipher     = $EncMemoryStream.ToArray() -as [byte[]]
    $CipherText = [convert]::ToBase64String($Cipher)

    # return base64 encoded encrypted string
    return $CipherText
}

$enc = [system.Text.Encoding]::UTF8
$string1 = "1234567890101212"
$key = $enc.GetBytes($string1)
$string = "123"

Encrypt-Rijndael256ECB  $key $string
pause

Вывод: zWbrs5FtqqRUtbjgKHsXMmjDtZ+itZU5yJF6z2MxxZM=

PHP (Apache 2.2. + php 7.1)

<?
$key = "1234567890101212";
$to_encrypt = "123";
$decrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $to_encrypt, MCRYPT_MODE_ECB);
echo ( base64_encode($decrypted) );
?>

Вывод: pmZ0YSUQMUd2O1WF2g4cMT4YBI80D3c6qL+tqSVZHCs=

И вообще, mcrypt_encrypt уже устарел. Спасибо!

1 ответ 1

2

mcrypt_encrypt использует padding с нулями, а .NET по умолчанию ставит PKCS7. В павершел вам нужно добавить 1 строчку:

$RijndaelProvider.Padding = [System.Security.Cryptography.PaddingMode]::Zeros
1
  • Боже мой, вы мозг. Большое спасибо!
    – Waik
    17 авг 2019 в 21:28

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.