title: How to encrypt a file using PGP encryption in jruby9.1.2.0?
body: <p>I am trying to encrypt file using gpg encryption before sending it in my jruby project. However I did not find adequate resources for it. I tried using <a href="https://github.com/ueno/ruby-gpgme" rel="nofollow noreferrer">ruby-gpgme</a> but jruby did not support C libraries. I tried reading <a href="http://bouncycastle.org/docs/pgdocs1.5on/index.html" rel="nofollow noreferrer">Bouncy Castle</a> but I was overwhelmed by the class documentation and did not find a simple article for encrypting a file. </p>
  
  <p>Vivek's answern in <a href="https://stackoverflow.com/questions/1455037/is-there-a-good-gnupg-encryption-library-for-java-scala">this</a> question comes close to my solution, but there is only solution for decrypting the file. I am currently following <a href="http://sloanseaman.com/wordpress/2012/05/13/revisited-pgp-encryptiondecryption-in-java/" rel="nofollow noreferrer">this article</a> and trying to interface the java code in jruby to no avail. I think the <code>encryptFile</code> function is what I need which is as follows: </p>
  
  <pre><code>public static void encryptFile(
          OutputStream out,
          String fileName,
          PGPPublicKey encKey,
          boolean armor,
          boolean withIntegrityCheck)
          throws IOException, NoSuchProviderException, PGPException
      {
          Security.addProvider(new BouncyCastleProvider());
  
          if (armor) {
              out = new ArmoredOutputStream(out);
          }
  
          ByteArrayOutputStream bOut = new ByteArrayOutputStream();
          PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);
  
          PGPUtil.writeFileToLiteralData(
                  comData.open(bOut),
                  PGPLiteralData.BINARY,
                  new File(fileName) );
  
          comData.close();
  
          BcPGPDataEncryptorBuilder dataEncryptor = new BcPGPDataEncryptorBuilder(PGPEncryptedData.TRIPLE_DES);
          dataEncryptor.setWithIntegrityPacket(withIntegrityCheck);
          dataEncryptor.setSecureRandom(new SecureRandom());
  
          PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(dataEncryptor);
          encryptedDataGenerator.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(encKey));
  
          byte[] bytes = bOut.toByteArray();
          OutputStream cOut = encryptedDataGenerator.open(out, bytes.length);
          cOut.write(bytes);
          cOut.close();
          out.close();
      }
  
  )
  </code></pre>
  
  <p>I am getting following error:</p>
  
  <pre><code>NoMethodError: undefined method `ZIP' for Java::OrgBouncycastleOpenpgp::PGPCompressedData:Class
  </code></pre>
  
  <p>at </p>
  
  <pre><code> PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);
  </code></pre>
  
  <p>It would be a great help if you can help me with the code or with the encrypting file using gpg in jruby as a whole. </p>
  
  <p><strong>Update 1</strong>
  The ZIP value turns out to be constant of integer value and is listed in <a href="https://www.bouncycastle.org/docs/pgdocs1.5on/constant-values.html#org.bouncycastle.bcpg.CompressionAlgorithmTags.ZIP" rel="nofollow noreferrer">this</a> page.</p>
  
  <p><strong>Update 2</strong>
  I made it till the function:</p>
  
  <pre><code>PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(dataEncryptor);
      encryptedDataGenerator.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(encKey)); // encKey is class PGPPublicKey's instance
  </code></pre>
  
  <p>I have public key generated from OS. How do I create a PGPPublic Key instance <code>encKey</code>from the public key string I have?</p>