PGP Encryption and Decryption using MuleSoft

Encryption is when information is converted into a secret code that hides information’s true meaning. Science of encrypting and decrypting information is called cryptography. In computing, unencrypted data is called plaintext, and encrypted data is called Ciphertext. Formulas used to encrypt, and decrypt are called encryption algorithms or Ciphers. Purpose of data encrypting is to protect the confidentiality of data shared across networks over the internet. API Integration security breaches can be minimized using the crypto module in Mule 4
Pretty Good Privacy (PGP) is an encryption system used for sending both encrypted files and emails. PGP shares some features with other encryption systems like Kerberos encryption (used to authenticate network users) and SSL encryption (used to secure websites).

We will be doing PGP encryption in MuleSoft using a PGP encrypt connector from Mule 4 crypto module.
First things first, generate new private and public key pairs using a tool named Kleopatra. Once the Kleopatra is installed, we can move on to the following steps.
1. Go to File > “New key Pair” and a dialogue box will open.

2. Enter the details, with Common-Name and email address fields as the required fields. In advanced settings, you can set the encryption algorithm. By default, it uses RSA and also you can select the key size whether it is 2048, 3082, 4096 bits. Note: We cannot use DSA for encryption since it is primarily used for digital signature and validation purposes

3. Add the passphrase for your key and click “OK” and a new key pair will be generated.

4. Once the key pair is created, you can export by clicking on the key and then “Export” and export the certificates to any location

5. Once you open the File, it should look like this.

6. Once the private and public key pair is exported, add the Crypto module from the exchange. Use Mule 4 PGP connector and set the config. Provide the file path for the public key for encryption in the PGP connector config and the fingerprint information. For decryption, provide a path for the private key.

7. Here is the snapshot of the application

8. We will be encrypting a json data and then decryption it again. Here is the sample input for:
{
"firstName":"John",
"lastName":"Doe",
"dateOfBirth":"28/05/1994",
"employmentHistory":{
"companyName":"Amazon",
"location":"Hyderabad"
}
}
9. After running and encrypting the data, the encrypted output looks like this:
-----BEGIN PGP MESSAGE-----
Version: BCPG v1.61
hQEMA8pVfKM99DLIAQgAwf7sFW/fTt0sBbJ0COiqxswiJ9F1v1G3RelBBBCGshYt
YdS+l5chfm4gQd176u5u7kVqS/+GSn13fgfHJNnaSfqEmvEA99YdwsX0ucSgTrE7
Pklq3wCf0WlOY6qpk+rh+/754nysG672yXizYQNelgVbCjtf07rEH5jMkJqTj456
7sHzETA8nKhwLF10HDSGdrA8s/+gITeCj02UV3XNW4g+wAfSdJOsa8hFThZWiajY
OyZbK2rUWzzBaE0kj3dddbMBuD+zFfkxo+f+NiyeeZOAP1alps0PTmvApvKb1KV9
kkADWJ1NeE3WqGa6syII3kSRCX/40rHmGxHGN6UO3tK3ATm+KOGM/Pi+gTkWWNs5
0LHmJFw0RTvsAVWNqB6z8GD2fCen94GA1ijNd2jP7olZEMzQMbYJZ1DpdKl9IF7X
48IWg89yD+JSJjrkxbr1WAq/6RooApo2blhOeeY5lS2K33SX8Hm8UkjqyvFEUjaF
ACPLliGyLRW6aHTkuzNlWZg9zFy1X89NR9+/gDwyeJH6VK+4L3Z+hse6XP9m5QuG
jdCIaIlX9LUiGcvnPeuNNHwlM7Y2qOga
=vY2Y
-----END PGP MESSAGE-----
10. Application logs look like this

PGP Encryption adds a security layer that makes sure that the data being transmitted via various channels is secured and can only be accessed by the person with the key that can decrypt the data. Using Mule 4 crypto module, a MuleSoft developer we can achieve different functionalities like PGP encryption-decryption, JCE encryption, calculating checksum and more.
Recent Blogs

AI-Driven PDF Parsing in Salesforce
Introduction For the current digital ecosystem, data is an important aspect for decision-making. Yet, for many organizations, a significant portion of this valuable data remains locked away in unstructured formats. Organizations handle thousands of PDF documents daily — ranging from contracts and invoices to lab reports, quotations, and service agreements. Traditionally, extracting structured data from… Continue reading AI-Driven PDF Parsing in Salesforce
AI-Driven PDF Parsing in Salesforce
Introduction For the current digital ecosystem, data is an important aspect for decision-making. Yet, for many organizations, a significant portion of this valuable data remains locked away in unstructured formats. Organizations handle thousands of PDF documents daily — ranging from contracts and invoices to lab reports, quotations, and service agreements. Traditionally, extracting structured data from… Continue reading AI-Driven PDF Parsing in Salesforce

Compression Namespace in Apex: A Powerful New Salesforce Feature
Introduction Working with documents inside Salesforce has always challenged developers because of the platform’s multitenant constraints. Previously, packaging and sending files in a compact form required external services, like an AWS Lambda function, that retrieved files via API and then compressed them. With the introduction of the Compression Namespace and the powerful pre-defined Apex functions,… Continue reading Compression Namespace in Apex: A Powerful New Salesforce Feature
Compression Namespace in Apex: A Powerful New Salesforce Feature
Introduction Working with documents inside Salesforce has always challenged developers because of the platform’s multitenant constraints. Previously, packaging and sending files in a compact form required external services, like an AWS Lambda function, that retrieved files via API and then compressed them. With the introduction of the Compression Namespace and the powerful pre-defined Apex functions,… Continue reading Compression Namespace in Apex: A Powerful New Salesforce Feature

Boost LWC Performance with Debouncing
Introduction Lightning Web Components (LWC) is a modern framework for building fast and dynamic user interfaces on the Salesforce platform. However, one common challenge in web development, including LWC, is efficiently handling user input, especially when dealing with rapid or repetitive events, such as typing in a search field. This is where debouncing becomes an… Continue reading Boost LWC Performance with Debouncing
Boost LWC Performance with Debouncing
Introduction Lightning Web Components (LWC) is a modern framework for building fast and dynamic user interfaces on the Salesforce platform. However, one common challenge in web development, including LWC, is efficiently handling user input, especially when dealing with rapid or repetitive events, such as typing in a search field. This is where debouncing becomes an… Continue reading Boost LWC Performance with Debouncing

Salesforce Pricing Automation: Boost Efficiency And Accuracy with Apex Triggers
Introduction In order to succeed in today’s fast-paced business landscape, precision and speed define competitive advantage. For businesses, especially those managing complex product catalogs, ensuring accurate pricing on sales orders or custom lines can be a time-consuming and error-prone task. To overcome this challenge, Salesforce trigger handlers offer a powerful solution to automate the entire… Continue reading Salesforce Pricing Automation: Boost Efficiency And Accuracy with Apex Triggers
Salesforce Pricing Automation: Boost Efficiency And Accuracy with Apex Triggers
Introduction In order to succeed in today’s fast-paced business landscape, precision and speed define competitive advantage. For businesses, especially those managing complex product catalogs, ensuring accurate pricing on sales orders or custom lines can be a time-consuming and error-prone task. To overcome this challenge, Salesforce trigger handlers offer a powerful solution to automate the entire… Continue reading Salesforce Pricing Automation: Boost Efficiency And Accuracy with Apex Triggers