简体   繁体   中英

Can't find private key from certificate

We are working on encryption-decryption using 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 :)

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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