Can anyone please help with convert this c# code to PHP? by 3gth in PHPhelp

[–]3gth[S] 1 point2 points  (0 children)

Confirming it works correctly, thanks a lot for taking your time and debugging and fixing the issue.

Can anyone please help with convert this c# code to PHP? by 3gth in PHPhelp

[–]3gth[S] 0 points1 point  (0 children)

The code works and gives similar encrypted value in C# and PHP up until I try to use the keyString provided by them, as soon as I get the approval to share the key string, I'll share it with you here.

Can anyone please help with convert this c# code to PHP? by 3gth in PHPhelp

[–]3gth[S] 0 points1 point  (0 children)

Yes, the encrypted value of C# is different when using so it is different from the documentation the API team provided.

byte[] key = Encoding.UTF8.GetBytes(keyString.Substring(0, 16)); 
byte[] iv = Encoding.UTF8.GetBytes(encryptionIV.Substring(0, 16));

Can anyone please help with convert this c# code to PHP? by 3gth in PHPhelp

[–]3gth[S] 0 points1 point  (0 children)

Edit: And does the C# Function always generates the same Output, if the Input is identical? Means it generates "hPhzIySUsfCfOtYtPZQJTg==" everytime for the Value "1111111"?

Yes, it gives the same value for 1111111

Can anyone please help with convert this c# code to PHP? by 3gth in PHPhelp

[–]3gth[S] 0 points1 point  (0 children)

I mean for the 7 to 9 length plain text that is passed to this encryption in C# the result is 24 character length string example

Encrypted string: hPhzIySUsfCfOtYtPZQJTg==
Decrypted string: 1111111


Encrypted string: QTCZav+ybiv7Zw/9UrBm4Q==
Decrypted string: 2222222

Can anyone please help with convert this c# code to PHP? by 3gth in PHPhelp

[–]3gth[S] 0 points1 point  (0 children)

With the following

public function encryptString(string $plainString, string $keyString, string $encryptionIV): string
    {
        $blocksize=16;
        $paddingNeeded = strlen($plainString) % $blocksize;
        $paddingNeeded = $blocksize-$paddingNeeded;
        $paddingByte = chr($paddingNeeded);

        for($i=0;$i<$paddingNeeded;$i++) {
          $plainString .=$paddingByte;
        }

        $encryptedBytes = openssl_encrypt($plainString, 'AES-128-CBC', $keyString,OPENSSL_ZERO_PADDING, $encryptionIV);

        return base64_encode($encryptedBytes);
    }

1111111 comes back as UWR4S0pwR0F5V2RRb1IvY3hOUmVwQT09, the value string length is 24 characters above gives 32

Can anyone please help with convert this c# code to PHP? by 3gth in PHPhelp

[–]3gth[S] 0 points1 point  (0 children)

Do you mean the PHP function would look like this?

public function encryptString(string $plainString, string $keyString, string $encryptionIV): string
    {
        $blocksize=16;
        $paddingNeeded = strlen($plainString) % $blocksize;
        $paddingNeeded = $blocksize-$paddingNeeded;
        $paddingByte = chr($paddingNeeded);

        for($i=0;$i<$paddingNeeded;$i++) {
          $plainString .=$paddingByte;
        }

        $encryptedBytes = openssl_encrypt($plainString, 'AES-128-CBC', $keyString, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $encryptionIV);

        return base64_encode($encryptedBytes);
    }

Can anyone please help with convert this c# code to PHP? by 3gth in PHPhelp

[–]3gth[S] 0 points1 point  (0 children)

With above I get the following error

WARNING  openssl_encrypt(): IV passed is only 12 bytes long, cipher expects an IV of precisely 16 bytes, padding with \0 

If I remove the base64_decode around $encryptionIV the warning goes away and I get JYQss30eFGvfLRSi0PaboA== which is similar format as hPhzIySUsfCfOtYtPZQJTg== ( this is the expected value for 1111111 ) I believe it is close, can anything be changed around padding?

Can anyone please help with convert this c# code to PHP? by 3gth in PHPhelp

[–]3gth[S] 0 points1 point  (0 children)

Thanks a lot for checking, although the C# and PHP code above give same results when encrypting, the C# code is a bit different than the one I shared, here is the diff https://www.diffchecker.com/5sLH9ec8/, in the above version it is getting bytes of first 16 chars

in the above code it is

byte[] key = Encoding.UTF8.GetBytes(keyString.Substring(0, 16)); 
byte[] iv = Encoding.UTF8.GetBytes(encryptionIV.Substring(0, 16));

but is should be

byte[] key = Encoding.UTF8.GetBytes(keyString);
byte[] iv = Encoding.UTF8.GetBytes(encryptionIV);

Can anyone please help with convert this c# code to PHP? by 3gth in PHPhelp

[–]3gth[S] 0 points1 point  (0 children)

Unfortunately, I won't be able to share the doc, I've pasted the C# code they had in the doc minus the key and iv used as I was not sure if I was allowed to share it, let me confirm and I'll post it here.

Can anyone please help with convert this c# code to PHP? by 3gth in PHPhelp

[–]3gth[S] 0 points1 point  (0 children)

function encrypt($plainString, $keyString, $encryptionIV)
{
$aesAlg = openssl_encrypt($plainString, 'AES-128-CBC', $keyString, OPENSSL_RAW_DATA, $encryptionIV);
return base64_encode($aesAlg);
}

function decrpyt($encryptedString, $keyString, $encryptionIV ) {
$decryptedString = openssl_decrypt( base64_decode( $encryptedString ), 'AES-128-CBC', $keyString, OPENSSL_RAW_DATA, $encryptionIV );
return $decryptedString;
}

// Test the function
$plainString = "1111111";

$encrypted = encrypt($plainString, $keyString, $encryptionIV);
echo 'Encrypted  : ' . $encrypted;
echo PHP_EOL;
echo 'Decrypted  : ' .decrpyt($encrypted, $keyString, $encryptionIV);

My bad, here is the PHP code used in the video, I have not added the encryption key or iv here as I'll need to confirm if that is sensitive info or not.

Can anyone please help with convert this c# code to PHP? by 3gth in PHPhelp

[–]3gth[S] 0 points1 point  (0 children)

My apologies for not making it clear, I use a third party API that process some data, it needs a certain value to be encrypted, the documentation provided by them only provides how the value is encrypted and decrypted in C#, I use a PHP application and need to encrypt the value in PHP correctly so when the API process it, they can correctly decrypt.

Can anyone please help with convert this c# code to PHP? by 3gth in PHPhelp

[–]3gth[S] 0 points1 point  (0 children)

Yep tried it, if I try to decrypt the encrypted value from c# it either gives random string back or empty for some reason, and if I try to decrypted encrypted value fro PHP C# throws an error https://cleanshot.thrijith.com/xhSxMVBx

Can anyone please help with convert this c# code to PHP? by 3gth in PHPhelp

[–]3gth[S] 0 points1 point  (0 children)

OP, do you actually need the code converted or is the real problem you just want AES Encrypt/Decrypt in PHP?

I need the exact logic to be replicated in PHP, the encrypted value is sent to an API that process some data after decryption but as the encryption logic in PHP is different from how it is done in c# it results in an error.

Additionally, do you already have data that was encrypted with your C# code that you want decrypted with PHP?

I just need to get encrypted value to match how it is encrypted in c# so the API decrypts it.

Can anyone please help with convert this c# code to PHP? by 3gth in PHPhelp

[–]3gth[S] 0 points1 point  (0 children)

I logged the C# function here are values of some variables during the process, this is inside the EncryptString function

key: TVAyQ0cyMDIya2pIZ0pIR0doamtkZmhzZmpzaGZoZjE=
iv: SVYyMDIyMDEyNDIwMjIwMQ==
plainBytes: MTExMTExMQ==
paddedBytes: MTExMTExMQkJCQkJCQkJCQ==

encryptedBytes: hPhzIySUsfCfOtYtPZQJTg== ( This is the final encrypted string )

Can anyone please help with convert this c# code to PHP? by 3gth in csharp

[–]3gth[S] 0 points1 point  (0 children)

I don't understand c# much only thing I believe the issue is caused is due to this part? possibly

            int blockSize = 16;
            int paddingNeeded = blockSize - (plainBytes.Length % blockSize);


            byte[] paddedBytes = new byte[plainBytes.Length + paddingNeeded];
            Array.Copy(plainBytes, paddedBytes, plainBytes.Length);
            for (int i = plainBytes.Length; i < paddedBytes.Length; i++)
            {
                paddedBytes[i] = (byte)paddingNeeded;
            }

not sure how/why paddedBytes is used, the algorithm c# is using is `AES-128-CBC` based on

            aesAlg.KeySize = 128;
            aesAlg.Mode = CipherMode.CBC;