0

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?

1 Answers1

0

I think it's too late reply.

  1. You are not casting the right object.

  2. check the supported crypto mechanism for MSCAPI(Windows-MY) Store

  3. Some providers will not allow you to take the Private key out from their repository. So check the Microsoft documentation about MSCAPI(Windows-MY) Store.

Virudhai Paul
  • 81
  • 1
  • 9