【问题标题】:PGP, verify signature on a certificatePGP,验证证书上的签名
【发布时间】:2015-01-10 16:35:47
【问题描述】:

我正在开发一个简单的 Java 代码,它使用 BouncyCastle v1.51 打开 PGP 公钥并验证其中包含的签名。 目前,我能够加载公钥并遍历所有签名。但是,验证总是返回“false”,即使我使用与生成签名的私钥对应的公钥测试签名。

这是我的代码:

    try {
        PGPPublicKey pkey = PGPEncryptionUtils.readPublicKey(new FileInputStream(new File(HOME_DIR + "to_verify")));
        Iterator it = pkey.getSignatures();

        PGPPublicKey signing_key = PGPEncryptionUtils.readPublicKey(
                new FileInputStream(new File(HOME_DIR + "my_public_key")));

        while (it.hasNext()) {
            PGPSignature sig = (PGPSignature) it.next();
            sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), signing_key);
            // Here I'd expect to see at least a "true".
            println(sig.verify());
        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (PGPException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

readPublicKey 的代码取自这里:https://github.com/damico/OpenPgp-BounceCastle-Example/blob/master/src/org/jdamico/bc/openpgp/utils/PgpHelper.java

我做错了什么? 谢谢!

【问题讨论】:

标签: java cryptography digital-signature bouncycastle pgp


【解决方案1】:

我没有使用 PGPSignatures 的经验,但是要验证公钥加密中的签名,您需要三件事:

  1. 签名。
  2. 公钥。
  3. 应该签名的原始消息。

在您的示例中缺少original message,您需要提供通过PGPSignature.update(byte[]) 方法签名的original message,因此您的代码必须类似于:

while (it.hasNext()) {
        PGPSignature sig = (PGPSignature) it.next();
        sig.init(new >JcaPGPContentVerifierBuilderProvider().setProvider("BC"), signing_key);

       // here you need the original message
        sig.update("signature original message".getBytes());

        // now you can try to verify!
        println(sig.verify());
}

希望对你有帮助,

【讨论】:

    猜你喜欢
    • 2017-06-29
    • 2011-12-03
    • 1970-01-01
    • 2012-09-09
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    相关资源
    最近更新 更多