C# RSA Encrpytion -> Laravel phpseclib decrypt() C# RSA Encrpytion -> Laravel phpseclib decrypt() laravel laravel

C# RSA Encrpytion -> Laravel phpseclib decrypt()


In the C# code PKCS#1 v1.5 padding is applied (2nd parameter of Encrypt() is false), phpseclib uses OAEPadding by default (here).

Decryption works if PKCS#1 v1.5 padding is explicitly specified in the PHP code:

$message = $private->withPadding(RSA::ENCRYPTION_PKCS1)->decrypt(base64_decode($encoded_message));

Alternatively, OAEPadding could be used in the C# code. But be careful, if in Encrypt() the 2nd parameter is set to true, .NET uses SHA-1 for both digests by default. However, phpseclib applies SHA-256 for both digests by default. Therefore, in the C# code, for compatibility with the PHP code, both digests must be explicitly set to SHA256 (this is not possible with RSACryptoServiceProvider, but with RSACng; also, the overload of Encrypt() is needed, with which the digest can be set explicitly).