简体   繁体   English

无法从证书中找到私钥

[英]Can't find private key from certificate

We are working on encryption-decryption using applet. 我们正在使用applet进行加密解密。 We find some unexpected issue with digital certificate. 我们发现数字证书存在一些意外问题。 One system has certificate and we can't find the private key from that certificate but by installing the same certificate again works fine. 一个系统有证书,我们找不到该证书的私钥,但再次安装相同的证书工作正常。

Java Plug-in 10.25.2.17
Using JRE version 1.7.0_25-b17 Java HotSpot(TM) 64-Bit Server VM
User home directory = C:\Users\admin

To access private key we are using below code. 要访问私钥,我们使用下面的代码。

private PrivateKey getPrivateKeyFromKeyStore(String pubkey, KeyStore browser) {
        PrivateKey privateKey = null;
        String pubKey1 = "";
        if (browser != null) {
            try {
                Field spiField = KeyStore.class.getDeclaredField("keyStoreSpi");
                spiField.setAccessible(true);
                KeyStoreSpi spi = (KeyStoreSpi) spiField.get(browser);
                Field entriesField = spi.getClass().getSuperclass().getDeclaredField("entries");
                entriesField.setAccessible(true);
                @SuppressWarnings("rawtypes")
                Collection entries = (Collection) entriesField.get(spi);
                for (Object entry : entries) {
                    String alias = (String) invokeGetter(entry, "getAlias");
                    X509Certificate[] certificateChain = (X509Certificate[]) invokeGetter(entry, "getCertificateChain");
                    for (X509Certificate current : certificateChain) {
                        pubKey1 = this.bASE64Encoder.encode(current.getPublicKey().getEncoded());
                        if (pubkey.equals(pubKey1) && !pubkey.equals("")) {
                            privateKey = (PrivateKey) invokeGetter(entry, "getPrivateKey");
                            return privateKey;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return privateKey;
    }

You won't find private key in certificate because it must be in your keystore, of course, if you generated your cert with its CSR :) 您将无法在证书中找到私钥,因为它必须位于您的密钥库中,当然,如果您使用CSR生成证书:)

As a tip, I may ask is the cert expired for example? 作为提示,我可能会问,例如证书过期了吗?

Anyway, the question is pretty unclear :( If you have cert you must have the keystore which was used to sign your app... It would be better you give more details... 无论如何,问题还不清楚:(如果你有证书,你必须拥有用于签署你的应用程序的密钥库...你会更好地提供更多细节......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM