C# Encryption to PHP Decryption

后端 未结 2 1662
难免孤独
难免孤独 2020-11-30 05:18

I\'m trying to encrypt some (cookie) data in C# and then decrypt it in PHP. I have chosen to use Rijndael encryption. I\'ve almost got it working, except only part of the te

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 05:26

    Since the string is partially OK, but there is gibberish at the end it would suggest a padding problem within the encryption which expects exact blocks of 256 bytes. I suggest setting the padding as PKCS7 (PaddingMode.PKCS7) instead of Zeros on the C# side which PHP will understand without issues (as it's the default mode on that parser).

    Edit: Oops, I did not notice that you had the following in your PHP:

    $enc = $_COOKIE["MyCookie"];
    

    This is the caveat. PHP is likely not getting the encrypted data as-is and is running some urldecode sanitizing. You should print this variable to see that it really matches what is being sent from the C# code.

    Edit2:

    Convert the whitespaces to missing + characters from the cookie by adding this:

    str_replace(' ', '+', $enc);
    

提交回复
热议问题