如何以编程方式访问 windows 和 mac 可信证书存储

问题描述:

使用 keytool 实用程序创建自签名 Java 密钥库和证书文件.通过使用 mmc.exe 命令进入证书控制台,我可以将证书添加到 Windows 信任库中.

Create a selfsigned java keystore and certificate file using keytool utility. Am able to add the certificate into windows trust store by going to certificate console by using mmc.exe command.

但是无论如何以编程方式将证书添加到 Windows 信任存储中.MAC系统也需要同样的东西.

But is there anyway to add the certificate into windows trust store programmatically. And also required the same things for MAC system.

感谢您的任何建议.

以下是 Windows/MAC 在其信任存储中添加证书的代码片段.

Below is code snippet for Windows/MAC to add certificate in their trust store.

窗口:

    KeyStore root = KeyStore.getInstance("Windows-ROOT","SunMSCAPI");
    root.load(null,null);
    /* certificate must be DER-encoded */
    FileInputStream in = new FileInputStream("yourcertificate.cer");
    X509Certificate cacert = (X509Certificate)CertificateFactory.getInstance("X.509").generateCertificate(in);                      
    root.setCertificateEntry("certificatealiasname", cacert);

在 windows 中,它成功地将证书添加到信任存储中,但由于没有管理员权限,某些系统无法运行.所以在那些机器上,如果以管理员身份登录或给用户一些管理员权限,它就会工作.

In windows it is successfully adding the certificate in trust store, but some system does not work due to not having admin privileges. So in those machine it will work if logged in as Administrator or give the user some Admin privileges.

MAC:

        KeyStore root = KeyStore.getInstance("KeychainStore", "Apple");
        root.load(null);
        /* certificate must be DER-encoded */
        FileInputStream in = new FileInputStream("yourcertificate.cer");
        X509Certificate cacert = (X509Certificate)CertificateFactory.getInstance("X.509").generateCertificate(in);
        root.setCertificateEntry("certificatealiasname", cacert);
        root.store(null, null);

它能够成功地将证书添加到钥匙串中但不信任该证书.所以需要去KeyChain Access手动信任证书.

It is able to successfully add the certificate in keychain but not trusting the certificate. So need to go to KeyChain Access and manually trust the certificate.