Вот быстрое шифрование / дешифрование строковых данных с использованием AES (Rijndael):
private static readonly byte[] rgbKey = Encoding.UTF8.GetBytes("Ni=9OE=$i+62eprIuDr@ewOu5I9r34Ro"); // change to your own secure key
private static readonly byte[] rgbIv = Encoding.UTF8.GetBytes("to$eO_e!maI*o3ut"); // change to your own secure initialization vector
public static string Encrypt(string originalString)
{
if (string.IsNullOrEmpty(originalString))
{
throw new ArgumentNullException(
"originalString",
"The string which needs to be encrypted can not be null.");
}
using (var cryptoProvider = new RijndaelManaged())
using (var memoryStream = new MemoryStream())
using (var encryptor = cryptoProvider.CreateEncryptor(rgbKey, rgbIv))
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
using (var writer = new StreamWriter(cryptoStream))
{
writer.Write(originalString);
writer.Flush();
cryptoStream.FlushFinalBlock();
writer.Flush();
return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
}
}
public static string Decrypt(string encryptedString)
{
if (string.IsNullOrEmpty(encryptedString))
{
throw new ArgumentNullException(
"encryptedString",
"The string which needs to be decrypted can not be null.");
}
using (var cryptoProvider = new RijndaelManaged())
using (var memoryStream = new MemoryStream(Convert.FromBase64String(encryptedString)))
using (var decryptor = cryptoProvider.CreateDecryptor(rgbKey, rgbIv))
using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
using (var reader = new StreamReader(cryptoStream))
{
return reader.ReadToEnd();
}
}