title: C# RSA Decryption using Bouncy Castle
body: <p>I have been given a Base64 Encoded encrypted string, which was encrypted in Java using Bouncy Castle. Example Java snippet below:</p>
  
  <pre><code>Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
  cipher.init(Cipher.ENCRYPT_MODE, key.getPublic());
  byte[] encryptedText = cipher.doFinal("xxxxx|xxxxx".getBytes("UTF-8"));
  String encodedText = new BASE64Encoder().encode(encryptedText);
  </code></pre>
  
  <p>I need to decrypt the resulting string using Bouncy Castle, but in C#
  I have been given a code snippet on how to do this in Java, but I can't convert this for C# (reasons is we are building a .net site, and is going to be an iFrame within a Java site. The Java site is going to passing in the RSA Encrypted string to the .NET site). Example Java code to decrypt below:</p>
  
  <pre><code>Cipher cipherDec = Cipher.getInstance("RSA/ECB/PKCS1Padding");
  cipherDec.init(Cipher.DECRYPT_MODE, key.getPrivate());
  byte[] decodedText = new BASE64Decoder().decodeBuffer(encodedText);
  byte[] decryptedText = cipherDec.doFinal(decodedText);
  String finalValue = new String(decryptedText, "UTF-8");
  </code></pre>
  
  <p>I have downloaded the examples from <a href="http://www.bouncycastle.org/csharp/" rel="nofollow">http://www.bouncycastle.org/csharp/</a> but there doesn't seem to be an example of inputting a string value to get encrypted, and it then going though the encrypt/decrypt process. </p>
  
  <p>I have been given values for <strong>modulus</strong>, <strong>public exponent</strong>, <strong>private expontent</strong>, <strong>prime P</strong>, <strong>prime q</strong>, <strong>prime exponent p</strong>, <strong>prime exponent q</strong> and <strong>crt coefficient</strong>.</p>
  
  <p>I have seen that I can use the following:</p>
  
  <pre><code>IAsymmetricBlockCipher signer = new Pkcs1Encoding(new RsaEngine());
  signer.Init(true, pubParameters);
  </code></pre>
  
  <p>But the <code>signer</code> object doesn't seem to have the same methods as the Java examples above.</p>
  
  <p>Only method I can use is</p>
  
  <pre><code>ProcessBlock(byte[] inbuf, int inOff, int inLen);
  </code></pre>
  
  <p>But I can't see how to use this in my context.</p>
  
  <p>Any help here would be most appreciated.</p>