第一个坑:生产上业务要求用的是子密钥加密文件。
@SuppressWarnings("unchecked") public static PGPPublicKey readPublicKey(InputStream in) throws IOException, PGPException {
PGPPublicKeyRingCollection keyRingCollection = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(in)); PGPPublicKey publicKey = null;
Iterator<PGPPublicKeyRing> rIt = keyRingCollection.getKeyRings();
while (publicKey == null && rIt.hasNext()) {
PGPPublicKeyRing kRing = rIt.next();
Iterator<PGPPublicKey> kIt = kRing.getPublicKeys();
while (publicKey == null && kIt.hasNext()) {
PGPPublicKey key = kIt.next();
if (key.isEncryptionKey()) {
//重点在这里面,从公钥环上面取到的公钥对象是有俩个的,遍历时第一个是主密钥,而我们需要的是子密钥,也就是第二个。可是查看源码,并没有判断是否子密钥的方法。最后找到一个,