Mule Message Encryption-XML Encrypter

Introduction

In the post I have explained how to configure Anypoint Enterprise Security module and also shown how to use the JCE Encrypter. Please read the whole post before stepping into this post. It’s very important.

Objectives

The main objective of this post is to use the same flow we have used in the post. The flow is as follows. “We send some XML data to an HTTP endpoint. Then the XML data is encrypted using XML Encryption Strategy and we log the encrypted data. After that we simply decrypt the encrypted data and log it”. Here is diagram of the flow,

flow
Flow Diagram

Step 1

Now configure a global Encryption Element named as XML_Encrypter 

global-element
Global Element configuration.

Step 2

In the flow configure the Encrypt component,

encrypt-elm
Encrypt Element General Config.

In the Connector Configuration please select the global encryption element(XML_Encrypter) that we have defined in step 1. In the Operation dropdown select Encrypt. In the Input Reference field please put the payload. Now  select the encryption strategy as XML_ENCRYPTER. As we are using XML_ENCRYPTER in this example, let us configure it. Select the Define attributes radio button in the XML encrypter Strategy Configuration for operation category.

encrypt-element-xml-strategy-config
XML Strategy config.

Here we are encrypting only the name xml element using  Xpath.

Step 3

In the step 2 we have encrypted the message. Now let’s decrypt the encrypted message. Here we are going to configure the component labelled as Decrypt payload. 

decrypt-config
Decrypt General Config.
sasa
Decrypt XML Config.

The configuration is almost same as for encryption. The only difference is the

Operation: Decrypt

Testing

Now let us run the project. After running the project, send a POST request with XML payload. I am using Postman for this operation. On observing in the Anypoint console you can see the encrypted message as well as decrypted message.

test
Test Data

Sample result

INFO  2016-12-11 23:27:49,150 [[springbeanexample].HTTP_8081.worker.01] org.mule.api.processor.LoggerMessageProcessor: <?xml version="1.0" encoding="UTF-8"?><book>
	<name><xenc:EncryptedData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" Type="http://www.w3.org/2001/04/xmlenc#Content"><xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/><ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<xenc:EncryptedKey xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"><xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#kw-aes128"/><xenc:CipherData><xenc:CipherValue>w6kq/2uvqK/EX+Z9ROWKMCW+wRwLdehj</xenc:CipherValue></xenc:CipherData></xenc:EncryptedKey></ds:KeyInfo><xenc:CipherData><xenc:CipherValue>DIO/ZC/PrlpdBVnl2uzUTPDuGhXuO1tXF54qH0k70cE=</xenc:CipherValue></xenc:CipherData></xenc:EncryptedData></name>
	<author>Anupam</author>
</book>
INFO  2016-12-11 23:27:49,159 [[springbeanexample].HTTP_8081.worker.01] org.mule.api.processor.LoggerMessageProcessor: <?xml version="1.0" encoding="UTF-8"?><book>
	<name>ABC</name>
	<author>Anupam</author>
</book>

Please find the source code here.

N.B: Use mvn eclipse:eclipse to import the maven dependencies.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s