How we can store API keys encrypted inside .net core console application

Viewed 1317

I am working on a .NET core console application which integrates with 3rd party APIs. and to do the integration I need to pass the API keys inside the API requests. so my question is where/how I can store the API keys inside my console application? in regular .NET console application I use to store the API keys inside the app.config and encrypt the keys using Aspnet_regiis.. but not sure how i can do so inside .NET core console application?

3 Answers

You can consider...

  • Get separate keys issued for different environments (so that a compromised key can be expired/cancelled without affecting other environment/s).

  • Store keys encrypted - for example - in the operating system provided key store [https://docs.microsoft.com/en-us/dotnet/standard/security/cryptography-model]. (This just means that they are not "left lying around" for casual passers by.)

  • Remove the code for retrieval of keys from the console app, perhaps to a simple service or library to which code the regular developers do not have access. (A level of separation.)

  • To embed information within the application, consider resource files [https://docs.microsoft.com/en-us/dotnet/framework/resources/creating-resource-files-for-desktop-apps]

Bottom line: This question illustrates the raison d'ĂȘtre for public key (asymmetric) cryptography [https://en.wikipedia.org/wiki/Public-key_cryptography] but any app that handles a piece of information "decrypted in flow" makes that information vulnerable to everyone who has permission to work on (debug) the live app or poke around with diagnostic tools on the system. (If a person needs a key to open a door then you must give them a key with which to open it and can't then prevent dishonest misuse of that privilege.)

A little bit of paranoia is healthy but too much can be an obstacle to getting things done.

Security in Development

For local development, you can use the Secrets Manager Tool (used only in development, never in production). This tool allows you to store your sensitive data locally on your machine, outside the project tree.

This tool is not super secure, and the keys are not encrypted, but it provides an easy way to avoid storing secrets in your project config files and having to remember to add them to the source control ignore list.

Security in Production

A common way is to store secrets in an external vault.

If you are hosting your application on Azure, Azure also provides a more secure option: Azure Key Vault.

Key Vault is a cloud-hosted service for managing secrets, which will be accessible by the applications you authorize through an encrypted channel.

It is advisable not to store encrypted keys inside a .NET console application. It is more ideal to keep secrets separate, store them using the Secret management tool in dev mode, and look into services like Azure KeyVault for production as mentioned earlier.

See the link about app secrets and configuration and why we need a tool to manage it on https://dev.to/dotnet/how-to-store-app-secrets-for-your-asp-net-core-project-2j5b for detailed instructions on how to do this.

Related