title: Loading public key from file in C#
body: <p>I am having difficulty loading my private key.
  I created a certificate using the following command:</p>
  
  <pre><code>openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout privateKey.key -out certificate.crt
  </code></pre>
  
  <p>I created a GIST of the output: <a href="https://gist.github.com/anonymous/5592135" rel="nofollow">https://gist.github.com/anonymous/5592135</a> </p>
  
  <pre><code>    static void Main(string[] args)
      {
          string location = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
          string certLocation = location + "\\assets\\certificate.crt";
          string privateKeyLocation = location + "\\assets\\privateKey.key";
          string xmlDocLocation = location + "\\assets\\sample.xml";
  
          XmlDocument Doc = new XmlDocument();
          Doc.Load(xmlDocLocation);
  
          // read up the certificate
          X509Certificate2 cert = new X509Certificate2(certLocation);
          X509Certificate2 privateKey = new X509Certificate2(privateKeyLocation);
  
      }
  </code></pre>
  
  <p>My certificate loads fine but I cannot get the private key to load. I get "Cannot find the requested object."</p>
  
  <p>I would prefer to load the private key and cert from files instead of using the store, is this possible? Did I generate the cert and private key incorrectly? Ultimately I would like to include the public key in an xml document and the receiver would parse the xml data and validate the private and public keys match up.</p>