02-10-2010 02:49 AM
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.
02-10-2010 03:46 AM
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.