2016-05-05 36 views
2

私は電子メールを送信するサーバー用のモジュールを作成しています。クライアントアプリケーションでは、多くの受取人を追加することができ、それぞれに独自の公開鍵があります。複数の鍵を使って添付ファイルを暗号化したいたとえば、3人の受信者を追加する場合、添付ファイルは3つの異なる公開鍵で暗号化する必要があります。私はそれを行う弾力のある城を使用していますが、それは暗号化プロセスの最初の公開鍵のためにのみ動作します。私は最初の人だけが自分の秘密鍵を使って解読できることを意味します。残りの人はそれが機能しません。 などの各キールックスのためのメソッドを追加するための私のコード:複数のRSA公開鍵を使用した暗号化

PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(dataEncryptor); 

for (PGPPublicKey publicKey : publicKeys) { 
     encryptedDataGenerator.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(publicKey)); 
} 

全体の方法は次のようになります。

public File encryptFile(String fileName, 
     boolean armor, 
     boolean withIntegrityCheck) throws IOException, 
     NoSuchProviderException, 
     PGPException { 
    Security.addProvider(new BouncyCastleProvider()); 

    ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 

    PGPCompressedDataGenerator comData 
      = new PGPCompressedDataGenerator(PGPCompressedData.UNCOMPRESSED); 

    PGPUtil.writeFileToLiteralData(comData.open(bOut), 
      PGPLiteralData.BINARY, 
      new File(fileName)); 

    comData.close(); 

    BcPGPDataEncryptorBuilder dataEncryptor 
      = new BcPGPDataEncryptorBuilder(PGPEncryptedData.AES_256); 

    dataEncryptor.setWithIntegrityPacket(withIntegrityCheck); 

    dataEncryptor.setSecureRandom(new SecureRandom()); 

    PGPEncryptedDataGenerator encryptedDataGenerator 
      = new PGPEncryptedDataGenerator(dataEncryptor); 

    for (PGPPublicKey publicKey : publicKeys) { 
     encryptedDataGenerator.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(publicKey)); 
    } 

    byte[] bytes = bOut.toByteArray(); 

    FileOutputStream localByteArrayOutputStream = new FileOutputStream(fileName); 

    Object localObject = localByteArrayOutputStream; 

    if (armor) { 

     localObject = new ArmoredOutputStream((OutputStream) localObject); 

    } 

    OutputStream localOutputStream = encryptedDataGenerator.open((OutputStream) localObject, 
      bytes.length); 

    localOutputStream.write(bytes); 

    localOutputStream.close(); 

    return new File(fileName); 
} 

は、誰かが私を助け、私が間違ってやっているものを私に伝えることができますか?

ありがとうございました。

[編集] このコードは機能し、複数のキーを読み込むメソッドに問題がありました。

+0

もっと標準的なアプローチについては、http://stackoverflow.com/questions/38846/how-to-encrypt-one-message-for-multiple-recipientsを参照してください。 – jonrsharpe

+0

私は対称キーを生成する必要があることを理解していますファイルを暗号化してから、すべての公開鍵を使用してこの鍵を暗号化しますが、私はbouncycastleを使用してこれを行う方法がわかりません。私は、OpenPGP標準を使用して任意のプログラムを使用して解読することを可能にする必要があります。 –

答えて

1

私は1年後に同じ問題を抱えました。私はあなたがあなたを解決したことを願っています。他の人が同様の問題を抱えている場合に備えて、ここに私のソリューションを書いています。

暗号化コードに問題はありません。問題は解読にあるかもしれません。暗号化されたデータオブジェクトの場合、オブジェクトに格納されているキーIDを使用して正しいキーを見つける必要があります。私の解読プロセスは以下のようになります:

private byte[] decryptWithKey(byte[] bytes, PGPSecretKey secKey, String pass) 
     throws PGPException, IOException { 
    PBESecretKeyDecryptor keyDec = new JcePBESecretKeyDecryptorBuilder(
      new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()) 
      .setProvider("BC").build(pass.toCharArray()); 
    ByteArrayOutputStream bout = new ByteArrayOutputStream(); 

    PGPPrivateKey privateKey = secKey.extractPrivateKey(keyDec); 
    PublicKeyDataDecryptorFactory dec1 = 
      new JcePublicKeyDataDecryptorFactoryBuilder().setProvider("BC").build(privateKey); 
    JcaPGPObjectFactory objFact = new JcaPGPObjectFactory(bytes); 
    PGPEncryptedDataList encList = (PGPEncryptedDataList) objFact.nextObject(); 

    PGPPublicKeyEncryptedData encD = null; 
    for(Iterator<PGPPublicKeyEncryptedData> it = encList.iterator(); it.hasNext();) { 
     PGPPublicKeyEncryptedData end = it.next(); 
     if (secKey.getKeyID() == end.getKeyID()) { 
      encD = end; 
      break; 
     } 
    } 
    assert encD != null: "Cannot find encrypted data with key: " 
      + Long.toHexString(secKey.getKeyID()); 
    InputStream in = encD.getDataStream(dec1); 
    byte[] buf = new byte[BufferSize]; 
    for (int len; (len = in.read(buf)) >= 0;) { 
     bout.write(buf, 0, len); 
    } 
    bout.close(); 
    return bout.toByteArray(); 
} 

鍵は、暗号化されたオブジェクトのための一致する鍵を見つけるforループです。

関連する問題