public OutputStream encryptFile(OutputStream outputStream, InputStream keyIn, long keyId, String cipher)
      throws IOException {
    try {
      if (Security.getProvider(PROVIDER_NAME) == null) {
        Security.addProvider(new BouncyCastleProvider());
      }

      PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(
          new JcePGPDataEncryptorBuilder(symmetricKeyAlgorithmNameToTag(cipher))
              .setSecureRandom(new SecureRandom())
              .setProvider(PROVIDER_NAME));

      PGPPublicKey publicKey;
      PGPPublicKeyRingCollection keyRings = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(keyIn),
          new BcKeyFingerprintCalculator());
      publicKey = keyRings.getPublicKey(keyId);

      if (publicKey == null) {
        throw new IllegalArgumentException("public key for encryption not found");
      }

      cPk.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(publicKey).setProvider(PROVIDER_NAME));

      OutputStream cOut = cPk.open(outputStream, new byte[BUFFER_SIZE]);

      PGPLiteralDataGenerator literalGen = new PGPLiteralDataGenerator();
      OutputStream _literalOut =
          literalGen.open(cOut, PGPLiteralDataGenerator.BINARY, PAYLOAD_NAME, new Date(), new byte[BUFFER_SIZE]);

      return new ClosingWrapperOutputStream(_literalOut, cOut, outputStream);
    } catch (PGPException e) {
      throw new IOException(e);
    }
  }
public OutputStream encryptFile ( OutputStream outputStream , InputStream keyIn , long keyId , String cipher ) throws IOException { try { if ( Security . getProvider ( PROVIDER_NAME ) == null ) { Security . addProvider ( new BouncyCastleProvider ( ) ) ; } PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator ( new JcePGPDataEncryptorBuilder ( symmetricKeyAlgorithmNameToTag ( cipher ) ) . setSecureRandom ( new SecureRandom ( ) ) . setProvider ( PROVIDER_NAME ) ) ; PGPPublicKey publicKey ; PGPPublicKeyRingCollection keyRings = new PGPPublicKeyRingCollection ( PGPUtil . getDecoderStream ( keyIn ) , new BcKeyFingerprintCalculator ( ) ) ; publicKey = keyRings . getPublicKey ( keyId ) ; if ( publicKey == null ) { throw new IllegalArgumentException ( "public key for encryption not found" ) ; } cPk . addMethod ( new JcePublicKeyKeyEncryptionMethodGenerator ( publicKey ) . setProvider ( PROVIDER_NAME ) ) ; OutputStream cOut = cPk . open ( outputStream , new byte [ BUFFER_SIZE ] ) ; PGPLiteralDataGenerator literalGen = new PGPLiteralDataGenerator ( ) ; OutputStream _literalOut = literalGen . open ( cOut , PGPLiteralDataGenerator . BINARY , PAYLOAD_NAME , new Date ( ) , new byte [ BUFFER_SIZE ] ) ; return new ClosingWrapperOutputStream ( _literalOut , cOut , outputStream ) ; } catch ( PGPException e ) { throw new IOException ( e ) ; } }
Taking in an input {@link OutputStream}, keyring inputstream and a passPhrase, generate an encrypted {@link OutputStream}.
@param outputStream {@link OutputStream} that will receive the encrypted content
@param keyIn keyring inputstream. This InputStream is owned by the caller.
@param keyId key identifier
@param cipher the symmetric cipher to use for encryption. If null or empty then a default cipher is used.
@return an {@link OutputStream} to write content to for encryption
@throws IOException