I need asymmetric encryption in java. I generate .key and .crt files with own password and .crt file by openssl that said in http://www.imacat.idv.tw/tech/sslcerts.html .
How to use these .key and .crt file to extract publickey and private key in Java?
I need asymmetric encryption in java. I generate .key and .crt files with own
Share
Your
.keyand.crtfiles may be in PEM format. To check this open them with a text editor and check whether the content looks like------BEGIN CERTIFICATE------(or “begin RSA private key”…). This is generally the default format used by OpenSSL, unless you’ve explicitly specified DER.It’s probably not required (see below), but if your certificate is in DER format (a binary format), you can convert them in PEM format using:
(Check the help for
openssl rsafor doing something similar with the private key if needed.)You then get two options:
Build a PKCS#12 file
You can then use it directly from Java as a keystore of type “PKCS12”. Most Java applications should allow you to specify a keystore type in addition to the file location. For the default system properties, this is done with
javax.net.ssl.keyStoreType(but the application you’re using might not be using this). Otherwise, if you want to load it explicitly, use something like this:(Then, you should be able to iterate through the
aliases()of theKeyStoreand usegetCertificate(and thengetPublicKey()for the public key) andgetKey().Use BouncyCastle‘s
PEMReader.For the private key, you’ll need to implement a
PasswordFinder(see link from PEMReader doc) for constructing thePEMReaderif the private key is password-protected. (You’ll need to cast the result ofreadObject()into aKeyorPrivateKey.)