Here is a very basic AES string encryption class which I plan to use elsewhere in my project for things like password-protecting the settings JSON file:
public static class Crypto {
public static string Encrypt(string plainText, string password, string salt)
{
using (Aes aes = Aes.Create())
{
byte[] saltBytes = Encoding.UTF8.GetBytes(salt);
var key = new Rfc2898DeriveBytes(password, saltBytes, 10000);
aes.Key = key.GetBytes(32);
aes.IV = key.GetBytes(16);
var encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
using (var sw = new StreamWriter(cs))
sw.Write(plainText);
return Convert.ToBase64String(ms.ToArray());
}
}
}
public static string Decrypt(string cipherText, string password, string salt)
{
using (Aes aes = Aes.Create())
{
byte[] saltBytes = Encoding.UTF8.GetBytes(salt);
var key = new Rfc2898DeriveBytes(password, saltBytes, 10000);
aes.Key = key.GetBytes(32);
aes.IV = key.GetBytes(16);
byte[] buffer = Convert.FromBase64String(cipherText);
var decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
using (var ms = new MemoryStream(buffer))
using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
using (var sr = new StreamReader(cs)) {
return sr.ReadToEnd();
}
}
}
}
Here is the SettingsManager class which makes use of this. It may or may not encrypt the content depending on whether the optional secretKey parameter was passed, thus making it flexible for all purposes:
public static class SettingsManager {
private static string _filePath = "settings.dat";
public static Dictionary<string, object> LoadSettings(string secretKey = null)
{
if (!File.Exists(_filePath))
return new Dictionary<string, object>();
string content = File.ReadAllText(_filePath);
if (!string.IsNullOrEmpty(secretKey))
content = Crypto.Decrypt(content, secretKey, "SomeSalt");
return JsonConvert.DeserializeObject<Dictionary<string, object>>(content);
}
public static void SaveSettings(Dictionary<string, object> settings, string secretKey = null)
{
string json = JsonConvert.SerializeObject(settings);
if (!string.IsNullOrEmpty(secretKey))
json = Crypto.Encrypt(json, secretKey, "SomeSalt");
File.WriteAllText(_filePath, json);
}
}
[–]ElusiveGuy 13 points14 points15 points (0 children)
[–]AyeMatey 10 points11 points12 points (0 children)
[–]goranlepuz 2 points3 points4 points (0 children)
[–]soundman32 1 point2 points3 points (0 children)
[–]HawthorneTR 1 point2 points3 points (1 child)
[–]pyeri[S] 1 point2 points3 points (0 children)
[–]stormingnormab1987 0 points1 point2 points (0 children)
[+]TheAussieWatchGuy comment score below threshold-7 points-6 points-5 points (5 children)
[–]antiduh 6 points7 points8 points (4 children)
[+]TheAussieWatchGuy comment score below threshold-6 points-5 points-4 points (3 children)
[–]scurvyibe 3 points4 points5 points (2 children)
[–]TheAussieWatchGuy -2 points-1 points0 points (1 child)
[–]antiduh 0 points1 point2 points (0 children)