I want to extract private key from .pfx
file. The key extracting from this code snippet is in format PKCS#1, but I want in format PKCS#8.
KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
char[] password = "password".toCharArray();
KeyStore keyStore = KeyStore.getInstance("PKCS12");
getPrivateKeyFromPFX(password);
InputStream certificateStream = getResources().openRawResource(R.raw.certificate);
keyStore.load(certificateStream, password);
kmf.init(keyStore,password);
Enumeration<String> aliases = keyStore.aliases();
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
if (keyStore.isKeyEntry(alias)) {
PrivateKey keyEntry = (PrivateKey) keyStore.getKey(alias, password);
byte[] bytes = keyEntry.getEncoded();
String somthing =(keyEntry.getFormat());
// Encode the private key to Base64
String base64PrivateKey = Base64.encodeToString(bytes, Base64.DEFAULT) ;
Log.d("PFXExtractor", "Private key extracted and encoded in Base64:n" + base64PrivateKey);
}
}
I tried using this snippet to cenvert but still not luck
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey privateKey1 = kf.generatePrivate(pkcs8EncodedKeySpec);
I extracting this key from android
and pass to flutter
code using methodChannel
where
RSAKeyParser().parse("-----BEGIN RSA PRIVATE KEY-----n$keyStrn-----END RSA PRIVATE KEY-----") as RSAPrivateKey;
this code use that key for decryption