ArtisanCode / SimpleAesEncryption

MIT License
14 stars 5 forks source link

Multiple Keys? #2

Closed ceeSharpFan closed 7 years ago

ceeSharpFan commented 7 years ago

We are thinking of using this project but the one of our requirements is a different key for different clients. Looking over the getting started, i saw only one place in the config for a key.

Is it possible to have multiple keys? And determine which key to use at runtime?

Tom-Kuhn commented 7 years ago

This isn't a scenario that I considered when initially creating the library, however, it should be possible. You can use the following constructor to create client specific encryption objects.

public RijndaelMessageEncryptor(SimpleAesEncryptionConfiguration config)

As the encryption key is stored within this configuration object, this means that each client could have its own configuration independent of each other.

If you don't mind having duplicated configuration sections (one for each client) you could even try using the following constructor with the name of the client configuration section e.g.

var client1encryptor = new RijndaelMessageEncryptor("client1EncryptionSection");
var client2encryptor = new RijndaelMessageEncryptor("client2EncryptionSection");

With a configuration file like

<configSections>
    <section name="client1EncryptionSection" type="ArtisanCode.SimpleAesEncryption.SimpleAesEncryptionConfiguration, ArtisanCode.SimpleAesEncryption"/>
    <section name="client2EncryptionSection" type="ArtisanCode.SimpleAesEncryption.SimpleAesEncryptionConfiguration, ArtisanCode.SimpleAesEncryption"/>
</configSections>

<client1EncryptionSection>
    <EncryptionKey KeySize="256" Key="key1"/>
</client1EncryptionSection>

<client2EncryptionSection>
    <EncryptionKey KeySize="256" Key="key2"/>
</client2EncryptionSection>

Even with this approach you'll need to manage the creation of the client specific encryptor/decryptor. You should be able to manage this through your Dependency Injection framework or even manually if required.

I wish you luck with your project. Please let me know how it goes!