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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • FIPS 140-3: The Security Standard That Protects Our Federal Data
  • Enhancing Avro With Semantic Metadata Using Logical Types
  • Secure File Transfer as a Critical Component for AI Success
  • How Data Test Engineers Ensure Compliance and Security With Automation

Trending

  • AI, ML, and Data Science: Shaping the Future of Automation
  • Agile and Quality Engineering: A Holistic Perspective
  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  • Stateless vs Stateful Stream Processing With Kafka Streams and Apache Flink
  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 user avatar
Sapna Tambe
·
Dec. 27, 21 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
7.2K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • FIPS 140-3: The Security Standard That Protects Our Federal Data
  • Enhancing Avro With Semantic Metadata Using Logical Types
  • Secure File Transfer as a Critical Component for AI Success
  • How Data Test Engineers Ensure Compliance and Security With Automation

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!