title: How to create a truststore.jks with one certificate?
body: <p>(I'm a newby in cryptographic things.)
  I have an setup program written in C#. This asks the user to input the server URL. Then it connects to this server and stores this server certificate into a truststore file that is used by the installed Java REST service.  </p>
  
  <p>The truststore file is created by <em>keytool.exe</em>:</p>
  
  <pre><code>keytool.exe -alias anAlias -import -file cert.cer -noprompt -keystore truststore.jks -storepass aPassword
  </code></pre>
  
  <p>Now we don't want to use <em>keytool.exe</em>. We want to create the keystore by C#. My first tries are as follows:</p>
  
  <pre><code>class AddCertToTruststore
    {
      public static void Do()
      {
        ServicePointManager.ServerCertificateValidationCallback += Validate;
  
        X509Certificate2 cert = new X509Certificate2("cert.cer");
        cert.Archived = true;
  
        bool ok = cert.Verify(); // always false
  
        X509Certificate2Collection certs = new X509Certificate2Collection();
        certs.Add(cert);
        byte[] bytes = certs.Export(X509ContentType.Pkcs12);
  
        File.WriteAllBytes("truststore.jks", bytes);
  
        ServicePointManager.ServerCertificateValidationCallback -= Validate;
      }
  
      private static bool Validate(object sender, X509Certificate certificate, X509Chain chain,
                                   SslPolicyErrors sslPolicyErrors)
      {
        return true;
      }
    }
  </code></pre>
  
  <p>This code creates a truststore but I'm missing the certificate in it. If I open the truststore.jks with <em>KeyStore Explorer 5.1</em> there is not any certificate in it. What am I doing wrong?  </p>
  
  <p>The certificate is a self-signed certificate. <code>cert.Verify()</code> returns always <code>false</code>.</p>