How to securely store and retrieve encrypted values (like passwords or API keys) using C#.
Loading
How to securely store and retrieve encrypted values (like passwords or API keys) using C#.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Divyansh GuptaPosted May 21, 2025, 6:41 AM
To securely store and retrieve sensitive data like passwords or API keys in an ASP.NET app, you can use AES (Advanced Encryption Standard), a strong built-in encryption algorithm in .NET.
Here's a simple helper class I use for encrypting and decrypting strings:
Example Usage:
Important Notes:
Don’t hardcode the key like this in real applications: store it in environment variables, Azure Key Vault, or user secrets.
The above example uses a randomly generated IV (initialization vector) for better security, stored at the start of the encrypted text.
AES is symmetric encryption: the same key is used to encrypt and decrypt.