title: Adding a certificate to Store then retrieve it
body: <p>Disclaimer: I am 2 days into reading about Certificates/RSA Algorithms and Encrypt/Decrypt.</p>
  <p>I am trying to do a small app that communicates with Windows Key Store ( Certificate Store ) and where I should be able to read certificates/add certificates.</p>
  <p>I have created a method to add a certificate.</p>
  <pre><code> public void AddKey()
      {
          CngKey cngKey;
  
          CngKeyCreationParameters cng = new CngKeyCreationParameters
          {
              KeyUsage = CngKeyUsages.AllUsages
          };
  
  
          if (!CngKey.Exists(KEY_NAME))
          {
              cngKey = CngKey.Create(CngAlgorithm.Rsa, KEY_NAME, cng);
          }
          else
          {
              cngKey = CngKey.Open(KEY_NAME);
          }
  
          RSACng rsaKey = new RSACng(cngKey)
          {
              KeySize = 2048
          };
  
          byte[] rsaPrvKeyExport = rsaKey.Key.Export(CngKeyBlobFormat.GenericPrivateBlob);
          byte[] rsaPubKeyExport = rsaKey.Key.Export(CngKeyBlobFormat.GenericPublicBlob);
  
          CngKey cngPrv = CngKey.Import(rsaPrvKeyExport, CngKeyBlobFormat.GenericPrivateBlob);
          CngKey cngPub = CngKey.Import(rsaPubKeyExport, CngKeyBlobFormat.GenericPublicBlob);
  
          //var signed = Sign512(Constants.STRING_TO_ENCODE.ToByteArray(), rsaPrvKeyExport);
  
          string exportPrivateKey = Convert.ToBase64String(rsaKey.ExportPkcs8PrivateKey());
  
          string pemString = $&quot;{Constants.RSA_KEY_HEADER}\n{exportPrivateKey}\n{Constants.RSA_KEY_FOOTER}&quot;;
  
          
  
          X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
          store.Open(OpenFlags.ReadWrite);
          var certificate = BuildSelfSignedServerCertificate(rsaKey);
          var thumbprint = certificate.Thumbprint;
  
          store.Certificates.Add(certificate);
          store.Close();
  </code></pre>
  <p>}</p>
  <p>I know not all lines in this code are needed, but I am in the learning process.</p>
  <p>So what I do here is create a cngKey
  Open the store.
  Create a Certificate from my cngKey
  Add the certificate to the store
  Close the store.</p>
  <p>I generate a certificate from a cngKey using this code</p>
  <pre><code> private X509Certificate2 BuildSelfSignedServerCertificate(RSA key)
          {           
  
              X500DistinguishedName distinguishedName = new X500DistinguishedName($&quot;CN={Constants.CERTIFICATE_NAME}&quot;);
  
  
              var request = new CertificateRequest(distinguishedName, key, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
  
              request.CertificateExtensions.Add(
                  new X509KeyUsageExtension(X509KeyUsageFlags.DataEncipherment | X509KeyUsageFlags.KeyEncipherment | X509KeyUsageFlags.DigitalSignature, false));
  
  
              request.CertificateExtensions.Add(
                 new X509EnhancedKeyUsageExtension(
                     new OidCollection { new Oid(&quot;1.3.6.1.5.5.7.3.1&quot;) }, false));
  
              //request.CertificateExtensions.Add(sanBuilder.Build());
  
              var certificate = request.CreateSelfSigned(new DateTimeOffset(DateTime.UtcNow.AddDays(-1)), new DateTimeOffset(DateTime.UtcNow.AddDays(3650)));
              certificate.FriendlyName = Constants.CERTIFICATE_NAME;
  
              return new X509Certificate2(certificate.Export(X509ContentType.Pfx, &quot;WeNeedASaf3rPassword&quot;), &quot;WeNeedASaf3rPassword&quot;, X509KeyStorageFlags.MachineKeySet);
  
          }
  </code></pre>
  <p>This gives no error, but when I open the certificate store I can't find it</p>
  <p><a href="https://i.stack.imgur.com/mOvBz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mOvBz.png" alt="enter image description here" /></a></p>
  <p>I also tried to retrive it programmaticaly</p>
  <p>using this bit of code</p>
  <pre><code>X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
  
              store.Open(OpenFlags.ReadOnly);
  
              var certificates = store.Certificates;
  </code></pre>
  <p>but my certificate is not here.</p>
  <p>What am I missing, I am sure I am doing something wrong, but being new to this, I have no idea what.</p>
  <p>Online I could not find a complete example of what I tried to achieve here.</p>