Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用bouncy castle库从PGP公钥中获取密钥ID?_Java_Cryptography_Bouncycastle_Public Key_Pgp - Fatal编程技术网

Java 如何使用bouncy castle库从PGP公钥中获取密钥ID?

Java 如何使用bouncy castle库从PGP公钥中获取密钥ID?,java,cryptography,bouncycastle,public-key,pgp,Java,Cryptography,Bouncycastle,Public Key,Pgp,我正在尝试从PGP公钥获取密钥id。经过大量的谷歌搜索,我找到了一个使用BouncyCastleJavaAPI读取公钥的解决方案 其代码段如下所示: public static PGPPublicKey readPublicKey(InputStream in) throws IOException, PGPException { in = org.bouncycastle.openpgp.PGPUtil.getDecoderStream(in); PGPPub

我正在尝试从PGP公钥获取密钥id。经过大量的谷歌搜索,我找到了一个使用BouncyCastleJavaAPI读取公钥的解决方案

其代码段如下所示:

public static PGPPublicKey readPublicKey(InputStream in) throws IOException, PGPException {
        in = org.bouncycastle.openpgp.PGPUtil.getDecoderStream(in);

        PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(in);  

        PGPPublicKey key = null;

        //
        // iterate through the key rings.
        //
        Iterator<PGPPublicKeyRing> rIt = pgpPub.getKeyRings();

        while (key == null && rIt.hasNext()) {
            PGPPublicKeyRing kRing = rIt.next();
            Iterator<PGPPublicKey> kIt = kRing.getPublicKeys();
            while (key == null && kIt.hasNext()) {
                PGPPublicKey k = kIt.next();

                if (k.isEncryptionKey()) {
                    key = k;
                }
            }
        }

        if (key == null) {
            throw new IllegalArgumentException("Can't find encryption(public) key in key ring.");
        }

        return key;
    }
需要替换为

PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(in, new JcaKeyFingerprintCalculator());

我尝试了BC和JCA键盘指纹计算器,它们都给出了相同的键盘ID

所以问题是,

  • 这里应该使用哪个指纹计算器

  • 是否存在相关/需要选择其中一个的特定场景? 我检查了两个计算器类的javadocs,但没有提供太多信息

  • 是否有其他解决方案可以获取java中PGP公钥的密钥id?如果是,请建议

请注意,我这里不生成密钥-这些密钥将由客户提供,我没有关于它们的太多信息

我是java中公钥加密实现的新手。如有任何与本主题相关的信息,将不胜感激


谢谢。

我有完全相同的问题,这似乎没有记录在API中。简而言之,BC实现是针对BC的,并且具有一些标准Java实现JCE所不具备的特性和算法。
PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(in, new JcaKeyFingerprintCalculator());
PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(in, new BcKeyFingerprintCalculator());