I am trying to do an encryption/decryption in java with bouncy castle (v1.65). So I need to convert my MSCAPI(Windows-MY) Key to a PGP Key. When I try with a PSCK12 or PSCK8, it worwed like a charm. But with MSCAPI(Windows-MY) Store, it didn't work.
Here is my code :
Security.addProvider(new BouncyCastleProvider());
JcaPGPKeyConverter converter = new JcaPGPKeyConverter().setProvider(TypeProvider.MSCAPI.getType());
PGPPublicKey pgpPublicKey = this.getPgpPublicKey(keyPair.getCertificate());
PGPPrivateKey pgpPrivateKey = converter.getPGPPrivateKey(pgpPublicKey, (PrivateKey)keyPair.getPrivateKey());
PGPKeyPair pgpKeyPair = new PGPKeyPair(pgpPublicKey, pgpPrivateKey);
ByteArrayOutputStream secretKeyOutput = new PGPCryptographieServiceImpl().pgpPrivateKeyFromKeyPair(pgpKeyPair);
File pgpKeyFile = new File(initialDirectory, nom.replace("*", "")
.replace("/", "").replace("<", "").replace(">", "").replace(":", "")
.replace("|", "").replace("\\", "").replace("?", "") + ".secret.asc");
FileUtils.writeByteArrayToFile(pgpKeyFile, secretKeyOutput.toByteArray());
}
With MSCAPI(Windows-MY) key, I had this exception:
sun.security.mscapi.CPrivateKey cannot be cast to java.security.interfaces.RSAPrivateCrtKey
Could anyone help me please?