英文:
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<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
}
}
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<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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论