Instructions to extract the certificate out of a PFX file using OpenSSL


This command extracts the certificate(s) from the PFX file, without the private key. You will be prompted for the PFX import password

    openssl pkcs12 -in yourfile.pfx -nokeys -out certificate.pem

Replace yourfile.pfx with the actual name of your PFX file and certificate.pem with the desired output filename for the certificate.


Optional:
Extract the Public Key from the Certificate: 

This command takes the certificate (in PEM format) and extracts the public key from it.

    openssl x509 -in certificate.pem -pubkey -noout > publickey.pem

This will output the public key in PEM format to the file publickey.pem.    
    

    
  --------------------------------------------
  
  Another way of doing it using keytool:
  
  To extract a public key from a PFX file and save it as a Java Keystore (JKS), you can use the keytool utility provided with the Java Development Kit (JDK). This process typically involves converting the PFX keystore to a JKS keystore. Convert PFX to JKS.
Use the keytool -importkeystore command to convert your PFX file into a JKS keystore. You will need to provide the source PFX file, its password, and specify the destination JKS file and its password.

    keytool -importkeystore -srckeystore yourpfxfile.pfx -srcstoretype pkcs12 -destkeystore yourjkskeystore.jks -deststoretype JKS
    
     Replace yourpfxfile.pfx with the actual name of your PFX file.
    Replace yourjkskeystore.jks with the desired name for your new JKS file.
    You will be prompted to enter the source keystore password (for the PFX file) and then to create a new password for the destination keystore (the JKS file). 

    Extract the Public Key (Certificate) from JKS: 

Once you have the JKS file, you can extract the public key (which is part of the certificate) using the keytool -exportcert command.

    keytool -exportcert -alias your_alias -keystore yourjkskeystore.jks -file publickey.cer
    
    
Replace your_alias with the alias of the entry containing the certificate in your JKS file. You can find the alias by listing the contents of the JKS using 
keytool -list -keystore yourjkskeystore.jks.

    Replace yourjkskeystore.jks with the name of your JKS file.
    Replace publickey.cer with the desired name for the file containing the exported public key certificate. You will be prompted for the JKS keystore password. 

This process will result in a publickey.cer file containing the public key certificate extracted from your original PFX file.
  