Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you share encrypted data between two different .NET Core Apps?

I have an ASP.NET Core app and a .NET Core command line app that need to share the same database entries. I would like to encrypt one of the columns but still be able to have the two apps read the entries. I have been looking into the DataProtection apis for encryption but can figure out how to decrypt between the two app. Any suggestions?

In both apps I have tried configuring the DataProtector with the same purpose:

_protector = provider.CreateProtector("TestEncryption");

However, when I attempt to decrypt I wind up with the following exception:
CryptographicException: The payload was invalid.

like image 306
ProgrammingPope Avatar asked Jun 08 '26 20:06

ProgrammingPope


1 Answers

Share data protection keys between applications. You can store it in local filesystem, in Azure Storage or Redis caches, or use another third-party distributed storage. For example:

public void ConfigureServices(IServiceCollection services)
{
    // Connect to Redis database.
    var redis = ConnectionMultiplexer.Connect("<URI>");
    services.AddDataProtection()
        .PersistKeysToRedis(redis, "DataProtection-Keys");

    services.AddMvc();
}

Documentation: Key storage providers.

like image 190
Ilya Chumakov Avatar answered Jun 10 '26 09:06

Ilya Chumakov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!