Message Property Encryption

Component: Message Property Encryption | Nuget: NServiceBus (Version: 5.x)

Run the solution.

Two console applications will start up.

Endpoint1

Which outputs

MessageWithSecretData sent.

Endpoint2

Which outputs

I know the secret - it's 'betcha can't guess my secret'
SubSecret: My sub secret
CreditCard: 312312312312312 is valid to 3/11/2015 5:21:59 AM
CreditCard: 543645546546456 is valid to 3/11/2016 5:21:59 AM

Code walk-through

The message contract

Starting with the Shared project, open the MessageWithSecretData.cs file and look at the following code:

Edit
using NServiceBus;

public class MessageWithSecretData :
    IMessage
{
    public WireEncryptedString Secret { get; set; }
    public MySecretSubProperty SubProperty { get; set; }
    public List<CreditCardDetails> CreditCards { get; set; }
}

public class MySecretSubProperty
{
    public WireEncryptedString Secret { get; set; }
}

public class CreditCardDetails
{
    public DateTime ValidTo { get; set; }
    public WireEncryptedString Number { get; set; }
}

How is encryption configured.

Open either one of the Program.cs. Notice the line

Edit
busConfiguration.ConfigurationEncryption();

This code indicates that encryption should be enabled.

The key is then configured using

Edit
using NServiceBus;

public static class EncryptionExtensions
{
    public static void ConfigurationEncryption(this BusConfiguration busConfiguration)
    {
        var encryptionKey = Convert.FromBase64String("gdDbqRpqdRbTs3mhdZh9qCaDaxJXl+e6");
        busConfiguration.RijndaelEncryptionService("2015-10", encryptionKey);
    }
}

The message on the wire

Now run Endpoint1 on its own (i.e. don't start Endpoint2).

Go to the server queue (called EncryptionSampleEndpoint1) and view the message content.

The message will look like this:

"MessageWithSecretData": {
  "Secret": {
    "EncryptedValue": {
      "EncryptedBase64Value": "+eeBont5Lzlre4cxDi8QT/M6EbAGxTerniqywbpLBVA=",
      "Base64Iv": "u8n8ds0Ssf/AdJCxpOG7AQ=="
  }
}

Last modified