title: Public Key RSA Decryption (C#)
body: <p>My problem is the following:</p>
  
  <p>I am receiving an encrypted string with RSA private key. I have the public key to decrypt it and the next method (based on suggestions such as <a href="http://www.codeproject.com/Articles/10877/Public-Key-RSA-Encryption-in-C-NET" rel="nofollow">http://www.codeproject.com/Articles/10877/Public-Key-RSA-Encryption-in-C-NET</a>
  and msdn):</p>
  
  <pre><code>public static string DecryptString(string inputString, int dwKeySize, string xmlString){
      try
      {               
          RSACryptoServiceProvider rsaCryptoServiceProvider = new RSACryptoServiceProvider(dwKeySize);
          rsaCryptoServiceProvider.FromXmlString(xmlString);
  
          int base64BlockSize = ((dwKeySize / 8) % 3 != 0) ?
            (((dwKeySize / 8) / 3) * 4) + 4 : ((dwKeySize / 8) / 3) * 4;
          int iterations = inputString.Length / base64BlockSize;
          ArrayList arrayList = new ArrayList();
          for (int i = 0; i &lt; iterations; i++)
          {
              byte[] encryptedBytes = Convert.FromBase64String(inputString.Substring(base64BlockSize * i, base64BlockSize));
              Array.Reverse(encryptedBytes);                    
              arrayList.AddRange(rsaCryptoServiceProvider.Decrypt(encryptedBytes, false));
          }
          return Encoding.UTF32.GetString(arrayList.ToArray(Type.GetType("System.Byte")) as byte[]);
      }
      catch (Exception exp)
      {
          Console.WriteLine(exp.ToString());                
          throw exp;
      }
  }
  </code></pre>
  
  <p>When you run I use the following values:</p>
  
  <p>inputString = <code>g/h/UizCowOX/uQubCZ1 .... I1k2FaCMSOpkYcTOQ==</code> encryption string 344 chars.</p>
  
  <p>dwKeySize = <code>2048</code></p>
  
  <p>xmlString = <code>&lt;RSAKeyValue&gt;&lt;Modulus&gt;mxBSd9ss/OmV ... D8miCOWclUJdbWw==&lt;/Modulus&gt;&lt;Exponent&gt;AQAB&lt;/Exponent&gt;&lt;/RSAKeyValue&gt;</code> with the public key in xml format.</p>
  
  <p>Get the following error:</p>
  
  <pre class="lang-none prettyprint-override"><code>System.Security.Cryptography.CryptographicException: Key does not exist.
     at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
     at System.Security.Cryptography.RSACryptoServiceProvider.DecryptKey(SafeKeyHandle pKeyContext, Byte[] pbEncryptedKey, Int32 cbEncryptedKey,Boolean fOAEP, ObjectHandleOnStack ohRetDecryptedKey)
     at System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] rgb,Boolean fOAEP)
     at ConsoleApplication1.Program.DecryptString(String inputString, Int32 dwKeySize, String xmlString)
  </code></pre>
  
  <p>If I call rsaCryptoServiceProvider.Decrypt with the last parameter to TRUE the message is:</p>
  
  <pre class="lang-none prettyprint-override"><code>System.Security.Cryptography.CryptographicException: Error occurred while decoding OAEP padding.
     at System.Security.Cryptography.RSACryptoServiceProvider.DecryptKey(SafeKeyHandle pKeyContext, Byte[] pbEncryptedKey, Int32 cbEncryptedKey, Boolean fOAEP, ObjectHandleOnStack ohRetDecryptedKey)
     at System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] rgb,Boolean fOAEP)
     at ConsoleApplication1.Program.DecryptString(String inputString, Int32 dwKeySize, String xmlString)
  </code></pre>
  
  <p>I tried to load the public key replacing the first lines (instantiation provider) by:</p>
  
  <pre><code>X509Certificate2 Cert;
  Cert = new X509Certificate2(@"C:\Users\user1\Documents\seggar.der"); 
  RSACryptoServiceProvider rsaCryptoServiceProvider = new RSACryptoServiceProvider(2048);
  rsaCryptoServiceProvider = (RSACryptoServiceProvider)Cert.PublicKey.Key;
  </code></pre>
  
  <p>But the same errors occur.</p>
  
  <p>Any suggestions on how to resolve the issue?</p>