268 Comments

In some cases you may want to tighten up your NServiceBus security by encrypting some parts of your message. This may be required if your client application sends some sensitive data to the server. By following this tutorial you learn how to use the built-in encryption features of the NServiceBus.

We’re not going to build everything from the scratch. Instead, we’re using our previous ASP.NET MVC application as the starting point.

1. Modifying the message

We start by modifying our HelloWorldMessage. Until this point, the message hasn’t contained any data so let’s change this now. NServiceBus contains a class called “WireEncryptedString” which we can use when transporting sensitive string-based data from our client to server. Using the class is as easy as using a regular string. Modify our message class by adding a new member to it:

   1: public class HelloWorldMessage : IMessage
   2: {
   3:     public WireEncryptedString SecretMessage;
   4: }

 

That’s all we have to change in our message so it’s time to modify our client.

2. Modifying the client

As you remember, we create our message in the HomeController. Open it up and modify the SendHello-method so that our new SecretMessage-field is filled with information:

   1: public ActionResult SendHello()
   2: {
   3:     var message = new HelloWorldMessage() { SecretMessage = "Encrypt this" };
   4:
   5:     MvcApplication.Bus.Send(message);
   6:
   7:     return View("Index");

Now open up the client’s web.config and add a new configSection:

<section name="RijndaelEncryptionServiceConfig" type="NServiceBus.Config.RijndaelEncryptionServiceConfig, NServiceBus.Core"/>

And then add the actual configuration just before the </configuration> tag:

<RijndaelEncryptionServiceConfig Key="gdDbqRpqdRbTs3mhdZh9qCaDaxJXl+e7"/>
Only one thing left in our client. You must change the NServiceBus initialization so that the RijndaelEncryptionService is used. After the change, the full configuration in Global.asax.cs looks like this:
   1: Bus = Configure.WithWeb()
   2:     .Log4Net()
   3:     .DefaultBuilder()
   4:     .XmlSerializer()
   5:     .RijndaelEncryptionService()
   6:     .MsmqTransport()
   7:         .IsTransactional(false)
   8:         .PurgeOnStartup(false)
   9:     .UnicastBus()
  10:         //.ImpersonateSender(false)
  11:     .CreateBus()
  12:     .Start();

Before we modify our server, start up the client and hit the “Send hello” few times. Now, fire up the MSMQ monitoring tool of which we learned last time and browse to the Message Queuing – Private Queues – inputqueue – Queue messages. Here you can see listed all the messages in our queue. Double click on one of them and a window with information about your message opens. Select the Body-tab and see what your HelloWorldMessage looks like:

encrypted

As you can see, our SecretMessage has been encrypted. For comparison, here’s a screenshot of the SecretMessage if we were using a regular string:

plain

The last thing to do is to modify our server so that it can read our encrypted message. Let’s do that next.

4. Modifying the server

Our server’s message handler, initialization and configuration must be changed so that it can process the secret message. In message handler, start by modifying our current implementation so that it prints out the secret message to the console:

   1: public void Handle(HelloWorldMessage message)
   2: {
   3:
   4:     Console.WriteLine("Hello world from the client!");
   5:
   6:     var principal = System.Threading.Thread.CurrentPrincipal;
   7:
   8:     var userName = principal.Identity.Name;
   9:     Console.WriteLine("User: {0}", userName);
  10:     Console.WriteLine("Secret message: {0}", message.SecretMessage.Value);
  11: }
Notice the Value-property of the SecretMessage-field.

Now start your server and see how it processes the messages:

encrypted2

As you can see, the encrypted message isn’t yet readable. Open up the Class1, which contains our server initialization. We need to tell our server to use the encryptions by calling the Configure.Instance.RijndaelEncryptionService-method. But, because of some unknown reason, if we just add that one line into our Init-method, the server will crash in startup because of an “Object reference not set”. So instead, modify the Class1-class to look like this snippet:

   1: public class EndpointConfig : IConfigureThisEndpoint, AsA_Server { }
   2: public class ServerInit : IWantCustomInitialization, IWantCustomLogging
   3: {
   4:     public void Init()
   5:     {
   6:         Configure.Instance.RijndaelEncryptionService();
   7:         NServiceBus.SetLoggingLibrary.Log4Net(log4net.Config.XmlConfigurator.Configure);
   8:     }
   9: }

Now we have taken care of our server’s initialization so there’s only one last step left: modifying the server configuration. We need to add the exact same configurations as we did when modifying the web.config. Go ahead and copy-paste the RijndaelEncryptionServiceConfig-configurations from the client and you’re all set. It’s time to start your application and see the difference.

decrypted

Excellent!

Conclusion

When sending sensitive information between your client and server, it may be best to encrypt some parts of the message. NServiceBus contains a built-in “WireEncryptedString”- and “RijndaelEncryptionService”-classes which make the process very simple.

Comments

Comment by little

I see this key everywhere gdDbqRpqdRbTs3mhdZh9qCaDaxJXl+e7


how to change the key to a new one?

little
Comment by mcm bag

mcm bag...

nike air max 90 boot...

Comment by mcm backpacks

mcm backpacks...

nike air men shoes...

Comment by black nike air max

black nike air max...

white nike air max for women...

Comment by mcm uk online

mcm uk online...

nike air max colors...

Comment by black nike air

black nike air...

cheap nike air max sales...

Comment by new nike

new nike...

cheap nike air max trainer...

Comment by nike women shoes

nike women shoes...

customize shoes nike id...

Comment by buy nike air max

buy nike air max...

nike outlet store san leandro...

Comment by mcm online shop

mcm online shop...

nike free run shoes 5.0...

Comment by cheap mcm

cheap mcm...

buy nike shoes for cheap...

Comment by mcm store

mcm store...

nike free 5.0 sneakers...

Comment by nike women shoes

nike women shoes...

nike air max buy online cheap...

Comment by mcm clutch

mcm clutch...

nike free 7...

Comment by mcm online shop

mcm online shop...

tiffany blue nike free run 3...

Comment by mcm uk bag

mcm uk bag...

nike shoes nike store...

Comment by vintage mcm bags

vintage mcm bags...

nike free running shoes review...

Comment by mcm store korea

mcm store korea...

mens nike air max sale...

Comment by mcm bags

mcm bags...

mcm electronics canada...

Comment by vintage mcm bag

vintage mcm bag...

nike air max 2014 running shoe...

Comment by shop nike

shop nike...

white mcm backpack...

Comment by nike airmax

nike airmax...

nike air max london underground...

Comment by handbags mcm

handbags mcm...

nike free golf...

Comment by nikebags

nikebags...

cheap nike free shoes...

Comment by authentic mcm bags

authentic mcm bags...

buy cheap nike sneakers online...

Comment by white nike shoes

white nike shoes...

nike air max 90 current moire...

Comment by shop nike

shop nike...

nike free runs 3.0 women...

Comment by mcm backpack size

mcm backpack size...

cheap nike basketball shoes...

Comment by mcm review

mcm review...

nike zoom hyperfuse...

Comment by nikewhiteshoes

nikewhiteshoes...

2009 nike air max...

Comment by mcmsunglasses

mcmsunglasses...

womens nike free run cheap...

Comment by mcm on sale

mcm on sale...

nike shox 2011...

Comment by mcm hats

mcm hats...

nike air max run lite 4 lightweight running shoe...

Comment by october mcm

october mcm...

nike id air force 1...

Comment by nike free run3.0

nike free run3.0...

nike air max light grey...

Comment by mcmbriefcase

mcmbriefcase...

nike shox r3...

Comment by mcm online shop usa

mcm online shop usa...

cheapest nike air max online...

Comment by nike air max wiki

nike air max wiki...

really cheap nike shoes...

Comment by mcm women bag

mcm women bag...

nike air max 2010 tailwind...

Comment by cheap mcm bags uk

cheap mcm bags uk...

cheap nike air max for men...

Comment by buy mcm

buy mcm...

nike shoe sneakers...

Comment by mcm wallet uk

mcm wallet uk...

china cheap nike air max...

Comment by mcm elec

mcm elec...

get cheap nike shoes...

Comment by mcm bag for sale

mcm bag for sale...

nike free run premium...

Comment by mcm uk bag

mcm uk bag...

where can i find cheap nike clothes...

Comment by nike run free 3

nike run free 3...

nike free run model...

Comment by mcm shopping

mcm shopping...

nike classics...

Comment by mcm bags outlet

mcm bags outlet...

nike outlet johnson creek...

Comment by mcm online shop

mcm online shop...

nike nike free run 3...

Comment by nike air max a

nike air max a...

nike jordan outlet sale...

Comment by nike free 4 v3

nike free 4 v3...

nike 2014 air max...

Comment by mcm boutique

mcm boutique...

nike outlet medford...

Comment by mcm online store

mcm online store...

nike air max basketball shoes...

Comment by nike id cn

nike id cn...

mcm wholesale...

Comment by mcm bag men

mcm bag men...

nike trainers shoes...

Comment by mcm reviews

mcm reviews...

nike air max men black...

Comment by nike america

nike america...

nike footwear for women...

Comment by mcm sunglasses

mcm sunglasses...

create your own basketball shoes nike...

Comment by nike flyknit max

nike flyknit max...

cheap nike air max 90 shoes...

Comment by mcm store

mcm store...

cheap nike air max 90 shoes...

Comment by mcm electronics inc

mcm electronics inc...

cheap nike sneakers free shipping...

Comment by nike hats

nike hats...

nike warehouse sale...

Comment by mcm london comic

mcm london comic...

nike free run womens shoes...

Comment by mcm leather bags

mcm leather bags...

nike free womens sneakers...

Comment by nike headbands

nike headbands...

mcm first lady...

Comment by authentic mcm bags

authentic mcm bags...

cheap nike shoes free shipping...

Comment by mcm bags

mcm bags...

nike air max sneakers...

Comment by Nike Air Max

Nike Air Max...

nike free run runners...

Comment by mcm bags 2014

mcm bags 2014...

nike free run white...

Comment by mcm mens wallet

mcm mens wallet...

nike max air shoes...

Comment by nike id twitter

nike id twitter...

womens tiffany blue nike free runs...

Comment by backpack mcm

backpack mcm...

white nike air max mens...

Comment by mcm clutch bags

mcm clutch bags...

womens nike flyknit air max...

Comment by Tall Ugg

Tall Ugg...

nike shoes outlet store...

Comment by Ugg Autumn

Ugg Autumn...

nike free run 5.0 pink and grey...

Comment by Ugg Size Chart

Ugg Size Chart...

nike shoes in discount...

Comment by Www.ugg

Www.ugg...

nike free womens 4.0...

Comment by Ugg Kids Bobbey

Ugg Kids Bobbey...

where to find cheap nike shoes...

Comment by Kids Ugg Cardy

Kids Ugg Cardy...

nike air max 2009 kids...

Comment by Ugg For Men

Ugg For Men...

nike air max basketball...

Comment by Ebay Ugg Boots

Ebay Ugg Boots...

cheap free nike...

Comment by Ugg Blue Boots

Ugg Blue Boots...

nike air max 90 for men...

Comment by Ugg Online Sale

Ugg Online Sale...

cheap jordan and nike shoes...

Comment by mcm bag online

mcm bag online...

nike free run 3.0 online...

Comment by Ugg Coupons

Ugg Coupons...

mcm clothes...

Comment by Cute Ugg Boots

Cute Ugg Boots...

nike air max 2013 sale usa...

Comment by Ugg Men Boots

Ugg Men Boots...

nike 2013 air max...

Comment by Online Ugg Boots

Online Ugg Boots...

nike air max cheap 2013...

Comment by mcm luxury

mcm luxury...

discount nike free shoes...

Comment by Ugg Repair

Ugg Repair...

kids nike free run 4...

Comment by Ugg Broome

Ugg Broome...

really cheap nike air max 90...

Comment by Classic Ugg Tall

Classic Ugg Tall...

nike free run 3 blue silver...

Comment by Ebay Ugg Boots

Ebay Ugg Boots...

nike air max womens 2014...

Comment by Classic Mini Ugg

Classic Mini Ugg...

nike free womens 5.0...

Comment by Cheap Ugg Boot

Cheap Ugg Boot...

what are nike air max shoes for...

Comment by Ugg Men Boot

Ugg Men Boot...

nike air max shoes on sale...

Comment by Ugg Chestnut

Ugg Chestnut...

new mens nike air max...

Comment by mcm bag

mcm bag...

nike free 5.0 womens on sale...

Comment by Ugg Boots Caspia

Ugg Boots Caspia...

very cheap nike air max 90...

Comment by Ugg Black Friday

Ugg Black Friday...

nike free run 4.0 womens...

Comment by mcm elect

mcm elect...

mcm bags for women...

Comment by Kids Ugg Dakota

Kids Ugg Dakota...

nike discount shoes...

Comment by mcm shop

mcm shop...

nike air max first release...

Comment by nike runners

nike runners...

2013 women nike air max...

Comment by mcm outlet

mcm outlet...

tiffany nike running shoes...

Comment by Ugg Brown Boots

Ugg Brown Boots...

where can you buy nike air max...