Extract the private key, certificate __java from the Java keystore file

Source: Internet
Author: User

KeyStore is a password-protected file that holds the private key and the certificate. Can be generated from the Keytool tool with the JDK.

But the Keytool tool does not provide a convenient way to get the private keys and certificates from KeyStore files everywhere.

So it is possible to encode the relevant work through the Java.security.KeyStore class provided by the JDK.

[Java]  View Plain  copy import java.io.file;      import  java.io.fileinputstream;      import java.io.filewriter;       import java.security.key;      import java.security.keypair;       import java.security.keystore;      import  java.security.keystoreexception;      import  java.security.nosuchalgorithmexception;      import java.security.privatekey;      import java.security.publickey;     import  java.security.unrecoverablekeyexception;     import java.security.cert.certificate;             import sun.misc.base64encoder;       public class exportprivatekey {             private File keystoreFile;             private String keyStoreType;             private char[] password;             private String alias;              private File exportedFile;                        public  Static keypair getprivatekey (keystore keystore, string alias, char[]  Password)  {                      try {                 &nBsp;            key key=keystore.getkey (alias , password);                              if (Key instanceof privatekey )  {                                       certificate cert=keystore.getcertificate (alias);                                       publickey publickey=cert.getpublickey ();                                       return new  KeyPair (PublicKey, (Privatekey) key);                              }                      }  catch  (unrecoverablekeyexception e)  {              } catch  (nosuchalgorithmexception e)  {             } catch  (keystoreexception e)  {              }              return null;              }   &NBsp;                 public void  Export ()  throws Exception{                      keystore keystore=keystore.getinstance (KeyStoreType);                       base64encoder encoder=new base64encoder ();                      keystore.load (new  FileInputStream (keystorefile), password);                      keypair keypair=getprivatekey (Keystore,alias, Password);                      privatekey&nbsP;privatekey=keypair.getprivate ();                      string encoded=encoder.encode (privateKey.getEncoded ());                       filewriter fw=new filewriter (Exportedfile);    

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.