Welcome to the Official BlackBerry® Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Developer
ashishjmeshram
Posts: 154
Registered: 01-08-2010
My Carrier: Vodafone

How to read private key from PEM file?

Hi

 

I can read RSA publick key from PEM file but how can I read Private key also?

 

 

Here is my code to read Publick Key.

 

 

 

public static byte[] readFile(String fileName) {
	byte[] data = null;
	try {
		Class classs = Class
				.forName("CryptographyUtil");
		InputStream is = classs.getResourceAsStream(fileName);
		int len = is.available();
		data = new byte[len];
		is.read(data);
	} catch (Exception ex) {
		ex.printStackTrace();
	}
	return data;
}

public static RSAPublicKey readPublicKey() {
	String encodedMsg = new String(readFile("/PublicKey.pem"));
	RSAPublicKey publicKey = null;
	X509Certificate clientCert = null;
	
	byte[] certBuf = null;

	try {
		certBuf = Base64InputStream.decode(encodedMsg);
	} catch (Exception ex) {
		ex.printStackTrace();
	}
	try {
		clientCert = new X509Certificate(certBuf);
		publicKey = (RSAPublicKey) clientCert.getPublicKey();
	} catch (CertificateParsingException ex) {
		ex.printStackTrace();
	} catch (InvalidCryptoSystemException ex) {
		ex.printStackTrace();
	}
	return publicKey;
}

 

First I read the File using readFile method(). I get all file content in byte[]. Which then i use to get the publick key.

 

How do I get the private key?

 

Please help.

 

 

 

Please use plain text.
Developer
ashishjmeshram
Posts: 154
Registered: 01-08-2010
My Carrier: Vodafone

Re: How to read private key from PEM file?

Actually, if you see this line

 

 

clientCert = new X509Certificate(certBuf);
publicKey = (RSAPublicKey) clientCert.getPublicKey();

 

X509Certificate has method to get the publick key but not the private key.

 

Is there any way to get the private key also?

 

Thanks.

 

Please use plain text.