2017-10-05 27 views
1

gpgを使用して暗号化された暗号化されたファイルを書き込もうとしていますが、1つのチャンクの代わりにインクリメンタルに書き込んでいます。私はGnuPGで鍵を生成し、公開鍵を使って暗号化しています。JavaでのBouncyCastle PGPユーティリティによるインクリメンタル暗号化

public static byte[] encrypt(byte[] clearData, PGPPublicKey encKey, 
      String fileName,boolean withIntegrityCheck, boolean armor) 
      throws IOException, PGPException, NoSuchProviderException { 
     if (fileName == null) { 
      fileName = PGPLiteralData.CONSOLE; 
     } 

     ByteArrayOutputStream encOut = new ByteArrayOutputStream(); 

     OutputStream out = encOut; 
     if (armor) { 
      out = new ArmoredOutputStream(out); 
     } 

     ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 

     PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(
       PGPCompressedDataGenerator.ZIP); 
     OutputStream cos = comData.open(bOut); // open it with the final 
     // destination 
     PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator(); 

     // we want to generate compressed data. This might be a user option 
     // later, 
     // in which case we would pass in bOut. 
     OutputStream pOut = lData.open(cos, // the compressed output stream 
       PGPLiteralData.BINARY, fileName, // "filename" to store 
       clearData.length, // length of clear data 
       new Date() // current time 
       ); 
     pOut.write(clearData); 

     lData.close(); 
     comData.close(); 

     PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(new BcPGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.AES_192).setSecureRandom(new SecureRandom())); 


     cPk.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(encKey)); 

     byte[] bytes = bOut.toByteArray(); 

     OutputStream cOut = cPk.open(out, bytes.length); 

     cOut.write(bytes); // obtain the actual bytes from the compressed stream 

     cOut.close(); 

     out.close(); 

     return encOut.toByteArray(); 
    } 

そして、私はこのようにそのメソッドを使用するために、小さなプロトタイプのテストクラスを持っている:ここで私は暗号化するために使用している方法だ

  PGPPublicKey pubKey = PGPEncryptionTools.readPublicKeyFromCol(new FileInputStream(appProp.getKeyFileName())); 

     byte[] encryptbytes = PGPEncryptionTools.encrypt("\nthis is some test text".getBytes(), pubKey, null, true, false); 
     byte[] encryptbytes2 = PGPEncryptionTools.encrypt("\nmore test text".getBytes(), pubKey, null, true, false); 

     FileOutputStream fos = new FileOutputStream("C:/Users/me/workspace/workspace/spring-batch-project/resources/encryptedfile.gpg"); 
     fos.write(encryptbytes); 
     fos.write(encryptbytes2); 
     fos.flush(); 
     fos.close(); 

これはencryptedfile.gpgを作成しますが、私はGnuPGのに行くときファイルを復号化するには動作しますが、最初の行「これはテストテキストです」が出力されます。

このコードを変更して、両方の行を暗号化してGnuPGで復号化できるようにするにはどうすればよいですか?

答えて

1

PGPEncryptionTools.encrypt(...)メソッドを呼び出すたびに、複数の独立したOpenPGPメッセージを生成しています。 1つのOpenPGPメッセージ(GnuPGが1回の実行でも復号化する)だけを出力するには、すべてのプレーンテキストを1つのストリーム(コードではpOut)に書き込み、最後に最後のバイトをストリームに書き込む前に閉じないでください。

+0

たとえば、このストリームを開いて春のバッチでチャンクに書き込んだり、ストリームを閉じて1つの暗号化ファイルを作成したりすることは可能でしょうか? – Novacane

+0

はい、正確です。単一のストリームに書き込む限り、単一のOpenPGPメッセージが作成されます。 –

関連する問題