<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Re: building a encrypt Content-like script for NiFi in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/building-a-encrypt-Content-like-script-for-NiFi-with-PGP/m-p/338037#M232741</link>
    <description>&lt;P&gt;Thanks, but i must use PGP, with the public and private keys and all that.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 08 Mar 2022 23:27:03 GMT</pubDate>
    <dc:creator>FJATP</dc:creator>
    <dc:date>2022-03-08T23:27:03Z</dc:date>
    <item>
      <title>building a encrypt Content-like script for NiFi with PGP Encryption</title>
      <link>https://community.cloudera.com/t5/Support-Questions/building-a-encrypt-Content-like-script-for-NiFi-with-PGP/m-p/338017#M232731</link>
      <description>&lt;P&gt;I need to encrypt some content with PGP, but i can't use the processor Encrypt Content because we require that the "Keyring file route" and the "passphrase" must be entered through attributes (We must use an old version of NiFi). I make this script using&amp;nbsp;&lt;A href="https://github.com/apache/nifi/blob/master/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/EncryptContent.java" target="_blank" rel="noopener"&gt;the repo of NiFi&lt;/A&gt;&amp;nbsp;and modifying the things that i need and using a single script file considering how the processor works. My problem is that the result can't be decrypted backwards.&lt;BR /&gt;I'm using Nifi&amp;nbsp;1.13.2&lt;BR /&gt;With Groovy as language&lt;BR /&gt;and Bouncy Castle (bcpg-jdk15on-1.70 and bcprov-jdk15on-1.70)&lt;BR /&gt;And my code is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="java"&gt;import org.bouncycastle.bcpg.ArmoredOutputStream
import org.bouncycastle.openpgp.PGPCompressedData
import org.bouncycastle.openpgp.PGPCompressedDataGenerator
import org.bouncycastle.openpgp.PGPEncryptedData
import org.bouncycastle.openpgp.PGPEncryptedDataGenerator
import org.bouncycastle.openpgp.PGPException
import org.bouncycastle.openpgp.PGPLiteralData
import org.bouncycastle.openpgp.PGPPublicKey
import org.bouncycastle.openpgp.PGPLiteralDataGenerator
import org.bouncycastle.openpgp.operator.PGPKeyEncryptionMethodGenerator
import org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyKeyEncryptionMethodGenerator
import org.bouncycastle.openpgp.operator.jcajce.JcePGPDataEncryptorBuilder
import org.bouncycastle.openpgp.PGPPublicKeyRing
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator
import org.bouncycastle.jce.provider.BouncyCastleProvider

import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
import java.security.SecureRandom
import java.util.Date
import java.util.zip.Deflater

FlowFile flowFile = session.get()

if (!flowFile) {
    return
}

final int BUFFER_SIZE = 65536;
final int BLOCK_SIZE = 4096;

try {			
	flowFile = session.write(flowFile, { inputStream, outputStream -&amp;gt;
	output = new ArmoredOutputStream(outputStream)
	int cipher = PGPEncryptedData.AES_256
	String provider = "BC"
	filename = "encrypted.txt"
	publicKeyUserId = flowFile.getAttribute('PublicKeyUserId')
	publicKeyringFile = flowFile.getAttribute('publicKeyRoute')
	publicKey = getPublicKey(publicKeyUserId, publicKeyringFile)

	try {
    	PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(
        new JcePGPDataEncryptorBuilder(cipher).setWithIntegrityPacket(true).setSecureRandom(new SecureRandom()).setProvider(provider))

		encryptedDataGenerator.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(publicKey).setProvider(provider))
		
		OutputStream encryptedOut = encryptedDataGenerator.open(output, new byte[BUFFER_SIZE])

		PGPCompressedDataGenerator compressedDataGenerator = new PGPCompressedDataGenerator(PGPCompressedData.ZIP, Deflater.BEST_SPEED)

		OutputStream compressedOut = compressedDataGenerator.open(encryptedOut, new byte[BUFFER_SIZE])
		PGPLiteralDataGenerator literalDataGenerator = new PGPLiteralDataGenerator()

		OutputStream literalOut = literalDataGenerator.open(compressedOut, PGPLiteralData.BINARY, filename, new Date(), new byte[BUFFER_SIZE])
		
		final byte[] buffer = new byte[BLOCK_SIZE];
        int len;
        while ((len = inputStream.read(buffer)) &amp;gt;= 0) {
        	literalOut.write(buffer, 0, len);
        }
	} finally {
    	output.close();
    }

} as StreamCallback)

session.transfer(flowFile, REL_SUCCESS)
} catch (Exception e) {
    log.error("There was an error encrypting the attributes: ${e.getMessage()}")
    session.transfer(flowFile, REL_FAILURE)
}

static PGPPublicKey getPublicKey(String userId, String publicKeyringFile){
	FileInputStream keyInputStream = new FileInputStream(publicKeyringFile)
    // Form the PublicKeyRing collection (1.53 way with fingerprint calculator)
    PGPPublicKeyRingCollection pgpPublicKeyRingCollection = new PGPPublicKeyRingCollection(keyInputStream, new BcKeyFingerprintCalculator());
    // Iterate over all public keyrings
    Iterator&amp;lt;PGPPublicKeyRing&amp;gt; iter = pgpPublicKeyRingCollection.getKeyRings();
    PGPPublicKeyRing keyRing;
    while (iter.hasNext()) {
    	keyRing = iter.next();

		// Iterate over each public key in this keyring
        Iterator&amp;lt;PGPPublicKey&amp;gt; keyIter = keyRing.getPublicKeys();
        while (keyIter.hasNext()) {
        	PGPPublicKey publicKey = keyIter.next();

			// Iterate over each userId attached to the public key
            Iterator userIdIterator = publicKey.getUserIDs();
            while (userIdIterator.hasNext()) {
            	String id = (String) userIdIterator.next();
                if (userId.equalsIgnoreCase(id)) {
                	return publicKey;
                }
            }
		}
	}
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;Can anyone help me making this work?&lt;BR /&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 08 Mar 2022 23:29:39 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/building-a-encrypt-Content-like-script-for-NiFi-with-PGP/m-p/338017#M232731</guid>
      <dc:creator>FJATP</dc:creator>
      <dc:date>2022-03-08T23:29:39Z</dc:date>
    </item>
    <item>
      <title>Re: building a encrypt Content-like script for NiFi</title>
      <link>https://community.cloudera.com/t5/Support-Questions/building-a-encrypt-Content-like-script-for-NiFi-with-PGP/m-p/338025#M232734</link>
      <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/96380"&gt;@FJATP&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please have a look at this.&amp;nbsp; It doesn't use PGP but it's a similar encryption implementation using Groovy:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.cloudera.com/t5/Support-Questions/How-to-encrypt-a-column-using-nifi/m-p/336023/highlight/true#M232103" target="_blank"&gt;https://community.cloudera.com/t5/Support-Questions/How-to-encrypt-a-column-using-nifi/m-p/336023/highlight/true#M232103&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In that case I used parameters but you can replace them with variables/attributes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let me know if that helps.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;André&lt;/P&gt;&lt;P&gt;&lt;EM&gt;--&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Was your question answered? Make sure to mark the answer as the accepted solution.&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;If you find a reply useful, say thanks by clicking on the thumbs up button.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Mar 2022 20:37:46 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/building-a-encrypt-Content-like-script-for-NiFi-with-PGP/m-p/338025#M232734</guid>
      <dc:creator>araujo</dc:creator>
      <dc:date>2022-03-08T20:37:46Z</dc:date>
    </item>
    <item>
      <title>Re: building a encrypt Content-like script for NiFi</title>
      <link>https://community.cloudera.com/t5/Support-Questions/building-a-encrypt-Content-like-script-for-NiFi-with-PGP/m-p/338037#M232741</link>
      <description>&lt;P&gt;Thanks, but i must use PGP, with the public and private keys and all that.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Mar 2022 23:27:03 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/building-a-encrypt-Content-like-script-for-NiFi-with-PGP/m-p/338037#M232741</guid>
      <dc:creator>FJATP</dc:creator>
      <dc:date>2022-03-08T23:27:03Z</dc:date>
    </item>
    <item>
      <title>Re: building a encrypt Content-like script for NiFi</title>
      <link>https://community.cloudera.com/t5/Support-Questions/building-a-encrypt-Content-like-script-for-NiFi-with-PGP/m-p/338038#M232742</link>
      <description>&lt;P&gt;What's the error you're getting?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;André&lt;/P&gt;</description>
      <pubDate>Tue, 08 Mar 2022 23:28:28 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/building-a-encrypt-Content-like-script-for-NiFi-with-PGP/m-p/338038#M232742</guid>
      <dc:creator>araujo</dc:creator>
      <dc:date>2022-03-08T23:28:28Z</dc:date>
    </item>
    <item>
      <title>Re: building a encrypt Content-like script for NiFi</title>
      <link>https://community.cloudera.com/t5/Support-Questions/building-a-encrypt-Content-like-script-for-NiFi-with-PGP/m-p/338039#M232743</link>
      <description>&lt;P&gt;When i try to decrypt it with kleoptatra, it throws the diagnostic message "gpg: [don't know]: 1st length byte missing".&lt;/P&gt;</description>
      <pubDate>Tue, 08 Mar 2022 23:31:02 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/building-a-encrypt-Content-like-script-for-NiFi-with-PGP/m-p/338039#M232743</guid>
      <dc:creator>FJATP</dc:creator>
      <dc:date>2022-03-08T23:31:02Z</dc:date>
    </item>
    <item>
      <title>Re: building a encrypt Content-like script for NiFi</title>
      <link>https://community.cloudera.com/t5/Support-Questions/building-a-encrypt-Content-like-script-for-NiFi-with-PGP/m-p/338048#M232747</link>
      <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/96380"&gt;@FJATP&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can you share your flow definition?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;André&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2022 02:53:07 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/building-a-encrypt-Content-like-script-for-NiFi-with-PGP/m-p/338048#M232747</guid>
      <dc:creator>araujo</dc:creator>
      <dc:date>2022-03-09T02:53:07Z</dc:date>
    </item>
    <item>
      <title>Re: building a encrypt Content-like script for NiFi</title>
      <link>https://community.cloudera.com/t5/Support-Questions/building-a-encrypt-Content-like-script-for-NiFi-with-PGP/m-p/338054#M232749</link>
      <description>&lt;P&gt;In the test version that i build is something like this: it creates the flow (In the original is a query from a database), adds the custom attributes (like the filename, the location of the public key, etc, those i get them from another table), it encrypts the contents and finally the encrypted content is transferred to an ftp.&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="FJATP_0-1646806629891.png" style="width: 400px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/33816iEE2B7DF9064BAA14/image-size/medium?v=v2&amp;amp;px=400" role="button" title="FJATP_0-1646806629891.png" alt="FJATP_0-1646806629891.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;This is what you need, right? I can upload the xml somewhere if you like.&lt;BR /&gt;&lt;BR /&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2022 06:21:51 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/building-a-encrypt-Content-like-script-for-NiFi-with-PGP/m-p/338054#M232749</guid>
      <dc:creator>FJATP</dc:creator>
      <dc:date>2022-03-09T06:21:51Z</dc:date>
    </item>
  </channel>
</rss>

