BAEL-4219 - How to read .pem file to get private and public key (#9676)
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package com.baeldung.pem;
|
||||
|
||||
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
|
||||
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
|
||||
import org.bouncycastle.openssl.PEMParser;
|
||||
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
|
||||
import org.bouncycastle.util.io.pem.PemObject;
|
||||
import org.bouncycastle.util.io.pem.PemReader;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.interfaces.RSAPrivateKey;
|
||||
import java.security.interfaces.RSAPublicKey;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
|
||||
public class BouncyCastlePemUtils {
|
||||
|
||||
public static RSAPublicKey readX509PublicKey(File file) throws InvalidKeySpecException, IOException, NoSuchAlgorithmException {
|
||||
KeyFactory factory = KeyFactory.getInstance("RSA");
|
||||
|
||||
try (FileReader keyReader = new FileReader(file);
|
||||
PemReader pemReader = new PemReader(keyReader)) {
|
||||
|
||||
PemObject pemObject = pemReader.readPemObject();
|
||||
byte[] content = pemObject.getContent();
|
||||
X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(content);
|
||||
return (RSAPublicKey) factory.generatePublic(pubKeySpec);
|
||||
}
|
||||
}
|
||||
|
||||
public static RSAPublicKey readX509PublicKeySecondApproach(File file) throws IOException {
|
||||
try (FileReader keyReader = new FileReader(file)) {
|
||||
|
||||
PEMParser pemParser = new PEMParser(keyReader);
|
||||
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
|
||||
SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfo.getInstance(pemParser.readObject());
|
||||
|
||||
return (RSAPublicKey) converter.getPublicKey(publicKeyInfo);
|
||||
}
|
||||
}
|
||||
|
||||
public static RSAPrivateKey readPKCS8PrivateKey(File file) throws InvalidKeySpecException, IOException, NoSuchAlgorithmException {
|
||||
KeyFactory factory = KeyFactory.getInstance("RSA");
|
||||
|
||||
try (FileReader keyReader = new FileReader(file);
|
||||
PemReader pemReader = new PemReader(keyReader)) {
|
||||
|
||||
PemObject pemObject = pemReader.readPemObject();
|
||||
byte[] content = pemObject.getContent();
|
||||
PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(content);
|
||||
return (RSAPrivateKey) factory.generatePrivate(privKeySpec);
|
||||
}
|
||||
}
|
||||
|
||||
public static RSAPrivateKey readPKCS8PrivateKeySecondApproach(File file) throws IOException {
|
||||
try (FileReader keyReader = new FileReader(file)) {
|
||||
|
||||
PEMParser pemParser = new PEMParser(keyReader);
|
||||
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
|
||||
PrivateKeyInfo privateKeyInfo = PrivateKeyInfo.getInstance(pemParser.readObject());
|
||||
|
||||
return (RSAPrivateKey) converter.getPrivateKey(privateKeyInfo);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user