Example #1
0
        /// <summary>
        /// 解密数据
        /// </summary>
        /// <param name="text"></param>
        /// <param name="sKey"></param>
        /// <returns></returns>
        public static string Decrypt(string text, string sKey)
        {
            var des = new DESCryptoServiceProvider();
            var len = text.Length / 2;

            byte[] inputByteArray = new byte[len];
            for (int x = 0; x < len; x++)
            {
                var i = Convert.ToInt32(text.Substring(x * 2, 2), 16);
                inputByteArray[x] = (byte)i;
            }

            des.Key      = Encoding.ASCII.GetBytes(Md5.Hash(sKey).Substring(0, 8));
            des.IV       = Encoding.ASCII.GetBytes(Md5.Hash(sKey).Substring(0, 8));
            using var ms = new System.IO.MemoryStream();
            using var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return(Encoding.Default.GetString(ms.ToArray()));
        }
Example #2
0
        /// <summary>
        /// 加密数据
        /// </summary>
        /// <param name="text"></param>
        /// <param name="sKey"></param>
        /// <returns></returns>
        public static string Encrypt(string text, string sKey)
        {
            var des = new DESCryptoServiceProvider();

            byte[] inputByteArray = Encoding.Default.GetBytes(text);
            des.Key      = Encoding.ASCII.GetBytes(Md5.Hash(sKey).Substring(0, 8));
            des.IV       = Encoding.ASCII.GetBytes(Md5.Hash(sKey).Substring(0, 8));
            using var ms = new MemoryStream();
            using var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            var ret = new StringBuilder();

            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }

            return(ret.ToString());
        }