June 8, 2012

Encrypt Text file

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections.Specialized;
using System.Security.Cryptography;

namespace Helpers
{
    public class EncryptionHelper
    {

        public static void EncryptTextToFile(string Data, string FileName, string key)
        {
            FileStream fStream = null;
            CryptoStream cStream = null;
            StreamWriter sWriter = null;

            try
            {
                fStream = File.Open(FileName, FileMode.Create);
                byte[] keyArray;

                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                hashmd5.Clear();

                TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
                tdes.Key = keyArray;
                tdes.Mode = CipherMode.ECB;
                tdes.Padding = PaddingMode.PKCS7;

                ICryptoTransform cTransform = tdes.CreateEncryptor();

                cStream = new CryptoStream(fStream, cTransform, CryptoStreamMode.Write);
                sWriter = new StreamWriter(cStream, UTF8Encoding.UTF8);
                sWriter.WriteLine(Data);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {

                sWriter.Close();
                fStream.Close();
                cStream.Close();
            }
        }


        public static string DecryptTextFromFile(String FileName, string key)
        {

            FileStream fStream = null;
            CryptoStream cStream = null;
            StreamReader sReader = null;
            string val = null;

            try
            {
                fStream = File.Open(FileName, FileMode.OpenOrCreate);
                byte[] keyArray;

                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                hashmd5.Clear();

                TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
                tdes.Key = keyArray;
                tdes.Mode = CipherMode.ECB;
                tdes.Padding = PaddingMode.PKCS7;

                ICryptoTransform cTransform = tdes.CreateDecryptor();
                cStream = new CryptoStream(fStream, cTransform, CryptoStreamMode.Read);
                sReader = new StreamReader(cStream, UTF8Encoding.UTF8);

                val = sReader.ReadToEnd();
            }
            catch (Exception ex)
            {
                fStream.Close();
                throw ex;
            }
            finally
            {
                if (sReader != null)
                    sReader.Close();
                if (cStream != null)
                    cStream.Close();
                if (fStream != null)
                    fStream.Close();
            }

            return val;
        }


    }
}