如何在Java和Bouncy Castle中生成PGP密钥吊销

时间:2018-06-20 15:41:24

标签: java bouncycastle pgp

我想同时生成吊销证书和公用和专用密钥对。

正确生成了私钥和公钥。

我试图这样做:

public void generateRevoke(String id, PGPPublicKey pk, PGPSecretKey secretKey, char[] passPhrase, OutputStream out) throws PGPException, IOException {

    PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(
            new JcaPGPContentSignerBuilder(secretKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1));

    PGPPrivateKey pgpPrivKey = secretKey.extractPrivateKey(
            new JcePBESecretKeyDecryptorBuilder().setProvider(new BouncyCastleProvider())
                    .build(passPhrase));

    signatureGenerator.init(PGPSignature.KEY_REVOCATION, pgpPrivKey);

    PGPSignature signature = signatureGenerator.generateCertification(id, pk);

    PGPPublicKey key = PGPPublicKey.addCertification(pk, id, signature);

    key.encode(new ArmoredOutputStream(out));
}

但是在输出文件中我得到的是PGP消息,而不是PGP公用密钥

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

我解决了问题。正确的方法返回带有撤消证书的公共密钥:

public void generateRevoke(String id, PGPSecretKey secretKey, char[] passPhrase, OutputStream out) throws PGPException, IOException {

    PGPPublicKey oldKey = secretKey.getPublicKey();

    PGPPrivateKey pgpPrivKey = secretKey.extractPrivateKey(
            new JcePBESecretKeyDecryptorBuilder().setProvider( provider )
                    .build(passPhrase));

    PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(
            new JcaPGPContentSignerBuilder( secretKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1 ) );

    signatureGenerator.init( PGPSignature.CERTIFICATION_REVOCATION, pgpPrivKey );

    PGPSignature signature = signatureGenerator.generateCertification(id, oldKey);

    PGPPublicKey newKey = PGPPublicKey.addCertification(oldKey, id, signature);

    out = new ArmoredOutputStream(out);

    newKey.encode(out);
    out.close();
}