title: How to decrypt using rsa from PEM file
body: <p>I am using the following c# code to encrypt and decrypt using rsa with PEM files:</p>
  
  <pre><code>public string encrypt(string elementToEncrypt, string pathPrivateKey) {
          string pem = System.IO.File.ReadAllText(pathPrivateKey);
          byte[] Buffer = getBytesFromPEMFile(pem, "PUBLIC KEY");
          System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider();
          System.Security.Cryptography.RSAParameters rsaParam = rsa.ExportParameters(false);
          rsaParam.Modulus = Buffer;
          rsa.ImportParameters(rsaParam);
          byte[] encryptedMessageByte = rsa.Encrypt(Convert.FromBase64String(elementToEncrypt),false);
          return Convert.ToBase64String(encryptedMessageByte);
      }
  
  public string decrypt(string elementToDesencrypt, string pathPublicKey)
      {
          string pem = System.IO.File.ReadAllText(pathPublicKey);
          byte[] Buffer = getBytesFromPEMFile(pem, "RSA PRIVATE KEY");
          System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider();
          System.Security.Cryptography.RSAParameters rsaParam = rsa.ExportParameters(false);
          rsaParam.Modulus = Buffer;
          rsa.ImportParameters(rsaParam);
          byte[] encryptedMessageByte = rsa.Decrypt(Convert.FromBase64String(elementToDesencrypt), false);
          return Convert.ToBase64String(encryptedMessageByte);
      }
  
  public byte[] getBytesFromPEMFile(string pemString, string headerPEM) {
          string header = String.Format("-----BEGIN {0}-----", headerPEM);
          string footer = String.Format("-----END {0}-----", headerPEM);
          int start = pemString.IndexOf(header, StringComparison.Ordinal) + header.Length;
          int end = pemString.IndexOf(footer, start, StringComparison.Ordinal) - start;
          if (start &lt; 0 || end &lt; 0)
          {
              return null;
          }
          return Convert.FromBase64String(pemString.Substring(start, end));
      }
  </code></pre>
  
  <p>But the problem is when i want to decrypt in the line:</p>
  
  <pre><code>byte[] encryptedMessageByte = rsa.Decrypt(Convert.FromBase64String(elementToDesencrypt), false);
  </code></pre>
  
  <p>and the error that i am getting is that the key does not exist.
  <a href="https://i.stack.imgur.com/BWHqw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BWHqw.png" alt="enter image description here"></a></p>
  
  <p>My pem files are:</p>
  
  <p>key.pem</p>
  
  <pre><code>-----BEGIN RSA PRIVATE KEY-----
  MIICXAIBAAKBgQDLPKI8p+ANRabCTdLvJjuT0wx1kt2voJ0+BtdTRBqhJQbRgM2P
  dtHilmaVSyiVtD5l1mTl+h8mFRBttiH0VgW3KuyvFk2mrjF78MrsXlYoHVizGgeh
  UWVUsNh7EhdgF/hM7miZMXsHoa/MEQwgytPwjpDbOXXECZz8CpHiyNOftwIDAQAB
  AoGAUrmXgAEFHeHgAu8SkO2LCpy5UZI6UiaaWokGVIpAHJ+pqtU21tKSlByMHPC+
  0FDRpTojT8kDrMieK0obgA0TvcUaARVPGZsLjB4WZLKh7e8LPaUTvAS9dTmKd7xB
  4YGFKY+AJb38VdDU9CoQMsiPtIIiPWz09lgGvYRGzXmTBwECQQDsEtLRyOijXISK
  iFhtdpBI4yAmnTYyYLrsPXgS7asa80h7vnTmOlUpuqsxZtWNVGcpNiYG4y8OpJU5
  Jr8IkNnXAkEA3GRC63+SEbEo5wXcrHF+tzxfFmk3yzS38w5jtGik3yrp6psyjaQ8
  Q+D3RaKjGYtjTH3pmljRH2OGEvrNwvFtIQJAFkLgJnAvn9gFl5qr3AamLHleesWw
  aqe8eLKDNCW9UNlIKIMZOuydQ0YbBpmP4bfn0ncMtvGNanASskT5FrGyGQJASE7k
  3dsnE4LqhpGXy0QZbQjzsain05XiXG52K/TBUy8DPCPbPDmMREEFH+WyWWkwFSKi
  iC9nvUKr9IIxDCqlwQJBAIDwEg6yVGdVCQry+OEGtsiaGPveX+lAx/kULba0wfRq
  KaQAstQrT7p+ONtC8x8NHDE/ayjz6GlEZ7svR/LZO7w=
  -----END RSA PRIVATE KEY-----
  </code></pre>
  
  <p>and pubkey.pem</p>
  
  <pre><code>-----BEGIN PUBLIC KEY-----
  MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDLPKI8p+ANRabCTdLvJjuT0wx1
  kt2voJ0+BtdTRBqhJQbRgM2PdtHilmaVSyiVtD5l1mTl+h8mFRBttiH0VgW3Kuyv
  Fk2mrjF78MrsXlYoHVizGgehUWVUsNh7EhdgF/hM7miZMXsHoa/MEQwgytPwjpDb
  OXXECZz8CpHiyNOftwIDAQAB
  -----END PUBLIC KEY-----
  </code></pre>
  
  <p>I have read that if I want to decrypt, I should have both keys into one pem file, but if I do it, I would not know what to put on </p>
  
  <pre><code>rsaParam.Modulus = Buffer;
  </code></pre>
  
  <p>I mean, do I have to mix both buffers (private and public) into one? if so, how should I do that?.</p>
  
  <p>Thanks for the help.</p>