How to use .key and .crt file in java that generated by openssl?

前端 未结 4 453
花落未央
花落未央 2020-12-01 08:29

I need asymmetric encryption in java. I generate .key and .crt files with own password and .crt file by openssl that said in http://www.imacat.idv.tw/tech/sslcerts.html .

4条回答
  •  伪装坚强ぢ
    2020-12-01 08:58

    This should do what you want to do (using the BouncyCastle PEMReader as suggested above) -- take a PEM-encoded private key + certificate, and output a PKCS#12 file. Uses the same password for the PKCS12 that was used to protect the private key.

    public static byte[] pemToPKCS12(final String keyFile, final String cerFile, final String password) throws Exception {
        // Get the private key
        FileReader reader = new FileReader(keyFile);
    
        PEMReader pem = new PEMReader(reader, new PasswordFinder() {
            @Override public char[] getPassword() {
                return password.toCharArray();
            }
        });
    
        PrivateKey key = ((KeyPair)pem.readObject()).getPrivate();
    
        pem.close();
        reader.close();
    
        // Get the certificate      
        reader = new FileReader(cerFile);
        pem = new PEMReader(reader);
    
        X509Certificate cert = (X509Certificate)pem.readObject();
    
        pem.close();
        reader.close();
    
        // Put them into a PKCS12 keystore and write it to a byte[]
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        KeyStore ks = KeyStore.getInstance("PKCS12");
        ks.load(null);
        ks.setKeyEntry("alias", (Key)key, password.toCharArray(), new java.security.cert.Certificate[]{cert});
        ks.store(bos, password.toCharArray());
        bos.close();
        return bos.toByteArray();
    }
    

提交回复
热议问题