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

namespace ASHermed.SinoInterface.Tools
{
    /// <summary>
    /// key:437d1ccaa370a1fc86650aef91195bf4
    /// </summary>
    public static class DES
    {
        /// <summary>
        /// DES加密密钥
        /// </summary>
        public static string DESKey = "437d1ccaa370a1fc86650aef91195bf4";
        /// <summary>
        /// 加密解密时作为唯一标识区分该数据是否加密过    hashkey:加密后的字符串    
        /// </summary>
        public static string HashKey = "2a0af7fef216d66199096a7316beb56979d2d214";
        ///<summary><![CDATA[字符串DES加密函数]]></summary>  
        ///<param name="str"><![CDATA[被加密字符串 ]]></param>  
        ///<param name="key"><![CDATA[密钥 ]]></param>   
        ///<returns><![CDATA[加密后字符串]]></returns>     
        ///加密
        public static string EncodeDES(string str, string key)
        {
            if (string.IsNullOrEmpty(str))
                return "";
            try
            {
                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
                provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
                byte[] bytes = Encoding.GetEncoding("UTF-8").GetBytes(str);
                MemoryStream stream = new MemoryStream();
                CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
                stream2.Write(bytes, 0, bytes.Length);
                stream2.FlushFinalBlock();
                StringBuilder builder = new StringBuilder();
                foreach (byte num in stream.ToArray())
                {
                    builder.AppendFormat("{0:X2}", num);
                }
                stream.Close();
                return HashKey + ":" + builder.ToString();
            }
            catch (Exception) { return "xxxx"; }
        }
        ///<summary><![CDATA[字符串DES解密函数]]></summary>  
        ///<param name="str"><![CDATA[被解密字符串 ]]></param>  
        ///<param name="key"><![CDATA[密钥 ]]></param>   
        ///<returns><![CDATA[解密后字符串]]></returns>   
        ///解密
        public static string DecodeDES(string str, string key)
        {
            if (str.Contains(HashKey + ":"))
            {
                str = str.Replace(HashKey + ":", "");
                try
                {
                    DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                    provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
                    provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
                    byte[] buffer = new byte[str.Length / 2];
                    for (int i = 0; i < (str.Length / 2); i++)
                    {
                        int num2 = Convert.ToInt32(str.Substring(i * 2, 2), 0x10);
                        buffer[i] = (byte)num2;
                    }
                    MemoryStream stream = new MemoryStream();
                    CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
                    stream2.Write(buffer, 0, buffer.Length);
                    stream2.FlushFinalBlock();
                    stream.Close();
                    return Encoding.GetEncoding("UTF-8").GetString(stream.ToArray());
                }
                catch (Exception) { return ""; }
            }
            else
            {
                return str;
            }
        }
    }
}
