zoukankan      html  css  js  c++  java
  • DES加密解密 MD5加密解密

     #region  MD5 加密
    
            /// <summary>
            /// MD5加密静态方法
            /// </summary>
            /// <param name="EncryptString">待加密的密文</param>
            /// <returns></returns>
            public static string MD5Encrypt(string EncryptString)
            {
                if (string.IsNullOrEmpty(EncryptString))
                {
                    throw (new Exception("密文不得为空"));
                }
    
                MD5 m_ClassMD5 = new MD5CryptoServiceProvider();
                string m_strEncrypt = "";
                try
                {
                    m_strEncrypt = BitConverter.ToString(m_ClassMD5.ComputeHash(Encoding.Default.GetBytes(EncryptString))).Replace("-", "");
                }
                catch (ArgumentException ex) { throw ex; }
                catch (CryptographicException ex) { throw ex; }
                catch (Exception ex) { throw ex; }
                finally {
                    m_ClassMD5.Clear();
                }
    
                return m_strEncrypt;
            }
    
            #endregion
    
            #region  DES 加密解密  可以自己设定
            private static string DesKey = "wanwanzz";
    
            /// <summary>
            /// DES 加密(数据加密标准,速度较快,适用于加密大量数据的场合)
            /// </summary>
            /// <param name="EncryptString">待加密的密文</param>
            /// <returns></returns>
            public static string DESEncrypt(string EncryptString)
            {
                if (string.IsNullOrEmpty(EncryptString))
                {
                    throw (new Exception("密文不得为空"));
                }
                if (string.IsNullOrEmpty(DesKey))
                {
                    throw (new Exception("密钥不得为空"));
                }
                if (DesKey.Length!=8)
                {
                    throw (new Exception("密钥必须为8位"));
                }
    
                //定义偏移量
                byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
    
                string m_strEncrypt = "";
    
                DESCryptoServiceProvider m_DESProvider = new DESCryptoServiceProvider();
                try
                {
                    byte[] m_btEncryptString = Encoding.Default.GetBytes(EncryptString);
                    MemoryStream m_stream = new MemoryStream();
                    CryptoStream m_cstream = new CryptoStream(m_stream, m_DESProvider.CreateEncryptor(Encoding.Default.GetBytes(DesKey), m_btIV), CryptoStreamMode.Write);
                    m_cstream.Write(m_btEncryptString, 0, m_btEncryptString.Length);
                    m_cstream.FlushFinalBlock();
                    m_strEncrypt = Convert.ToBase64String(m_stream.ToArray());
                    m_stream.Close();
                    m_stream.Dispose();
                    m_cstream.Close();
                    m_cstream.Dispose();
    
                }
                catch (IOException ex) { throw ex; }
                catch (CryptographicException ex) { throw ex; }
                catch (ArgumentException ex) { throw ex; }
                catch (Exception ex) { throw ex; }
    
                finally
                {
                    m_DESProvider.Clear();
                }
                return m_strEncrypt;
            }
    
    
            /// <summary>
            /// DES 解密(数据加密标准,速度较快,适用于加密大量数据的场合)
            /// </summary>
            /// <param name="DecryptString"></param>
            /// <returns></returns>
            public static string DESDecrypt(string DecryptString)
            {
                if (string.IsNullOrEmpty(DecryptString))
                {
                    throw (new Exception("密文不得为空"));
                }
                if (string.IsNullOrEmpty(DesKey))
                {
                    throw (new Exception("密钥不得为空"));
                }
                if (DesKey.Length != 8)
                {
                    throw (new Exception("密钥必须为9位"));
                }
    
                byte[] m_btIV = { 0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF};
                string m_strDecrpty = "";
                DESCryptoServiceProvider m_DESProvider = new DESCryptoServiceProvider();
                try
                {
                    byte[] m_btDecrptyString = Convert.FromBase64String(DecryptString);
                    MemoryStream m_stream = new MemoryStream();
                    CryptoStream m_cstream = new CryptoStream(m_stream, m_DESProvider.CreateDecryptor(Encoding.Default.GetBytes(DesKey), m_btIV), CryptoStreamMode.Write);
                    m_cstream.Write(m_btDecrptyString, 0, m_btDecrptyString.Length);
                    m_cstream.FlushFinalBlock();
                    m_strDecrpty = Encoding.Default.GetString(m_stream.ToArray());
                    m_stream.Close();
                    m_stream.Dispose();
                    m_cstream.Close();
                    m_cstream.Dispose();
                }
                catch (IOException ex) { throw ex; }
                catch (CryptographicException ex) { throw ex; }
                catch (ArgumentException ex) { throw ex; }
                catch (Exception ex) { throw ex; }
    
                return m_strDecrpty;
            }
    
            #endregion
    
    
            #region MD5加密解密
            /// <summary>   
            /// MD5加密   
            /// </summary>   
            /// <param name="strSource">需要加密的字符串</param>   
            /// <returns>MD5加密后的字符串</returns>   
            public static string Md5Encrypt(string strSource)
            {
                //把字符串放到byte数组中   
                byte[] bytIn = System.Text.Encoding.Default.GetBytes(strSource);
                //建立加密对象的密钥和偏移量           
                byte[] iv = { 102, 16, 93, 156, 78, 4, 218, 32 };//定义偏移量   
                byte[] key = { 55, 103, 246, 79, 36, 99, 167, 3 };//定义密钥   
                //实例DES加密类   
                DESCryptoServiceProvider mobjCryptoService = new DESCryptoServiceProvider();
                mobjCryptoService.Key = iv;
                mobjCryptoService.IV = key;
                ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
                //实例MemoryStream流加密密文件   
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
                cs.Write(bytIn, 0, bytIn.Length);
                cs.FlushFinalBlock();
                return System.Convert.ToBase64String(ms.ToArray());
            }
            #endregion
    
    
            #region MD5解密
            /// <summary>   
            /// MD5解密   
            /// </summary>   
            /// <param name="Source">需要解密的字符串</param>   
            /// <returns>MD5解密后的字符串</returns>   
            public static string Md5Decrypt(string Source)
            {
                //将解密字符串转换成字节数组   
                byte[] bytIn = System.Convert.FromBase64String(Source);
                //给出解密的密钥和偏移量,密钥和偏移量必须与加密时的密钥和偏移量相同   
                byte[] iv = { 102, 16, 93, 156, 78, 4, 218, 32 };//定义偏移量   
                byte[] key = { 55, 103, 246, 79, 36, 99, 167, 3 };//定义密钥   
                DESCryptoServiceProvider mobjCryptoService = new DESCryptoServiceProvider();
                mobjCryptoService.Key = iv;
                mobjCryptoService.IV = key;
                //实例流进行解密   
                System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length);
                ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
                CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
                StreamReader strd = new StreamReader(cs, Encoding.Default);
                return strd.ReadToEnd();
            }
            #endregion
    

      

  • 相关阅读:
    Sass开发环境搭建
    三款Javascript SPAs框架资料整理和总结
    Web纯前端“旭日图”实现元素周期表
    能在多种前端框架下使用的表格控件
    控件使用经验-MVP模式+控件封装
    最好的Angular2表格控件
    跨平台开发的两种方法及其对比
    是时候 UWP 了 !
    你的系统也可以拥有“数据透视表”功能!
    Xamarin 免费了,你能做什么?
  • 原文地址:https://www.cnblogs.com/pushYYL/p/10026219.html
Copyright © 2011-2022 走看看