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 cryptoStream = new CryptoStream (
memoryStream,
cryptoProvider.CreateEncryptor (rgbKey, rgbIV),
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 cryptedString)
{
if (string.IsNullOrEmpty (cryptedString))
{
throw new ArgumentNullException (
"cryptedString",
"The string which needs to be decrypted can not be null.");
}
using (var cryptoProvider = new RijndaelManaged ())
using (var memoryStream = new MemoryStream (
Convert.FromBase64String (cryptedString)))
using (var cryptoStream = new CryptoStream (
memoryStream,
cryptoProvider.CreateDecryptor (rgbKey, rgbIV),
CryptoStreamMode.Read))
using (var reader = new StreamReader (cryptoStream))
{
return reader.ReadToEnd ();
}
}
private static byte [] rgbKey = ASCIIEncoding.ASCII.GetBytes ("Ni=9OE=$i+62eprIuDr@ewOu5I9r34Ro"); // change to your own secure key
private static byte [] rgbIV = ASCIIEncoding.ASCII.GetBytes ("to$eO_e!maI*o3ut"); // change to your own secure initialization vector