简体   繁体   English

PGP 签名格式阅读器

[英]PGP signature format reader

In my project I need to verify PGP clear signed signatures using a corresponding public key.在我的项目中,我需要使用相应的公钥来验证 PGP 明确签名的签名。 While I did manage to find a code which does that (For example: https://github.com/cjmalloy/openbitpub/blob/64485d64a699eb6096f01b27d5f7e51dd726602f/src/main/java/com/cjmalloy/obp/server/pgp/PgpUtil.java ), it operates on a low level and looks pretty horrible.虽然我确实设法找到了一个代码(例如: https : //github.com/cjmalloy/openbitpub/blob/64485d64a699eb6096f01b27d5f7e51dd726602f/src/main/java/com/cjmalloy/obp/server/pgp/Pgp ),它在低级别运行,看起来非常可怕。

I was thinking, perhaps there exist some specialized parsers that can consume -----BEGIN PGP PUBLIC KEY BLOCK-----xxx-----END PGP PUBLIC KEY BLOCK----- and -----BEGIN PGP SIGNED MESSAGE-----xxx-----BEGIN PGP SIGNATURE-----xxx-----END PGP SIGNATURE----- blocks so I can check signatures in a more declarative way?我在想,也许存在一些专门的解析器可以使用-----BEGIN PGP PUBLIC KEY BLOCK-----xxx-----END PGP PUBLIC KEY BLOCK----------BEGIN PGP SIGNED MESSAGE-----xxx-----BEGIN PGP SIGNATURE-----xxx-----END PGP SIGNATURE-----块以便我可以以更具声明性的方式检查签名?

I've found related PEMReader class from bouncycastle.openssl package but nothing PGP-related so far.我发现相关PEMReader从类bouncycastle.openssl包,但没有PGP相关为止。

I was thinking, perhaps there exist some specialized parsers that can consume -----BEGIN PGP PUBLIC KEY BLOCK-----xxx-----END PGP PUBLIC KEY BLOCK----- and -----BEGIN PGP SIGNED MESSAGE-----xxx-----BEGIN PGP SIGNATURE-----xxx-----END PGP SIGNATURE----- blocks so I can check signatures in a more declarative way?我在想,也许存在一些专门的解析器可以使用-----BEGIN PGP PUBLIC KEY BLOCK-----xxx-----END PGP PUBLIC KEY BLOCK----------BEGIN PGP SIGNED MESSAGE-----xxx-----BEGIN PGP SIGNATURE-----xxx-----END PGP SIGNATURE-----块以便我可以以更具声明性的方式检查签名?

A parser will not be enough at all -- you will need to implement lots of OpenPGP-specific functions like symmetric key derivation from strings (for encrypted keys), handling of different types of assymetric cryptography algorithms, hash sums, different kinds of packet nesting, ... -- at least you're not required to implement the OpenPGP CBC mode deriate as you don't require encryption (only signatures).一个解析器根本不够——您将需要实现许多特定于 OpenPGP 的功能,例如从字符串派生对称密钥(用于加密密钥)、处理不同类型的非对称加密算法、散列和、不同类型的数据包嵌套, ... -- 至少您不需要实现 OpenPGP CBC 模式,因为您不需要加密(仅签名)。

OpenPGP is much to complicated to write your own parser and crypto code, rely on existing libraries instead. OpenPGP 编写自己的解析器和加密代码非常复杂,而是依赖现有的库。 In the end, with Java you've got two possible roads to follow:最后,对于 Java,您有两条可能的道路可以遵循:

I've found related PEMReader class from bouncycastle.openssl package but nothing PGP-related so far.我从PEMReader包中找到了相关的PEMReader类,但到目前为止没有任何与 PGP 相关的类。

You probably looked in the wrong BouncyCastle package.您可能查看了错误的 BouncyCastle 包。 OpenPGP does not use keys in PEM format (which belongs to the X.509 standard), so this class will not be useful at all. OpenPGP 不使用 PEM 格式的密钥(属于 X.509 标准),所以这个类根本没有用。

I came through the same situation sometimes back.我有时会遇到同样的情况。

This was resolved by using the bouncy castle dependency and by using the method这是通过使用充气城堡依赖项和使用方法解决的

decryptAndVerify(InputStream in, OutputStream fOut, InputStream publicKeyIn, InputStream keyIn, char[] passwd)解密和验证(输入流输入,输出流输出,输入流 publicKeyIn,输入流密钥输入,字符 [] 密码)

in PGP util class在 PGP 实用程序类中

The commercial OpenPGP Library for Java offers a convenient API for verifying clear text signatures. Java的商业OpenPGP 库提供了一个方便的 API 来验证明文签名。 Sample code is:示例代码是:

import com.didisoft.pgp.*;
 
public class VerifyFile {
    public static void main(String[] args) throws Exception{
        // create an instance of the library
        PGPLib pgp = new PGPLib();
 
        // verify and extract the signed content
        SignatureCheckResult signatureCheck = pgp.verifyAndExtract("signed.pgp", "sender_public_key.asc", "OUTPUT.txt");      
        if (signatureCheck == SignatureCheckResult.SignatureVerified) {
            System.out.println("The signature is valid.");
        } else if (signatureCheck == SignatureCheckResult.SignatureBroken) {
            System.out.println("Message corrupted or signature forged");
        } else if (signatureCheck == SignatureCheckResult.PublicKeyNotMatching) {
            System.out.println("Signature not matching provided public key (the message is from another sender)");
        } else {
              System.out.println("No signature found in message");
        }
    }
}

Disclaimer: I work for DidiSoft.免责声明:我为滴滴出行工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM