zoukankan      html  css  js  c++  java
  • 关于MD5值加密算法

     public static string getMD5(string str)//该方法获取字符串的md5加密 通经常使用来验证数据
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] pwd = md5.ComputeHash(Encoding.ASCII.GetBytes(str));
            string ret = "";
            for (int i = 0; i < pwd.Length; i++)
            {
                ret += pwd[i].ToString("X");
            }
            return ret;
        }
        private static string GetMD5HashFromFile(string fileName)//该方法用来进行获取文件的md5加密 用来比对 client文件是否被篡改
        {
            try
            {
                FileStream file = new FileStream(fileName, FileMode.Open);
                System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] retVal = md5.ComputeHash(file);
                file.Close();


                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                   // sb.Append(retVal[i].ToString("x2"));//每两位转换为16进制、
                    sb.Append(retVal[i].ToString("X2"));//这儿必须是x2  才行 不然转换的时候 0Xeasy转为X  0 会被去掉、
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {
                throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
            }
        }  
  • 相关阅读:
    第四次实践作业
    第三次实践作业
    第二次实践作业
    第一次实践作业
    第02组 Beta版本演示
    第02组 Beta冲刺(4/4)
    大数据应用技术课程实践--选题与实践方案
    15 手写数字识别-小数据集
    14 深度学习-卷积
    13 垃圾邮件分类2
  • 原文地址:https://www.cnblogs.com/llguanli/p/7250514.html
Copyright © 2011-2022 走看看