C# 加密解密

#region 加密 解密

        /// <summary>

        /// MD5

        /// </summary>

        /// <param name="input"></param>

        /// <returns></returns>

        public static String EncryptMD5(string input)

        {

            //Byte[] clearBytes = new UnicodeEncoding().GetBytes(input);

            //Byte[] hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);

            //return BitConverter.ToString(hashedBytes).Replace("-", "");

 

            MD5 md5 = new MD5CryptoServiceProvider();

            byte[] data = Encoding.UTF8.GetBytes(input);//将字符编码为一个字节序列

            byte[] md5data = md5.ComputeHash(data);//计算data字节数组的哈希值

            md5.Clear();

            string str = "";

            for (int i = 0; i < md5data.Length; i++)

            {

                str += md5data[i].ToString("x").PadLeft(2, '0');

            }

            return str;

 

        }

        /// <summary>

        /// 64位DES加密

        /// </summary>

        /// <param name="input"></param>

        /// <returns></returns>

        public static string EncryptDES(string input)

        {

            byte[] bKey = Encoding.UTF8.GetBytes(ENCRYPT_KEY);

            byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };//对称算法的初始化向量,64位(8个字节)

            try

            {

                DESCryptoServiceProvider des = new DESCryptoServiceProvider();

                byte[] bInput = Encoding.UTF8.GetBytes(input);

                MemoryStream ms = new MemoryStream();

                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(bKey, IV), CryptoStreamMode.Write);

                cs.Write(bInput, 0, bInput.Length);

                cs.FlushFinalBlock();

                return Convert.ToBase64String(ms.ToArray());

            }

            catch (Exception ex)

            {

                throw (ex);

            }

        }

        /// <summary>

        /// 64位DES解密

        /// </summary>

        /// <param name="input"></param>

        /// <returns></returns>

        public static string DecryptDES(string input)

        {

            byte[] bKey = Encoding.UTF8.GetBytes(ENCRYPT_KEY);

            byte[] len = new Byte[input.Length];

            byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

            try

            {

                DESCryptoServiceProvider des = new DESCryptoServiceProvider();

                len = Convert.FromBase64String(input);

                MemoryStream ms = new MemoryStream();

                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(bKey, IV), CryptoStreamMode.Write);

                cs.Write(len, 0, len.Length);

                cs.FlushFinalBlock();

                return Encoding.UTF8.GetString(ms.ToArray());

            }

            catch (Exception ex)

            {

                throw (ex);

            }

        }

        #endregion

posted @ 2014-09-23 14:27  Mark.Yang  阅读(313)  评论(0)    收藏  举报