title: C# encrypt PHP decrypt using RSA
body: <p>I'm trying to build a simple RSA encryption decrpytion process between C# and PHP.
  I've accomplished encrypting in PHP and decrpyt in C# with phpseclib(<a href="http://phpseclib.sourceforge.net/" rel="nofollow">http://phpseclib.sourceforge.net/</a>). However I'm getting 
  "Decryption error in C:\xampp\htdocs\Crypt\RSA.php on line 2103" which is this part:</p>
  
  <pre><code>if ($lHash != $lHash2) {
          user_error('Decryption error', E_USER_NOTICE);
          return false;
      }
  </code></pre>
  
  <p>the encryption in C# I used this bunch of code:</p>
  
  <pre><code>RSACryptoServiceProvider rsaCryptoServiceProvider = new RSACryptoServiceProvider(dwKeySize);
          rsaCryptoServiceProvider.FromXmlString(publickey);
          int keySize = dwKeySize / 8;
          byte[] bytes = Encoding.UTF32.GetBytes(inputString);
          // The hash function in use by the .NET RSACryptoServiceProvider here is SHA1
          // int maxLength = ( keySize ) - 2 - ( 2 * SHA1.Create().ComputeHash( rawBytes ).Length );
          int maxLength = keySize - 42;
          int dataLength = bytes.Length;
          int iterations = dataLength / maxLength;
          StringBuilder stringBuilder = new StringBuilder();
          for (int i = 0; i &lt;= iterations; i++)
          {
              byte[] tempBytes = new byte[(dataLength - maxLength * i &gt; maxLength) ? maxLength : dataLength - maxLength * i];
              Buffer.BlockCopy(bytes, maxLength * i, tempBytes, 0, tempBytes.Length);
              byte[] encryptedBytes = rsaCryptoServiceProvider.Encrypt(tempBytes, true);
              // Be aware the RSACryptoServiceProvider reverses the order of encrypted bytes after encryption and before decryption.
              // If you do not require compatibility with Microsoft Cryptographic API (CAPI) and/or other vendors.
              // Comment out the next line and the corresponding one in the DecryptString function.
              Array.Reverse(encryptedBytes);
              // Why convert to base 64?
              // Because it is the largest power-of-two base printable using only ASCII characters
              stringBuilder.Append(Convert.ToBase64String(encryptedBytes));
          }
          string ciphertext = stringBuilder.ToString();
  </code></pre>
  
  <p>and my basic PHP code to decrpyt:</p>
  
  <pre><code>$rsa-&gt;loadKeyfromXML($privatekey);  
  $ciphertext = file_get_contents('cipher.txt');
  $ciphertext = base64_decode(strrev($ciphertext));
  
  //$rsa-&gt;setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
  
  $plaintext = $rsa-&gt;decrypt($ciphertext);
  </code></pre>
  
  <p>I've tried PKC1 it also gave another error in Crypt/RSA.php</p>