title: How to decrypt JWE source (encrypted with RSA1_5 A256CBC-HS512) in C#?
body: <p>I am implementing a client for communicate with some server by the cryptological way. The client sends get request with public RSA key to the server. Documentation &quot;how to communicate with server&quot; has the sample with java code. The following code generates the public key:</p>
  <pre><code>KeyPairGenerator keyGen = KeyPairGenerator.getInstance(&quot;RSA&quot;);
  keyGen.initialize(2048);
  KeyPair keypair = keyGen.genKeyPair();
  byte[] pubKeyBytes = keypair.getPublic().getEncoded();
  </code></pre>
  <p>I need to implement my client in C#. I found the way how to do the same in C#:</p>
  <pre><code>var rsa = new RSACryptoServiceProvider(2048);
  var parameters = _rsa.ExportParameters(includePrivateParameters: false);
  </code></pre>
  <p>The client uses parameters and solution from <a href="https://stackoverflow.com/questions/49282558/how-to-adapt-public-private-rsa-keys-of-c-sharp-for-using-them-as-in-java">How to adapt public/private RSA keys of C# for using them as in Java?</a> That's ok. The client can generate the key which can pass verification on the server.
  As result, I have the encrypted response and trying to decrypt it.</p>
  <pre><code>responseContent = rsa.Decrypt(responseContent)
  </code></pre>
  <p>But this code throws following exception:</p>
  <blockquote>
  <p><strong>System.Security.Cryptography.CryptographicException:</strong> 'The data to be
  decrypted exceeds the maximum for this modulus of 256 bytes.'</p>
  </blockquote>
  <p><em>responseContent</em> is byte array with length 250996. And as I see there is impossible to decrypt response content by way above.</p>
  <p>From the documentation, I know that</p>
  <p><a href="https://i.stack.imgur.com/4VPBz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4VPBz.png" alt="enter image description here" /></a></p>
  <p>Also, I have an example how to decrypt response in java:</p>
  <pre><code>JWEObject jweObject = JWEObject.parse(encryptedPayload); 
  RSAPrivateKey rsaPrivatteKey = (RSAPrivateKey)KeyFactory
      .getInstance(&quot;RSA&quot;)
      .generatePrivate(new PKCS8EncodedKeySpec(keybytes));
  RSADecrypter RSADecrypter rsaDecrypter= new RSADecrypter(rsaPrivateKey);
  JWEObject jweObject.decrypt(rsaDecrypter); 
  String decryptedResponse = jweObject.getPayload().toString();
  </code></pre>
  <p>I thought that rsa.Decrypt is analog of code above. But as I sow - not.
  After some research, I found that my response is JWE source.
  Based on <a href="https://www.rfc-editor.org/rfc/rfc7516" rel="nofollow noreferrer">https://www.rfc-editor.org/rfc/rfc7516</a> I split my response to parts which was separated by '.' and decode each of them from base64url. As result I have:</p>
  <ul>
  <li>Header (which is JSON: {&quot;enc&quot;:&quot;A256CBC-HS512&quot;, &quot;alg&quot;:&quot;RSA1_5&quot;})</li>
  <li>Encryption key (size 256 bytes)</li>
  <li>Initialization Vector (size 16 bytes)</li>
  <li>Cipertext (size 1844688 bytes)</li>
  <li>Authentication Tag (size 32 bytes)</li>
  </ul>
  <p>I think the main content is in <em>Cipertext</em> and I need to decrypt it. But I don't know how because of the <em>Cipertext</em> size is more than 256 bytes and I can't use rsa.Decrypt.</p>
  <p>How to decrypt source when size of it is more than RSA key?</p>