私は電子メールを送信するサーバー用のモジュールを作成しています。クライアントアプリケーションでは、多くの受取人を追加することができ、それぞれに独自の公開鍵があります。複数の鍵を使って添付ファイルを暗号化したいたとえば、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);
}
は、誰かが私を助け、私が間違ってやっているものを私に伝えることができますか?
ありがとうございました。
[編集] このコードは機能し、複数のキーを読み込むメソッドに問題がありました。
もっと標準的なアプローチについては、http://stackoverflow.com/questions/38846/how-to-encrypt-one-message-for-multiple-recipientsを参照してください。 – jonrsharpe
私は対称キーを生成する必要があることを理解していますファイルを暗号化してから、すべての公開鍵を使用してこの鍵を暗号化しますが、私はbouncycastleを使用してこれを行う方法がわかりません。私は、OpenPGP標準を使用して任意のプログラムを使用して解読することを可能にする必要があります。 –