//字符串得到MD5加密串
using System.Text;
using System.Security.Cryptography;
string str="afdsfklsfds";//你的字符串
byte[] bytetohash=Encoding.Default.GetBytes(str);
byte[] hashvalue=(new MD5CryptoServiceProvider()).ComputeHash(bytetohash);
string hashstring=BitConverter.ToString(hashvalue);

解决方案 »

  1.   

    下面的例子或许有点用。注释很详细。
    using System;
    using System.Security.Cryptography;
    using System.Text;
    using System.IO;
    using System.Data;
    ........
    /// <summary>
    /// MD5CryptoServiceProvider类使用加密服务提供程序提供的方法实现输入数据的MD5哈希值
    /// </summary>
    public string  CoumputeHash1(string szToHashString)
    {

    //将 szToHashString转换为字节数组
    byte[] byteToHash = Encoding.Default.GetBytes(szToHashString);
    //创建的 MD5 类的实例
    System.Security.Cryptography.MD5CryptoServiceProvider csp=new System.Security.Cryptography.MD5CryptoServiceProvider();
    byte[] byteHashValue = csp.ComputeHash(byteToHash);//计算哈希值
    //将byte数组转化为string
    string szRetHashString=BitConverter.ToString(byteHashValue);
    return szRetHashString;
    }
    /// <summary>
    /// CryptoConfig类用于访问加密配置信息 
    /// </summary>
    public string  CoumputeHash2(string szToHashString)
    {
    //将 szToHashString转换为字节数组
    Byte[] byteToHash = Encoding.Default.GetBytes(szToHashString);

    //使用由加密配置系统返回的 MD5 实例创建哈希值
    byte[] byteHashValue = ((HashAlgorithm) CryptoConfig.CreateFromName("MD5")).ComputeHash(byteToHash);
    //将byte数组转化为string

    string szRetHashString=BitConverter.ToString(byteHashValue);
    return szRetHashString;

    }