In C#, use the System.Security.Cryptography.ProtectedData class. For example, to encrypt a piece of data, use ProtectedData.Protect():
// Data to protect. Convert a string to a byte using Encoding.UTF8.GetBytes().
// Generate additional entropy (will be used as the Initialization vector)
byte entropy = new byte;
using(RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
byte ciphertext = ProtectedData.Protect(plaintext, entropy,
Store the entropy and ciphertext securely, such as in a file or
registry key with permissions set so only the current user can read it.
To get access to the original data, use ProtectedData.Unprotect():
byte plaintext= ProtectedData.Unprotect(ciphertext, entropy,
Note that there are additional security considerations. For example, avoid storing secrets like passwords as a string.
Strings are immutable, being they cannot be notified in memory so
someone looking at the application's memory or a memory dump may see the
password. Use SecureString or a byte instead and remember to dispose or zero them as soon as the password is no longer needed.