using System.Security.Cryptography;
static string _content = "test content 1 2 3";
static TripleDESCryptoServiceProvider _tdes = new TripleDESCryptoServiceProvider();
public static void Main()
string encrypted = encrypt(_content);
Console.WriteLine(encrypted);
string decrypted = decrypt(encrypted);
Console.WriteLine(decrypted);
public static string encrypt(string unencrypted)
MemoryStream ms = new MemoryStream();
CryptoStream cStream = new CryptoStream(ms,
_tdes.CreateEncryptor(_tdes.Key, _tdes.IV),
StreamWriter sWriter = new StreamWriter(cStream);
sWriter.WriteLine(unencrypted);
cStream.FlushFinalBlock();
string encrypted = Convert.ToBase64String(ms.ToArray());
catch (CryptographicException e)
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
catch (UnauthorizedAccessException e)
Console.WriteLine("A file access error occurred: {0}", e.Message);
public static string decrypt(string encrypted)
MemoryStream encryptedStream = new MemoryStream(Convert.FromBase64String(encrypted));
CryptoStream cStream = new CryptoStream(encryptedStream,
_tdes.CreateDecryptor(_tdes.Key, _tdes.IV),
StreamReader sReader = new StreamReader(cStream);
String val = sReader.ReadToEnd();
catch (CryptographicException e)
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
catch (UnauthorizedAccessException e)
Console.WriteLine("A file access error occurred: {0}", e.Message);