0

I'm trying to decrypt a cipher, which has been encrypted in PHP, using RSA in C#. Here is my C# code:

public byte[] Decrypt(byte[] dataToDecrypt)
{
    byte[] plain;

    using (var rsa = new RSACryptoServiceProvider(2048))
    {
        rsa.PersistKeyInCsp = false;

        rsa.ImportParameters(PrivateKey);
        plain = rsa.Decrypt(dataToDecrypt, false);
    }

    return plain;
}

And the PHP code is:

public static function encrypt($dataToEncrypt, $userPublicKey) {
   if (openssl_public_encrypt($dataToEncrypt, $encrypted, $userPublicKey, OPENSSL_PKCS1_PADDING)) {
       return $encrypted;
   } else {
       throw new Exception('Unable to encrypt data. Perhaps it is bigger than the key size?');
   }
}

I get no exceptions or errors in C#. The problem is that if I convert the byte array output of the Decrypt function to a string (i.e. using System.Text.UTF8Encoding.GetString()) the decrypted string is very different from the original plain text passed to 'encrypt' function in PHP.

Any idea why they are different? What am I missing?

p.s. I'd like to add that I have code to encrypt data in C# and decrypt it back in PHP, in a very similar fashion with no problems. I cannot just get it to work the other way around, as I explained above. Thanks

=== UPDATE ===

The input data - i.e. $dataToEncrypt is:

$sessionKey = mcrypt_create_iv(32, MCRYPT_DEV_URANDOM);

The public and private keys are:

-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAzMzT6e4CVC11E1w9bXhnIVgZIAfs5McBeewW3bZtS8NBelHs
HyxiZjkHgMZp6BGSsux0gl0e+DkeISHe5dOQEX7WJBaARxiGJxTzhEo556rrmSXz
7h5TSht/ivTWPZTYfAaYwLC71C/IKWfRPGSVuqHTlflVc0JSUF3M6dgWPqLRF7G3
qqYFdx3+2CoVyCQkNogvkICZNbGGtx7RlGHJ1LCeCyEdC+6AIyNItBbXGtBOf/IZ
bdL+QioWCV/VzQAhm01QVn8g6CHiCMvAIMMj2RXUg/1tL2vw0QNS2ZJgfsM3x67s
M47T1zHj/OTaX6lAp77FHkof1tKmTwlV1RSj7wIDAQABAoIBAGHMbmYHgpUwH/he
0/ayyIzjxRvGrRmS7uGKHGFD/G1xkJMZC+j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QI4PRE90iauZ0DYGJyxY1jHqa0X64W/5nZqH/uv58+Hf6uv47l3n3n0T
58WFfKgXY5AaLCbi1UERdeHKPXNXDFkTKuuOb190M+RNbGgzwN9fcQ==
-----END RSA PRIVATE KEY-----

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA498EWxuZK/KsUgIEusEt
QOJulgTHwb8C4avtzJnzhosTeKooXvyGFPpex6HcQGSRqrWpNr2yhw1BvJvH2UyE
Jisl5BJA5Za+ofmbGifCFwCllZ37U1YpOmqpB2Yt+yYElGh5dp+lqs5Q3u3nPknd
nLS3bxH7qlZBvR9YPWj9x7IuSXJyopAmdJato8xeNHzmBxWD8FgQKICFpLtGsPXq
XRwT0imTs6/EcMqq6fdlp0OyBKyZjw6t47gMeqiuSYz6k41Nf/SbtIC4snUyoUgI
TvnHjWe1cY7js4kY62A9ZpHX0NpG7JXctxVb+aZOv1rS36bUjcP+bug1W3ZKrTG6
hwIDAQAB
-----END PUBLIC KEY-----

So for example if the input ($dataToEncrypt) in base64 string is :

VPA13tGd8HMlaB3yKxlFBb8/dAXlCjX5Rr1SzE4L6TY=

I get this output (plain) in base64 string is:

UTgfMQfl82FV4D4HfHPiwmo7jOygBmtXBap0Hn+fAoT4zZcGmLXiY1rGIhmEr90tNxzTGHhknx6PhgJ5eZDK2c9aWi/gD3FJz8MksRjbOkX09GMabcdekdwcyvu9JEWJZi2wOSaRLTRkf90uOArq7pG1g8CPwPLkghj39nUF+XYdGyAB4SXmKkPHshg+dk1acEaXhj9mnAF2fluDEJnfQEU=    
CloudWindMoonSun
  • 372
  • 1
  • 4
  • 13
  • What is "PrivateKey" in your C# code? Are you sure you're using the correct key pair in both code snippets? – Luke Joshua Park Jun 25 '17 at 00:36
  • This can probably not be answered without provding us the input and output including test keys. – Maarten Bodewes Jun 25 '17 at 01:03
  • See : https://stackoverflow.com/questions/28014976/c-sharp-rsa-encrypt-php-rsa-decrypt – jdweng Jun 25 '17 at 02:18
  • There is a limit of less than 256-bytes of data that can be encrypted with a 2048-bit RSA key. RSA is typically not used for encryption data. – zaph Jun 25 '17 at 02:54
  • @LukePark, The keys must be correct and paired, because I can successfully use them to encrypt in C# and decrypt in PHP. – CloudWindMoonSun Jun 26 '17 at 21:56
  • @MaartenBodewes, I updated the question with the input and keys. – CloudWindMoonSun Jun 26 '17 at 21:56
  • @jdweng, The question you pointed at is using 3rd party library 'phpseclib', which I'm not keen using it due to their licensing. – CloudWindMoonSun Jun 26 '17 at 21:56
  • @zaph, what I'm trying to encrypt is basically the SessionKey, which is only 32 bytes. – CloudWindMoonSun Jun 26 '17 at 21:57
  • 1
    Sorry, but what you posted as output is what exactly? Ciphertext generated by PHP? By C#? – Maarten Bodewes Jun 26 '17 at 22:19
  • @MaartenBodewes the output is what my decryption function in C# returns (in base64 string). I added it to the question so we can see how different it is to the input value in PHP (before encryption) – CloudWindMoonSun Jun 26 '17 at 22:59
  • Could you also encode the ciphertext? What you get seems to be garbage, but with the test key we can try and decrypt the ciphertext and see where the issue is. The size is really weird as well. – Maarten Bodewes Jun 26 '17 at 23:17
  • @MaartenBodewes, sorry I'm not sure if I get what you mean. Do you want me to test something for you? My problem here is that I need to encrypt data in PHP and then decrypt them in C#. Although my decryption function throws no errors but it returns garbage. I have similar functions where I encrypt in C# and successfully decrypt in PHP. But I cannot get the other way around working. – CloudWindMoonSun Jun 26 '17 at 23:28

0 Answers0