Skip to content

Message compression and encryption

Mogens Heller Grabe edited this page Jun 9, 2020 · 5 revisions

Serialization

By default, Rebus uses the built-in JSON serializer which internally uses the excellent Newtonsoft JSON.NET.

Message encryption

Rebus supports automatic encryption of message bodies, which means that the contents of messages cannot easily be read by eavesdroppers. This can be useful if you're e.g. sending messages containing payroll information, or other kinds of information that should not be readily readable.

You can enable encryption by going:

Configure.With(...)
    .(...)
    .Options(o => {
        o.EnableEncryption("base 64 encoded encryption key");
    })
    .(...)

In case you forget to provide a valid key, Rebus is nice enough to generate one for you that you may copy into your configuration file and start using :) Rebus will used the built-in functionality in .NET to generate the key.

Message compression

Since many serialization formats are pretty verbose, and JSON in that regard is probabably one of the most verbose formats because it includes the entire schema along with every single message, you may want to enable message compression.

In order to enable compression, you can do this:

Configure.With(...)
    .(...)
    .Options(o => {
        o.EnableCompression(512);
    })
    .(...)

in order to compress messages whose raw byte[] payload exceeds 512 bytes in length, or

Configure.With(...)
    .(...)
    .Options(o => {
        o.EnableCompression();
    })
    .(...)

in order to use the default threshold of 1024 bytes.

Encryption and compression combined

You can, of course, combine both encryption and compression, by going

Configure.With(...)
    .(...)
    .Options(o => {
        o.EnableEncryption(key);
        o.EnableCompression();
    })
    .(...)

Since encryption scrambles messages badly, Rebus will of course compress messages before they're encrypted when sending, and in reverse upon receiving them, no matter how you configure it.

JSON Encryption

The awesome Simon Cropp has provided a Rebus-compatible version of his excellent JSON encryption library, which allows for selectively encrypting nodes of JSON-serialized message objects, e.g. by attaching an [Encrypt] attribute to a property.

The library speaks for itself, so please visit the code repository, or go download the NuGet package.

Clone this wiki locally