using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;

namespace CarGallery
{
    public static class Crypto
    {
        public static string Encrypt(string toEncrypt, String key)
        {
            try
            {
                TripleDESCryptoServiceProvider objDESCrypto = new TripleDESCryptoServiceProvider();
                MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider();
                byte[] byteBuff;

                objDESCrypto.Key = objHashMD5.ComputeHash(UTF32Encoding.Default.GetBytes(key));
                objHashMD5 = null;
                objDESCrypto.Mode = CipherMode.ECB; //CBC, CFB
                byteBuff = UTF32Encoding.Default.GetBytes(toEncrypt);
                return Convert.ToBase64String(objDESCrypto.CreateEncryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
            }
            catch (Exception ex)
            {
                return "Wrong Input. " + ex.Message;
            }
        }

        public static string Decrypt(string encrypted, String key)
        {
            try
            {
                TripleDESCryptoServiceProvider objDESCrypto = new TripleDESCryptoServiceProvider();
                MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider();
                byte[] byteBuff;

                objDESCrypto.Key = objHashMD5.ComputeHash(UTF32Encoding.Default.GetBytes(key));
                objHashMD5 = null;
                objDESCrypto.Mode = CipherMode.ECB; //CBC, CFB
                byteBuff = Convert.FromBase64String(encrypted);
                string strDecrypted = UTF32Encoding.Default.GetString(objDESCrypto.CreateDecryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
                objDESCrypto = null;
                return strDecrypted;
            }
            catch (Exception ex)
            {
                return "Wrong Input. " + ex.Message;
            }
        }
    }
}