How to Encrypt Data Using PKCS1 Padding in Mule 4

Sudheerkumar
Another Integration Blog
4 min readAug 24, 2022

What is PKCS #1?

PKCS stands for Public-Key Cryptography Standards. In cryptography, PKCS #1 is the first cryptography standard. It provides the basic definitions and implements the RSA algorithm for public-key cryptography. PKCS #1 does not work on files, it works completely on data.

How Does PKCS Padding Work?

Padding bytes are always added to the clear text before it is encrypted. Each padding byte has a value equal to the total number of padding bytes that are added. For example, if 6 padding bytes must be added, each of those bytes will have the value 0x06. The total number of padding bytes is at least one and is the number that is required in order to bring the data length up to a multiple of the cypher algorithm block size.

Please see this link for more information on the PKCS padding method.

Prerequisites

  • Download Crypto Module from the Exchange
  • Generate JKS file using KeyStore explorer

Installation:

Step-1:

To use the Crypto Module, first download the module from the Exchange. Click on Search in Exchange -> Search for Cryptography Module -> Select Add >. Then click Finish.

The Crypto module exists in the Mule palette.

Step-2:

Download KeyStore Explorer from the browser. Now, we generate the JKS file.

Create a new KeyStore with the type JKS.

Generate a key pair and select the algorithm.

Generate key pair certificate.

Enter the key pair alias name.

Enter the key pair password.

Key pair generated successfully.

Demo:

Encrypt Data Using PKCS1 Padding in the JCE Cryptography Module

Step-1:

Configure the “Jce encrypt” component first to encrypt the data using PKCS1 padding.

Follow the steps below to configure the KeyStore as global.

Step-2:

Refer to the KeyStore configuration using Keyid in the JCE component, as highlighted below.

Step-3:

Now, write the following “RSA/ECB/PKCS1Padding” in the algorithm selection to encrypt the payload using the PKCS#1.

Note: if we write the cipher, we should not select the algorithm in the selection. We should select any one from the algorithm selection.

Step-4:

The figure below represents the flow to encrypt the payload using PKCS1 in the JCE encrypt component.

Step-5:

The figure below shows what you will see if the application is successfully deployed.

Step-6:

Below are the steps to test the flow.

Input:

Payload before encryption.

Output:

Payload after encryption.

Now, converting octet-stream encrypted data to Base64.

Conclusion:

In this article, we learned how to encrypt data using PKCS1 padding in Mule 4.

We also learned how to generate the keypair (certificate) using KeyStore explorer.

Thanks for reading my post and I hope it will be helpful.

That’s it for now! See you in the next post when I decrypt data using the same certificate and algorithm!

-Sudheer Kumar N

--

--