zoukankan      html  css  js  c++  java
  • MD5 加密解密字符串

    方法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;    
        }   
    /**
            * 验证输入的密码是否正确
         * md5不能解密,验证一个字符串是不是跟另一个加密之前的字符串相等,只能把这个字符串使用相同的加密,再比较密文 * @param password 加密后的密码 * @param inputString 输入的字符串 * @return 验证结果,TRUE:正确 FALSE:错误
    */ public static boolean validatePassword(String password, String inputString)
         {
    if(password.equals(encodeByMD5(inputString)))
           {
    return true; }
      
           else
           { return false; } }

     

  • 相关阅读:
    ext4.2常用的几种弹框
    oracle的批量操作sql语句
    ztree异步加载树节点
    shiro接口对象介绍
    jquery记住密码
    整合ssm框架
    redis中 Could not get a resource from the pool 异常解决
    redis的安装
    centos下tomcat的安装
    centos下MySQL的安装
  • 原文地址:https://www.cnblogs.com/zhaoyl9/p/11096155.html
Copyright © 2011-2022 走看看