使用 Bouncy Castle PGP 从单个文件加载多个 public 密钥
Load multiple public keys from single file with Bouncy Castle PGP
我有一个文本文件,其中包含几个 ASCII 装甲的 OpenPGP public 密钥。我想使用 Bouncy Castle 将字符串加密到此文件中包含的所有 public 密钥。但是,当我将文件加载到 PGPPublicKeyRingCollection
时,只返回第一个键:
private static List<PGPPublicKey> readPublicKeys(InputStream input) throws IOException, PGPException {
PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(input), new JcaKeyFingerprintCalculator());
List<PGPPublicKey> keys = new ArrayList<>();
// size is 1 here
logger.debug("size " + pgpPub.size());
@SuppressWarnings("unchecked")
Iterator<PGPPublicKeyRing> keyRingIter = pgpPub.getKeyRings();
while (keyRingIter.hasNext()) {
PGPPublicKeyRing keyRing = keyRingIter.next();
@SuppressWarnings("unchecked")
Iterator<PGPPublicKey> keyIter = keyRing.getPublicKeys();
while (keyIter.hasNext()) {
PGPPublicKey key = keyIter.next();
// there is only ever 1 key here as well
if (key.isEncryptionKey()) {
keys.add(key);
}
}
}
if (keys.size() > 0) {
return keys;
} else {
throw new IllegalArgumentException("Can't find encryption key in key ring.");
}
}
我是不是漏掉了什么?
FWIW,当我 运行 $ gpg --dry-run my.keys
时,它正确检测到所有 7 个 public 键并输出它们的指纹。
我不知道你是否最终找到了这个问题的答案,但我也遇到了同样的问题。如果我在命令行上从 gpg 导出多个密钥:
gpg --export --armor 374ABFC6 B3E4E0A5 > combined.public.asc
然后为该文件创建了一个 InputStream,Bouncy Castle 能够毫无问题地导入这两个密钥。
如果您的密钥来自其他地方,这可能没有帮助,但是您拥有的 public 密钥 InputStream 没有正确编码(我假设因为它只抓取第一个密钥)包含多个密钥.如果我只是将两个 PGP 密钥块连接到同一个文件中,那么它只会读取第一个密钥。像上面那样导出后,combined.public.asc 文件只有一个巨大的 PGP 密钥块。
我有一个文本文件,其中包含几个 ASCII 装甲的 OpenPGP public 密钥。我想使用 Bouncy Castle 将字符串加密到此文件中包含的所有 public 密钥。但是,当我将文件加载到 PGPPublicKeyRingCollection
时,只返回第一个键:
private static List<PGPPublicKey> readPublicKeys(InputStream input) throws IOException, PGPException {
PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(input), new JcaKeyFingerprintCalculator());
List<PGPPublicKey> keys = new ArrayList<>();
// size is 1 here
logger.debug("size " + pgpPub.size());
@SuppressWarnings("unchecked")
Iterator<PGPPublicKeyRing> keyRingIter = pgpPub.getKeyRings();
while (keyRingIter.hasNext()) {
PGPPublicKeyRing keyRing = keyRingIter.next();
@SuppressWarnings("unchecked")
Iterator<PGPPublicKey> keyIter = keyRing.getPublicKeys();
while (keyIter.hasNext()) {
PGPPublicKey key = keyIter.next();
// there is only ever 1 key here as well
if (key.isEncryptionKey()) {
keys.add(key);
}
}
}
if (keys.size() > 0) {
return keys;
} else {
throw new IllegalArgumentException("Can't find encryption key in key ring.");
}
}
我是不是漏掉了什么?
FWIW,当我 运行 $ gpg --dry-run my.keys
时,它正确检测到所有 7 个 public 键并输出它们的指纹。
我不知道你是否最终找到了这个问题的答案,但我也遇到了同样的问题。如果我在命令行上从 gpg 导出多个密钥:
gpg --export --armor 374ABFC6 B3E4E0A5 > combined.public.asc
然后为该文件创建了一个 InputStream,Bouncy Castle 能够毫无问题地导入这两个密钥。
如果您的密钥来自其他地方,这可能没有帮助,但是您拥有的 public 密钥 InputStream 没有正确编码(我假设因为它只抓取第一个密钥)包含多个密钥.如果我只是将两个 PGP 密钥块连接到同一个文件中,那么它只会读取第一个密钥。像上面那样导出后,combined.public.asc 文件只有一个巨大的 PGP 密钥块。