定义了命名空间:using System.Security.Cryptography;  MD5 md5=new MD5CryptoServiceProvider();
 string result=md5.ComputeHash(this.textBox3.Text.Trim());
这样加密出错了,说无法从"string"转换这"System.IO.Stream", 那应怎样修改这段程序来进行this.textBox3.Text内容的加密呢,请大家帮帮忙!!!

解决方案 »

  1.   

    添加web.dll引用using System.Web.Security;string result = FormsAuthentication.HashPasswordForStoringInConfigFile(this.textBox1.Text,"MD5");
      

  2.   

    这是我在实际项目中写的的,包装了一下。
    using System;
    using System.Text;
    using System.IO;
    using System.Security.Cryptography;namespace CryptionDataMD5
    {
    /// <summary>
    /// Make message digest based on MD5 for byte array and string 
    /// </summary>
    /// <Author>Yao</Author>
    /// <Date>2005/06/13</Date>
    public class CryptionDataMD5
    {
    /// <summary>
    /// Make message digest based on MD5 for byte array
    /// </summary>
    /// <param name="origin">source byte array</param>
    /// <returns>message digest</returns>
    public static string EncryptionByteData(byte[] SourceData)
    {
    try 
    {
    //create a stream object
    MemoryStream stream = new MemoryStream(SourceData);

    //define hex table
    const string HEX_TABLE = "0123456789abcdef";

    //create a MD5CryptoServiceProvider object
    MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
    //Calculate MD5 Checksum
    byte[] hashData = md5.ComputeHash(stream); //convert to string
    StringBuilder sb=new StringBuilder();
    sb.Length =hashData.Length *2;

    //make string sb
    for(int i=0;i<hashData.Length ;i++)
    {
    sb[i*2]=HEX_TABLE[hashData[i]>>4];
    sb[i*2+1]=HEX_TABLE[hashData[i] & 0xF];
    }

    return sb.ToString();
    }
    catch (Exception ex)
    {
    throw ex;
    }
    } /// <summary>
    /// /// Make message digest based on MD5 for string
    /// </summary>
    /// <param name="origin">source string</param>
    /// <returns>message digest</returns>
    public static string EncryptionStringData(string SourceData) 
    {
    string messageDigest = string.Empty;
    try 
    {
    byte[] sorData = Encoding.Default.GetBytes(SourceData);
    messageDigest = EncryptionByteData(sorData);
    }
    catch(Exception ex)
    {
    throw ex;
    }
    return messageDigest;
    }
    }
    }
      

  3.   

    你想要加密成string 类型,用System.web.security,不要用using System.Security.Cryptography;
      

  4.   

    我这有个好简单的
    using System;
    using System.Security.Cryptography;
    using System.IO;
    using System.Text;public string MD5(Stream stream)
    {
    const string HEX_TABLE = "0123456789ABCDEF";
    MD5 md5 = new MD5CryptoServiceProvider();
    //Calculate MD5 Checksum
    byte[]data = md5.ComputeHash(stream);
    //convert to string
    StringBuilder sb=new StringBuilder();
    sb.Length =data.Length *2;
    for(int i=0;i<data.Length ;i++)
    {
    sb[i*2]=HEX_TABLE[data[i]>>4];
    sb[i*2+1]=HEX_TABLE[data[i] & 0xF];
    }
    return sb.ToString();
    }public string MD5(string s)
    {
    byte[] data=ASCIIEncoding.ASCII.GetBytes(s);
    MemoryStream stream=new MemoryStream(data);
    //stream.Write(data,0,data.Length);
    return MD5(stream);
    }
      

  5.   

    '把字符串转换成md5
    Private Function Mymd5(ByVal instr As String) As String        Dim Buffer As Byte()
            Dim HashData As Byte()        Dim MD5test As System.Security.Cryptography.MD5 = System.Security.Cryptography.MD5.Create        Buffer = System.Text.Encoding.Default.GetBytes(instr)
            HashData = MD5test.ComputeHash(Buffer)        Dim encoded As String
            encoded = BitConverter.ToString(HashData)
            encoded = encoded.Replace("-", "")        Mymd5 = encoded
    End Function
      

  6.   

    'MD5
    Public Shared Function MD5(ByVal strKey As String) As String
    Dim sPwd As String
    Dim bytPwd As [Byte]() = ConStrArr(strKey)
    Dim hashPwd As Byte() = CType(System.Security.Cryptography.CryptoConfig.CreateFromName("MD5"), _
     System.Security.Cryptography.HashAlgorithm).ComputeHash(bytPwd)
    sPwd = BitConverter.ToString(hashPwd)
    sPwd = LCase(sPwd.Replace("-", ""))   '去掉中间的"-"符号并转换为小写字母
    Return sPwd
    End Function呵呵,楼上的代码怎么跟我的类似的?开玩笑的拉。