namespace CrazyStudio.Core.CryptHelper
{
    public static class DESCrypt
    {
        #region  ========算法========
        private static string key;//密钥
        static DESCrypt()
        {
            key = "";// ConfigExtensions.Configuration["DESCryptKey"];
        }
        private static byte[] Keys = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };//8个bit位，是DES算法的初始化向量  加解密钥也是8位;
        /// <summary>
        /// 解密字符串
        /// </summary>
        /// <param name="decryptString">是要被解密的密文数据</param>
        /// <param name="decryptKey">DES算法的工作密钥</param>
        /// <returns>明文</returns>
        public static string Decrypt(string decryptString, string decryptKey)
        {
            try
            {
                decryptKey = Utils.GetSubString(decryptKey, 8, "");
                decryptKey = decryptKey.PadRight(8, ' ');
                byte[] bytes = Encoding.UTF8.GetBytes(decryptKey);
                byte[] keys = Keys;
                byte[] buffer = Convert.FromBase64String(decryptString);
                DES provider = DES.Create();
                using (MemoryStream stream = new MemoryStream())
                {
                    CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(bytes, keys), CryptoStreamMode.Write);
                    stream2.Write(buffer, 0, buffer.Length);
                    stream2.FlushFinalBlock();
                    return Encoding.UTF8.GetString(stream.ToArray());
                }
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="decryptString">要被加密数据</param>
        /// <param name="decryptKey">DES算法的工作密钥</param>
        /// <returns>密文</returns>
        public static string Encrypt(string encryptString, string encryptKey)
        {
            encryptKey = Utils.GetSubString(encryptKey, 8, "");
            encryptKey = encryptKey.PadRight(8, ' ');
            byte[] bytes = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
            byte[] keys = Keys;
            byte[] buffer = Encoding.UTF8.GetBytes(encryptString);
            DES provider = DES.Create();
            using (MemoryStream stream = new MemoryStream())
            {
                CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(bytes, keys), CryptoStreamMode.Write);
                stream2.Write(buffer, 0, buffer.Length);
                stream2.FlushFinalBlock();
                return Convert.ToBase64String(stream.ToArray());
            }
        }
        #endregion

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        public static string Encrypt(string Text)
        {
            return Encrypt(Text, key);
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        public static string Decrypt(string Text)
        {
            return Decrypt(Text, key);
        }
    }
}
