2016-06-21 1 views
0

ASCII- 기갑 OpenPGP 공개 키가 여러 개인 텍스트 파일이 있습니다. Bouncy Castle을 사용하여이 파일에 포함 된 모든 공개 키로 문자열을 암호화하려고합니다. 나는 PGPPublicKeyRingCollection로 파일을로드 할 때, 첫 번째 키가 반환됩니다Bouncy Castle PGP를 사용하여 하나의 파일에서 여러 공개 키로드

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 개의 공개 키를 모두 감지하여 지문을 출력합니다.

답변

2

마침내이 질문에 대한 답변을 찾았는지 모르겠지만 동일한 문제가 있습니다. 내가 명령 줄에서 GPG에서 여러 키를 내보내는 경우 :

gpg --export --armor 374ABFC6 B3E4E0A5 > combined.public.asc 

그리고 그 파일에 대한 InputStream를 생성, 탄력 성 문제없이 그 열쇠 모두를 가져올 수 있었다.

열쇠가 다른 곳에서 오는 경우가 도움이되지 않을 수도 있습니다,하지만 여러 개의 키를 포함하는 제대로 인코딩되지 않은이 공개 키의 InputStream이 (가 첫 번째 키를 잡는 것 때문에 내가 있으리라 믿고있어). 두 개의 PGP 키 블록을 동일한 파일에 연결하면 첫 번째 키만 읽습니다. 위에서와 같이 export 한 후에, combined.public.asc 파일은 하나의 거대한 PGP 키 블록만을 가지고 있습니다.

관련 문제