首页 > 代码库 > MD5加密解密类

MD5加密解密类


    public class CrytMd5
    {
        private static string symmetricKey = "2fSA4Gd3czxzmsel5kgEs6dXj6h4vA6y";    //对称加密密钥
        private static string symmetricIV = "he86rTcIvr12CiwN";    //对称加密初始向量

        /// <summary>
        /// 设置密钥,只写
        /// </summary>
        public static string Key
        {
            set
            {
                symmetricKey = value;
            }
        }

        /// <summary>
        /// 设置初始向量,只写
        /// </summary>
        public static string IV
        {
            set
            {
                symmetricIV = value;

            }
        }

        /// <summary>
        /// 将字节数组buff进行加密,使用该方法之前需设置密钥(Key)和初始向量(IV)
        /// </summary>
        /// <param name = "buff" > 需要加密的字节数组 </ param >
        /// < returns > 返回加密后的字节数组 </ returns >
        public static string EncryptTo(string ordiStr)
        {
            string ret = ordiStr;
            try
            {
                RijndaelManaged myRijndael = new RijndaelManaged();

                byte[] key = System.Text.Encoding.UTF8.GetBytes(symmetricKey);
                byte[] IV = System.Text.Encoding.UTF8.GetBytes(symmetricIV);

                byte[] buff = System.Text.Encoding.UTF8.GetBytes(ordiStr);
                //取得加密器
                ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, IV);
                //加密数据
                MemoryStream msEncrypt = new MemoryStream();
                CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);

                //将数据写入流csEncrypt中
                csEncrypt.Write(buff, 0, buff.Length);
                csEncrypt.FlushFinalBlock();

                //读取加密后的数据
                buff = msEncrypt.ToArray();
                ret = ConvertUtil.Byte2HEX(buff);

                //关闭流
                csEncrypt.Close();
                msEncrypt.Close();
                return ret;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        //新的MD5算法
        public static string EncryptCode(string password)
        {
            //明文密码由字符串转换为byte数组
            byte[] clearBytes = new System.Text.UnicodeEncoding().GetBytes(password);
            //由明文的byte数组计算出MD5密文byte数组
            byte[] hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);

            //把byte数组转换为字符串后返回,BitConverter用于将基础数据类型与字节数组相互转换
            string Encrystr = BitConverter.ToString(hashedBytes);
            return Encrystr.Replace("-", "");
        }

        /// <summary>
        /// 将字节数组buff进行解密,使用该方法之前需设置密钥(Key)和初始向量(IV)
        /// </summary>
        /// <param name="buff">需要解密的字节数组</param>
        /// <returns>返回解密后的字节数组</returns>
        public static string DecryptTo(string cryptedStr)
        {
            string ret = cryptedStr;
            try
            {
                RijndaelManaged myRijndael = new RijndaelManaged();

                byte[] key = System.Text.Encoding.UTF8.GetBytes(symmetricKey);
                byte[] IV = System.Text.Encoding.UTF8.GetBytes(symmetricIV);

                //取得解密器
                ICryptoTransform decryptor = myRijndael.CreateDecryptor(key, IV);

                //解密数据
                byte[] buff = ConvertUtil.Hex2Byte(cryptedStr);
                MemoryStream msDecrypt = new MemoryStream(buff);
                CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);

                //读取已解密的数据
                buff = new byte[buff.Length];
                csDecrypt.Read(buff, 0, buff.Length);
                msDecrypt.Close();
                csDecrypt.Close();

                //去掉解密后多余的数据(后面的0,这是加密时为了补齐块大小而填充的)
                ArrayList list = new ArrayList();
                for (int i = 0; i < buff.Length; i++)
                {
                    if (buff[i] != 0)
                    {
                        list.Add(buff[i]);
                    }
                    else
                    {
                        break;
                    }
                }
                //读取解密后的正确数据
                buff = new byte[list.Count];
                list.CopyTo(buff, 0);

                //去掉解密后多余的数据(后面的0,这是加密时为了补齐块大小而填充的)
                //ret = System.Text.Encoding.UTF8.GetString(buff);                
                ret = System.Text.Encoding.UTF8.GetString(buff, 0, buff.Length);
                return ret;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static string GetMd5Str(string ConvertString)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
            t2 = t2.Replace("-", "");

            t2 = t2.ToLower();

            return t2;
        }
   


MD5加密解密类