Use Java to read the private key from the certificate

Source: Internet
Author: User

Import java. Io. fileinputstream;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Security. invalidkeyexception;
Import java. Security. keystore;
Import java. Security. keystoreexception;
Import java. Security. nosuchalgorithmexception;
Import java. Security. nosuchproviderexception;
Import java. Security. privatekey;
Import java. Security. signatureexception;
Import java. Security. unrecoverablekeyexception;
Import java. Security. cert. Certificate;
Import java. Security. cert. certificateexception;
Import java. Security. cert. certificatefactory;

Public class keyreader {

Public static void main (string [] ARGs) throws keystoreexception,
Nosuchproviderexception, nosuchalgorithmexception,
Certificateexception, ioexception, unrecoverablekeyexception,
Invalidkeyexception, signatureexception {

// Open an input stream on the keystore File
String cerfilename = "D:/certa. Cer ";
String p12filename = "D:/certa. p12 ";
String pfxpassword = "OpenSSL ";

Inputstream FCM = new fileinputstream (p12filename );

// Create a keystore object
Keystore = keystore. getinstance ("PKCS12", "BC ");
// Load the file into the keystore
Keystore. Load (FCM, pfxpassword. tochararray ());

String aliaesname = "ABCD ";
Privatekey prikey = (privatekey) (keystore. getkey (aliaesname, null ));
System. Out. println ("Private Key:/N" + prikey );

// Public Key
Inputstream is = new fileinputstream (cerfilename );
Certificatefactory cf = certificatefactory. getinstance ("X509 ");
Certificate cercert = Cf. generatecertificate (is );
System. Out. println ("Public Key:/N" + CERT );
}
}

 

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.