zoukankan      html  css  js  c++  java
  • C# 生成MD5编码方法(不同位数)

            /// <summary> 
            /// </summary> 
            /// <param name="strSource">待加密字串</param> 
            /// <returns>加密后的字串</returns> 
            public static string MD5Encrypt(string strSource)
            {
                return MD5Encrypt(strSource, 32);
            }

            /// <summary> 
            /// </summary> 
            /// <param name="strSource">待加密字串</param> 
            /// <param name="length">16或32值之一,其它则采用.net默认MD5加密算法</param> 
            /// <returns>加密后的字串</returns> 
            public static string MD5Encrypt(string strSource, int length)
            {
                byte[] bytes = Encoding.ASCII.GetBytes(strSource);
                byte[] hashValue = ((System.Security.Cryptography.HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(bytes);
                StringBuilder sb = new StringBuilder();
                switch (length)
                {
                    case 16:
                        for (int i = 4; i < 12; i++)
                            sb.Append(hashValue[i].ToString("x2"));
                        break;
                    case 32:
                        for (int i = 0; i < 16; i++)
                        {
                            sb.Append(hashValue[i].ToString("x2"));
                        }
                        break;
                    default:
                        for (int i = 0; i < hashValue.Length; i++)
                        {
                            sb.Append(hashValue[i].ToString("x2"));
                        }
                        break;
                }
                return sb.ToString();
            } 

  • 相关阅读:
    JDK1.7.0环境变量配置【Windows】
    QQ游戏百万人同时在线服务器架构实现
    C#关于AutoResetEvent的使用介绍[转载]
    ConcurrentDictionary:.NET 4.0中新的线程安全的哈希表
    大型网站采用的具有稳定性的系统构架
    简单使用Enterprise Library 5.0 中的Cache功能
    来电显示MODEM的的选购指南
    浅谈大型网站动态应用系统架构
    log4net工程中使用备忘
    稳定高效大型系统架构集群中间件开发
  • 原文地址:https://www.cnblogs.com/houzuofeng/p/3253187.html
Copyright © 2011-2022 走看看