amespace Sky.Decrypt
{
    using System;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Security.Cryptography;
    using System.Text;    public class Decryption
    {
        private string temporaryFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "license.dat");        public string DecryptFile(string path, string key)
        {
            string str = "";
            if (!File.Exists(path))
            {
                throw new Exception("File is not exist.");
            }
            byte[] rgbIV = new byte[] { 0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef };
            byte[] bytes = Encoding.UTF8.GetBytes(key);
            FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
            long length = stream.Length;
            DES des = new DESCryptoServiceProvider();
            CryptoStream stream2 = new CryptoStream(stream, des.CreateDecryptor(bytes, rgbIV), CryptoStreamMode.Read);
            StreamReader reader = new StreamReader(stream2);
            try
            {
                str = reader.ReadToEnd();
            }
            finally
            {
                stream.Close();
            }
            return str;
        }        public string DecryptString(string data, string key)
        {
            string str = "";
            if ((data == null) || (data == ""))
            {
                throw new Exception("Data is empty.");
            }
            byte[] rgbIV = new byte[] { 0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef };
            byte[] bytes = Encoding.UTF8.GetBytes(key);
            MemoryStream stream = new MemoryStream();
            DES des = new DESCryptoServiceProvider();
            CryptoStream stream2 = new CryptoStream(stream, des.CreateDecryptor(bytes, rgbIV), CryptoStreamMode.Write);
            try
            {
                byte[] buffer = Convert.FromBase64String(data);
                stream2.Write(buffer, 0, buffer.Length);
                stream2.FlushFinalBlock();
                str = Encoding.UTF8.GetString(stream.ToArray());
            }
            finally
            {
                stream2.Close();
                stream.Close();
            }
            return str;
        }        private string DeserializeFile(string path)
        {
            string str = "";
            if (!File.Exists(path))
            {
                throw new Exception("File is not exist.");
            }
            IFormatter formatter = new BinaryFormatter();
            FileStream serializationStream = new FileStream(path, FileMode.Open, FileAccess.Read);
            try
            {
                str = (string) formatter.Deserialize(serializationStream);
            }
            finally
            {
                serializationStream.Close();
            }
            return str;
        }        public string GetString(string path)
        {
            return this.DeserializeFile(path);
        }
    }
}