This sample demonstrates how to use conventions to encrypt and decrypt specific properties of a message as it passes through the pipeline.
Running the solution starts two console applications. Endpoint1
encrypts a message and sends it and Endpoint2
receives the encrypted message and decrypts it.
Endpoint1 output
MessageWithSecretData sent.
Endpoint2 output
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
Message contract
The Shared
project contains MessageWithSecretData.
, which defines the message contract:
// Note all the properties to encrypt start with "Encrypted"
public class MessageWithSecretData
{
public string EncryptedSecret { get; set; }
public MySecretSubProperty SubProperty { get; set; }
public List<CreditCardDetails> CreditCards { get; set; }
}
public class MySecretSubProperty
{
public string EncryptedSecret { get; set; }
}
public class CreditCardDetails
{
public DateTime ValidTo { get; set; }
public string EncryptedNumber { get; set; }
}
Encryption configuration
Encryption is enabled by calling an extension method in Program.
in both Endpoint1
and Endpoint2
:
endpointConfiguration.ConfigurationEncryption();
The extension method is in Shared/
:
public static void ConfigurationEncryption(this EndpointConfiguration endpointConfiguration)
{
var encryptionService = new RijndaelEncryptionService(
encryptionKeyIdentifier: "2015-10",
key: Convert.FromBase64String("gdDbqRpqdRbTs3mhdZh9qCaDaxJXl+e6"));
endpointConfiguration.EnableMessagePropertyEncryption(encryptionService,
encryptedPropertyConvention: info =>
{
return info.Name.StartsWith("Encrypted");
});
}
The message on the wire
The serialized message content can be seen by running Endpoint1
without running Endpoint2
.
Messages are queued in the .
folder next to the solution. The message will be contained in a file in the Samples.
sub-folder with the following content:
<?xml version="1.0"?>
<MessageWithSecretData>
<EncryptedSecret>zoksP3QrtMqMmnXyShnvaLEq3n/6DA2f/7d6DDtwzXo=@u5THG1mtftg6+QAEsRh21g==</EncryptedSecret>
<SubProperty>
<EncryptedSecret>bmWpBtnYu0ira0Ke6+4YEQ==@zhLAqIx+qjwLFD1VGg78Bw==</EncryptedSecret>
</SubProperty>
<CreditCards>
<CreditCardDetails>
<ValidTo>2018-07-28T13:52:10.9062784Z</ValidTo>
<EncryptedNumber>FMApSVh9UEIYcE75VWvYUw==@7z6A1A/I/w5lACPbMwxoKg==</EncryptedNumber>
</CreditCardDetails>
<CreditCardDetails>
<ValidTo>2019-07-28T13:52:10.9072791Z</ValidTo>
<EncryptedNumber>KLWeyjogoNfZS1mblvcOMw==@St/nXNacedk5rW4GOwzg/A==</EncryptedNumber>
</CreditCardDetails>
</CreditCards>
</MessageWithSecretData>