Porting C # AES 256 decryption to PHP

I am trying to pass this C # code to PHP:

private static string DecryptString(string content, string password)
{
    Rijndael aes;
    byte[] retVal = null;
    byte[] contentBytes;
    byte[] passwordBytes;
    byte[] ivBytes;

    try {
        //Get the content as byte[]
        contentBytes = Convert.FromBase64String(content);

        //Create the password and initial vector bytes
        passwordBytes = new byte[32];
        ivBytes = new byte[16];

        Array.Copy(Encoding.Unicode.GetBytes(password), passwordBytes, Encoding.Unicode.GetBytes(password).Length);
        Array.Copy(passwordBytes, ivBytes, 16);

        //Create the cryptograpy object
        aes = Rijndael.Create();
        aes.Key = passwordBytes;
        aes.IV = ivBytes;
        aes.Padding = PaddingMode.PKCS7;

        string mode = aes.Mode.ToString();

        //Decrypt
        retVal = aes.CreateDecryptor().TransformFinalBlock(contentBytes, 0, contentBytes.Length);
    }
    catch {}

    return Encoding.Unicode.GetString(retVal);
}

The parameter contentis a long string with a length of 44 characters, the encoding with the base is 64, the parameter passwordis a long string with a length of 6 characters.

This is the PHP code I compiled:

$content = "[44 char base 64 encoded string]";
$password = "[6 char password]";

$padding = 32 - (strlen($password) % 32);
$password .= str_repeat(chr($padding), $padding);

$iv = substr($password, 0, 16);

$data = base64_decode($content);

$decrypted  = openssl_decrypt($data, 'AES-256-CBC', $password, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $iv);

The result of C # code is 10 characters. The result of PHP is a 32-bit long bullshit - I think binary data.

Can someone help me fix this code, or do I have an idea what I can try?

0
source share
1 answer

As zaph mentioned in the comments, this code is not considered very safe, and I advise you not to use it in a production environment.
At the same time, there is the equivalent of PHP for your C # code:

function DecryptString($content, $password){
    $password = mb_convert_encoding($password, "utf-16le");
    $padding = 32 - (strlen($password) % 32);
    $password .= str_repeat("\0", $padding);
    $iv = substr($password, 0, 16);

    $data = base64_decode($content);
    $decrypted  = openssl_decrypt($data, 'AES-256-CBC', $password, OPENSSL_RAW_DATA, $iv);
    $decrypted = mb_convert_encoding($decrypted, "utf-8", "utf-16le");
    return $decrypted;
}

PHP:

$password = "012345";
$content = "EJWgZ/26wp+Cb5utbM1aMk8XfqPQide4dzjQzzzYfj8=";
echo DecryptString($content, $password);

0123456789

#:

string password = "012345";
string content = "EJWgZ/26wp+Cb5utbM1aMk8XfqPQide4dzjQzzzYfj8=";
Console.WriteLine(so.DecryptString(content, password));

0123456789

:
Unicode # Little Endian UTF-16; mb_convert_encoding .
openssl_decrypt PKCS.
( Rfc2898DeriveBytes - hash_pbkdf2)
IV ( Aes.GenerateIV - openssl_random_pseudo_bytes)

:
# Unicode php
php - mb_convert_encoding
php - openssl_decrypt

0

Source: https://habr.com/ru/post/1687609/


All Articles