0

我正在尝试使用充气城堡(v1.65)在java中进行加密/解密。所以我需要将我的 MSCAPI(Windows-MY) 密钥转换为 PGP 密钥。当我尝试使用 PSCK12 或 PSCK8 时,它就像一个魅力。但是对于 MSCAPI(Windows-MY) Store,它不起作用。

这是我的代码:

        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());
    }

使用 MSCAPI(Windows-MY) 密钥,我遇到了这个异常:

sun.security.mscapi.CPrivateKey cannot be cast to java.security.interfaces.RSAPrivateCrtKey

有人可以帮我吗?

4

1 回答 1

0

我觉得回复太晚了。

  1. 您没有投射正确的对象。

  2. 检查 MSCAPI(Windows-MY) 商店支持的加密机制

  3. 一些提供商不允许您从他们的存储库中取出私钥。因此,请查看有关 MSCAPI(Windows-MY) Store 的 Microsoft 文档。

于 2021-03-06T10:46:40.450 回答