Extract the private key and certificate from the Java keystore File

Source: Internet
Author: User

Keystore is a password-protected file that stores the private key and certificate. It can be generated using the built-in JDK keytool.

However, the keytool does not provide a convenient method. Private keys and certificates are everywhere in the keystore file.

Therefore, you can useJava. Security. keystore
Class.

See:

Http://www.anandsekar.com/2006/01/19/exporting-the-private-key-from-a-jks-keystore/

 

<Br/> Import Java. io. file; <br/> Import Java. io. fileinputstream; <br/> Import Java. io. filewriter; <br/> Import Java. security. key; <br/> Import Java. security. keypair; <br/> Import Java. security. keystore; <br/> Import Java. security. keystoreexception; <br/> Import Java. security. nosuchalgorithmexception; <br/> Import Java. security. privatekey; <br/> Import Java. security. publickey; <br/> Import Java. security. unrecoverablekeyexception; <br/> Import Java. security. cert. certificate; </P> <p> import sun. misc. base64encoder; </P> <p> public class exportprivatekey {<br/> private file keystorefile; <br/> private string keystoretype; <br/> private char [] password; <br/> private string alias; <br/> private file exportedfile; </P> <p> Public static keypair getprivatekey (keystore, string alias, char [] password) {<br/> try {<br/> key = keystore. getkey (alias, password); <br/> If (Key instanceof privatekey) {<br/> certificate Cert = keystore. getcertificate (alias); <br/> publickey = cert. getpublickey (); <br/> return New keypair (publickey, (privatekey) Key); <br/>}< br/>} catch (unrecoverablekeyexception E) {<br/>} catch (nosuchalgorithmexception e) {<br/>} catch (keystoreexception e) {<br/>}< br/> return NULL; <br/>}</P> <p> Public void Export () throws exception {<br/> keystore = keystore. getinstance (keystoretype); <br/> base64encoder encoder = new base64encoder (); <br/> keystore. load (New fileinputstream (keystorefile), password); <br/> keypair = getprivatekey (keystore, alias, password); <br/> privatekey = keypair. getprivate (); <br/> string encoded = encoder. encode (privatekey. getencoded (); <br/> filewriter fw = new filewriter (exportedfile); <br/> FW. write ("-- begin private key --/N"); <br/> FW. write (encoded); <br/> FW. write ("/N"); <br/> FW. write ("-- end private key --"); <br/> FW. close (); </P> <p >}</P> <p> Public static void main (string ARGs []) throws exception {<br/> exportprivatekey export = new exportprivatekey (); <br/> export. keystorefile = new file (ARGs [0]); <br/> export. keystoretype = ARGs [1]; <br/> export. password = ARGs [2]. tochararray (); <br/> export. alias = ARGs [3]; <br/> export. exportedfile = new file (ARGs [4]); <br/> export. export (); <br/>}< br/>

 

 

 

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.