'Generate or create p12 file from Android

I have a problem when generating a .pfx certificate that I get from an api. as can be seen in the image.

{
 "type": "success",
 "code": 0,
 "message": "Certificado descargado exitosamente",
 "detalle_mensaje": "55g/bNoVn........fHlukJDHhj4=",
 "pass": "oz7FkVw1zrHC/Nt+2NQR3arg4Keo409MRbKC6MM3GoE=",
 "excepcion": null,
 "extension": ".pfx"
}

This is an encrypted code, where the original result is a private key:

MIIM/AIBAzCCDMIGCSqGSIb3DQEHAaCCDLMEggyvMIIMqzCCBzcGCSqGSIb3DQEHBqCCBygwggckAgEAMIIHHQYJKoZI....

So I need to generate this .pfx or .p12 file on the device storage.

I have this code to generate p12 but I don't know where to use the key (MIIM/AIBAzCCD ...)

It is worth mentioning that with this code it generates the .p12 file but when I open the certificate, I copy the password and it cannot open

        String storeName =  "ruta_del_dispositivo.p12";
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(1024);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();
        Certificate trustCert =  createCertificate("CN=CA", "CN=CA", publicKey, privateKey);
        Certificate[] outChain = { createCertificate("CN=Client", "CN=CA", publicKey, privateKey), trustCert };

        KeyStore ks = KeyStore.getInstance("pkcs12");
        ks.load(null);

        // GUARDAR archivo p12
        OutputStream outputStream = new FileOutputStream(storeName);
        ks.store(outputStream, password.toCharArray());
        outputStream.flush();
        outputStream.close();

--

 private static java.security.cert.X509Certificate createCertificate(String dn, String issuer, PublicKey publicKey, PrivateKey privateKey) throws Exception {
    X509V3CertificateGenerator certGenerator = new X509V3CertificateGenerator();
    certGenerator.setSerialNumber(BigInteger.valueOf(Math.abs(new Random().nextLong())));
    certGenerator.setSubjectDN(new X509Name(dn));
    certGenerator.setIssuerDN(new X509Name(issuer)); // Set issuer!
    certGenerator.setNotBefore(Calendar.getInstance().getTime());
    certGenerator.setNotAfter(Calendar.getInstance().getTime());
    certGenerator.setPublicKey(publicKey);
    certGenerator.setSignatureAlgorithm("SHA1withRSA");
    X509Certificate certificate = certGenerator.generate(privateKey, "BC");
    return certificate;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source