我使用下面的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 ...我得到了“不可用的公钥”错误。
我是不是在从java代码导出公钥方面做错了什么?
发布于 2020-09-24 23:12:07
您必须使用所有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)https://stackoverflow.com/questions/64044582
复制相似问题