DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Locking Down the Enterprise: Data Security Patterns for AI Integrations
  • AI Is Finding Bugs Faster Than Enterprises Can Patch — Here's What Data Security Teams Should Do
  • Why Your DLP Policies Fall Short the Moment AI Agents Enter the Picture
  • AI in Patient Portals: From Digital Access to Intelligent Healthcare Experiences

Trending

  • Why More Executors Don't Always Make Spark Faster: Understanding AQE
  • You Do Not Need a PhD to Become an AI Engineer: The Essential Skill Stack
  • Introducing TypeSafe Jev: Typed Judgments as Programming Primitives
  • Member Spotlight: Raghava Dittakavi

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Data
  4. Encrypt and Decrypt a CSV File Using Mule 4

Encrypt and Decrypt a CSV File Using Mule 4

This article includes step-by-step details to encrypt and decrypt a CSV file using Mule 4 PGP. Read on to find out more!

By 
Sapna Tambe
·
Dec. 27, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
8.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Introduction

Data security is an important aspect of the digital world. It helps to prevent data breaches, reduces the risk of data exposure, secures sensitive data, and is highly recommended for the regulatory compliance process. Within any organization, data security's role is to ensure the safety and security of private and commercial/finance data while minimizing exposure risk. PGP (Pretty Good Privacy) is an encryption program that is used to encrypt and decrypt texts, files, e-mails, and directories. Many applications within an organization use CSV files to transfer huge data from one system to another. Securing this data while in transit is a key factor during data transmission. This blog will help you provide a step-by-step procedure to encrypt and decrypt a CSV file at source and destination.

We will encrypt and decrypt the following contents of a CSV file. It is a best practice to have the contents of the file within quotes to avoid any misalignment of the data while reading.Code screenshot.


How PGP Works

For PGP encryption and decryption, one needs to have a public key to encrypt the data and a private key to decrypt the data. You can refer to Create PGP Keys to generate public and private keys. Once the keys are generated, you can configure them in the Mule 4 project.

Encryption Flow








original-CSV-file.csv

Once the data is encrypted, you can write it into a file using the "write file palette."

Decryption Flow








Configure MIME type in Set Payload as shown in the right-side figure. The additional parameters mentioned are optional to configure. Post converting the data into CSV format you can either write back to a CSV file or transform it in JSON format and send it in response.

Set payload screenshot.






Results

Results screenshot #1.Results screenshot #2.

Note: The encoding used for encryption and decryption should be the same.

Sample Code:

XML
 
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
	xmlns:crypto="http://www.mulesoft.org/schema/mule/crypto"
	xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/crypto http://www.mulesoft.org/schema/mule/crypto/current/mule-crypto.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd">
	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="dbb144e4-cf7a-43b0-8f4f-2ad6f048f50e" >
		<http:listener-connection host="0.0.0.0" port="8081" />
	</http:listener-config>
	<crypto:pgp-config name="Public_Mulesy_Crypto_Pgp" doc:name="Crypto Pgp" doc:id="9e479a25-10da-41a6-bc04-f2c99003f0ed" publicKeyring="Public_4232E6BD7151FF89D9F755807AC70C45987C59AA.gpg" >
		<crypto:pgp-key-infos >
			<crypto:pgp-asymmetric-key-info keyId="MulesyKey" fingerprint="4232E6BD7151FF89D9F755807AC70C45987C59AA" />
		</crypto:pgp-key-infos>
	</crypto:pgp-config>
	<crypto:pgp-config name="Private_Mulesy_Crypto_Pgp" doc:name="Crypto Pgp" doc:id="6cdfa3c7-67a2-44d1-a83b-228f0e080b31" privateKeyring="Private_4232E6BD7151FF89D9F755807AC70C45987C59AA.gpg">
		<crypto:pgp-key-infos >
			<crypto:pgp-asymmetric-key-info keyId="Mulesy" passphrase="mulesy" fingerprint="4232E6BD7151FF89D9F755807AC70C45987C59AA"/>
		</crypto:pgp-key-infos>
	</crypto:pgp-config>
	<file:config name="File_Config" doc:name="File Config" doc:id="cc30fad7-5452-4077-8c7e-96d402b7e8b4" />
	<flow name="pgp-sample-encrypt-serviceFlow" doc:id="b6a14e57-5485-46d1-8204-e7d92ab0d6ae" >
		<http:listener doc:name="Listener" doc:id="8bacc2ba-64ee-41b9-954b-43718c72cd62" config-ref="HTTP_Listener_config" path="encrypt"/>
		<file:read doc:name="Read" doc:id="6b9f8ea3-56a6-4eff-a950-fd121668b11e" config-ref="File_Config" path="C:\Users\satambe\test-file\original-csv-file.csv"/>
		<ee:transform doc:name="Create file data" doc:id="4c8751be-3a36-44a2-9d0e-3edce39f617d" >
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/csv quoteValues=true, header=true
---
payload map ( payload01 , indexOfPayload01 ) -> {

		user_id: payload01.user_id,
		order_timestamp: payload01.order_timestamp,
		order_status: payload01.order_status,
		order_total: payload01.order_total
	}]]></ee:set-payload>
			</ee:message>
		</ee:transform>
		<crypto:pgp-encrypt doc:name="Pgp encrypt" doc:id="c6aac455-c0ac-4c36-8026-8a2219cbd683" config-ref="Public_Mulesy_Crypto_Pgp" keyId="MulesyKey"/>
		<logger level="INFO" doc:name="Encrypted Message" doc:id="ce19e7c6-2411-4fda-8e74-ac742a122eea" message="Encrypted Message #[payload]"/>
		<file:write doc:name="Write" doc:id="779f27db-4d37-439a-bec5-2d7103153898" config-ref="File_Config" path="C:\Users\satambe\test-file\encrypted-csv-file.csv"/>
	
</flow>
	<flow name="pgp-sample-service-decrypted-Flow" doc:id="c5cc9273-a728-44d0-96ec-29ad075295db" >
		<http:listener doc:name="Listener" doc:id="beac6100-6078-4663-93e6-c164ba641610" config-ref="HTTP_Listener_config" path="decrypt"/>
		<file:read doc:name="Read file content" doc:id="28a667c9-8fd4-4fb0-8718-853593b7f14b" config-ref="File_Config" path="C:\Users\satambe\test-file\encrypted-csv-file.csv"/>
		<crypto:pgp-decrypt doc:name="Pgp decrypt" doc:id="c525a1cb-eb01-4d00-9986-55cd9f789285" config-ref="Private_Mulesy_Crypto_Pgp"/>
		<logger level="INFO" doc:name="Decrypted Message" doc:id="f951d83a-4b38-4a5d-a9b2-50e44586e6ee" message="Decrypted Message #[payload]" />
		<set-payload value="#[payload]" doc:name="Set Payload To CSV file format" doc:id="dafb7bb8-d5c5-47c0-9267-a9ed50640034" mimeType='application/csv; quote="\"\\\"\""; separator=","; header=true'/>
		<ee:transform doc:name="Transform To JSON" doc:id="e603c655-2a06-471f-bf98-d8b581dee60f" >
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
			</ee:message>
		</ee:transform>	
</flow>
</mule>


Hope you find this article useful. Happy Coding!

CSV Data security

Opinions expressed by DZone contributors are their own.

Related

  • Locking Down the Enterprise: Data Security Patterns for AI Integrations
  • AI Is Finding Bugs Faster Than Enterprises Can Patch — Here's What Data Security Teams Should Do
  • Why Your DLP Policies Fall Short the Moment AI Agents Enter the Picture
  • AI in Patient Portals: From Digital Access to Intelligent Healthcare Experiences

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook