弹性城堡 – GPG。从秘钥中提取公钥

huangapple go评论68阅读模式
英文:

BouncyCastle - GPG. Extract Public Key from Secret Key

问题

我正在使用以下Java代码从秘密密钥中提取公钥:

PGPSecretKeyRingCollection ring = new PGPSecretKeyRingCollection(decoderStream,
            new JcaKeyFingerprintCalculator());
Iterator<PGPSecretKeyRing> it = ring.getKeyRings();
while (it.hasNext()) {
    PGPSecretKeyRing key = it.next();
    Iterator<PGPPublicKey> itpublic = key.getPublicKeys();
    while (itpublic.hasNext()) {
        PGPPublicKey pubKey = itpublic.next();
        // use this pubKey
    }
}

如果我尝试将该密钥导出到ArmoredOutputStream中,我会得到类似以下的内容:

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: BCPG v1.66

hQEMA6GfAr1vmvVrAQf/XF/6DqSxZu0dXXVnhfxoot+YTLBrwnec/af72R8G1aJI
[...]
=eLkg
-----END PGP PUBLIC KEY BLOCK-----

如果我使用这个密钥从Java代码加密某些内容,一切正常。

但如果我尝试使用这个密钥从命令行(或其他客户端,如Kleopatra)加密文件:

$ gpg --import pubKey.gpg
$ gpg --encrypt ...

我会收到"Unusable public key"错误消息。

我是否在从Java代码导出公钥时做错了什么?

英文:

I'm using this java code to extract the public key from secret key:

PGPSecretKeyRingCollection ring = new PGPSecretKeyRingCollection(decoderStream,
            new JcaKeyFingerprintCalculator());
Iterator&lt;PGPSecretKeyRing&gt; it = ring.getKeyRings();
while (it.hasNext()) {
    PGPSecretKeyRing key = it.next();
    Iterator&lt;PGPPublicKey&gt; itpublic = key.getPublicKeys();
    while (itpublic.hasNext()) {
        PGPPublicKey pubKey = itpublic.next();
        // use this pubKey
    }
}

If I try to export that key in an ArmoredOutputStream, I get something like:

    -----BEGIN PGP PUBLIC KEY BLOCK-----
    Version: BCPG v1.66
    
    hQEMA6GfAr1vmvVrAQf/XF/6DqSxZu0dXXVnhfxoot+YTLBrwnec/af72R8G1aJI
    [...]
    =eLkg
    -----END PGP PUBLIC KEY BLOCK-----

If I use this key to encrypt something from java code, everything works fine.

If I use this key to encrypt files from command line (or other clients like Kleopatra):

$ gpg --import pubKey.gpg
$ gpg --encrypt ...

I get the "Unusable public key" error.

Am I doing something wrong with the public key export from java code?

答案1

得分: 1

你必须使用所有的PublicKeyRing,而不仅仅是主公钥:

List<PGPPublicKey> list = new ArrayList<>();
Iterator<PGPSecretKeyRing> it = ring.getKeyRings();
while (it.hasNext()) {
    PGPSecretKeyRing secretRing = it.next();
    Iterator<PGPPublicKey> itpublic = secretRing.getPublicKeys();
    while (itpublic.hasNext()) {
        PGPPublicKey pub = itpublic.next();
        list.add(pub);
    }
    Iterator<PGPPublicKey> itextrapublic = secretRing.getExtraPublicKeys();
    while (itextrapublic.hasNext()) {
        PGPPublicKey pub = itextrapublic.next();
        list.add(pub);
    }
}
PGPPublicKeyRing publicRing = new PGPPublicKeyRing(list);
publicRing.encode(armoredOutputStream);
英文:

You have to use all the PublicKeyRing, not only the master public key:

List&lt;PGPPublicKey&gt; list = new ArrayList&lt;&gt;();
Iterator&lt;PGPSecretKeyRing&gt; it = ring.getKeyRings();
while (it.hasNext()) {
    PGPSecretKeyRing secretRing = it.next();
    Iterator&lt;PGPPublicKey&gt; itpublic = secretRing.getPublicKeys();
    while (itpublic.hasNext()) {
        PGPPublicKey pub = itpublic.next();
        list.add(pub);
    }
    Iterator&lt;PGPPublicKey&gt; itextrapublic = secretRing.getExtraPublicKeys();
    while (itextrapublic.hasNext()) {
        PGPPublicKey pub = itextrapublic.next();
        list.add(pub);
    }
}
PGPPublicKeyRing publicRing = new PGPPublicKeyRing(list);
publicRing.encode(armoredOutputStream)

huangapple
  • 本文由 发表于 2020年9月24日 18:32:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64044582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定