MD5 encryption and decryption string

方法1:
    using System.Text;
    using System.Security.Cryptography;


        public string Hash(string toHash)
        {
            MD5CryptoServiceProvider crypto = new MD5CryptoServiceProvider();
            byte[] bytes = Encoding.UTF7.GetBytes(toHash);
            bytes = crypto.ComputeHash(bytes);
            StringBuilder sb = new StringBuilder();
            foreach (byte num in bytes)
            {
                sb.AppendFormat("{0:x2}", num);
            }
             return sb.ToString();        //32位
            return sb.ToString().Substring(8,16);        //16位
        }

 

方法2:

16位

   public string GetMd5(string str)
   {
    System.Security.Cryptography.MD5CryptoServiceProvider md5=new MD5CryptoServiceProvider();
    string a=BitConverter.ToString(md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(str)),4,8);
    a=a.Replace("-","");
    return a;
   }

32位

   public string GetMd5(string str)
   {
    System.Security.Cryptography.MD5CryptoServiceProvider md5=new MD5CryptoServiceProvider();
    string a=BitConverter.ToString(md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(str)));
    a=a.Replace("-","");
    return a;
   }


Winform:  

    public static string StringToMD5Hash(string inputString)    
    {    
        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();    

        byte[] encryptedBytes = md5.ComputeHash(Encoding.ASCII.GetBytes(inputString));    

        StringBuilder sb = new StringBuilder();    

        for (int i = 0; i < encryptedBytes.Length; i++)    

        {    
            sb.AppendFormat("{0:x2}", encryptedBytes[i]);         
        }    

        return sb.ToString();    
    }   

       
    Webform:  

    public static string md5(string pwd)         
    {    
        string md5pwd = FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "MD5");    

        return md5pwd;    
    }   
/ * * 
        * Authentication password is correct 
     * MD5 not decryption, authentication string is not equal to the other with a string before encryption, can only use the same encryption string, compare ciphertext * @param password encrypted passwords * @param inputString input string * @return verification result, TRUE: right FALSE: error
* / public static Boolean the validatePassword (string password, string inputString)
     {
IF (password.equals (encodeByMD5 (inputString)))
       {
return to true ; }
  
       the else
       { return to false ; } }

 

Guess you like

Origin www.cnblogs.com/zhaoyl9/p/11096155.html