For simple encryption requirements, I used DPAPI through the ProtectedData class . To make the resulting encrypted value stored in a text file or registry, I encode the received byte array.
Here is the class I wrote to wrap this:
namespace SomeNamespace
{
using System;
using System.Security.Cryptography;
using System.Text;
public static class DataProtector
{
private const string EntropyValue = "secret";
public static string EncryptData(string stringToEncrypt)
{
byte[] encryptedData = ProtectedData.Protect(Encoding.Unicode.GetBytes(stringToEncrypt), Encoding.Unicode.GetBytes(EntropyValue), DataProtectionScope.LocalMachine);
return Convert.ToBase64String(encryptedData);
}
public static string DecryptData(string stringToDecrypt)
{
byte[] decryptedData = ProtectedData.Unprotect(Convert.FromBase64String(stringToDecrypt), Encoding.Unicode.GetBytes(EntropyValue), DataProtectionScope.LocalMachine);
return Encoding.Unicode.GetString(decryptedData);
}
}
}
source
share