MD5计算器,字符串&文件校验

2/22/2017来源:ASP.NET技巧人气:3082

练习所作

下面是将文件和字符串转化为md5值代码

 PRivate static string GetMd5FromFile(string path)
        {
            using (MD5 md=MD5.Create())
            {
                using (FileStream fsRead = File.OpenRead(path))
                {
                    byte[] buffer = md.ComputeHash(fsRead);
                    md.Clear();
                    StringBuilder sbMD5 = new StringBuilder();
                    for (int i = 0; i < buffer.Length; i++)
                    {
                        sbMD5.Append(buffer[i].ToString("X2"));
                    }
                    return sbMD5.ToString().ToLower();
                }
            }
        }

 private static string GetMD5FromString(string str)
        {
            using (MD5 md=MD5.Create())
            {
                byte[] strBuffer = Encoding.Default.GetBytes(str);
                byte[] mdBuffer = md.ComputeHash(strBuffer);
                StringBuilder sbMd5 = new StringBuilder();
                for (int i = 0; i < mdBuffer.Length; i++)
                {
                    sbMd5.Append(mdBuffer[i].ToString("X2"));
                }
                return sbMd5.ToString().ToLower();
            }
        }


无论是文件还是字符串用ComputeHash()方法,传入的是byte[] ,返回值也是byte[].

ComputeHash()得到的返回值byte[]

希望得到的是

循环将素组中每个元素转换成16进制,保存两位.->buffer[i].ToString("X2"),保存到StringBuilder中

拼接得到的字符串

用ToLower()处理,返回

下面是winform程序中的所有代码,

可以在路径textbox中直接输入路径,判断路径存在?打开改路径:打开默认路径

判断路径中的文件存在?直接计算文件md5值:提示文件不存在,都在leave()中执行

信息textbox中输入字符串,在TextChanged()事件中执行计算MD5方法

比较两个MD5值是否相同,这个简单,注意都转换成大写或小写

注意:空格字符也是内容 "1"的MD5和"1   "不同

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace md5计算器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCompare_Click(object sender, EventArgs e)
        {
            string strMD5 = txtMD.Text.Trim().ToLower();
            string strRef = txtReference.Text.Trim().ToLower();
            bool b = ResCompare(strMD5,strRef);
            string res = b ? "一致" : "MD5值 与 参考值 不相同!";
            MessageBox.Show(res);

        }

        private static bool ResCompare(string strMD5, string strRef)
        {
            if (strMD5==strRef)
            {
                return true;
            }
            return false;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            String str = txtInfo.Text;
            txtMD.Text = GetMD5FromString(str);
        }

        private static string GetMD5FromString(string str)
        {
            using (MD5 md=MD5.Create())
            {
                byte[] strBuffer = Encoding.Default.GetBytes(str);
                byte[] mdBuffer = md.ComputeHash(strBuffer);
                StringBuilder sbMd5 = new StringBuilder();
                for (int i = 0; i < mdBuffer.Length; i++)
                {
                    sbMd5.Append(mdBuffer[i].ToString("X2"));
                }
                return sbMd5.ToString().ToLower();
            }
        }

        private void btnChooseFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            //ofd.InitialDirectory = @"D:\BEI\download\other";
            ofd.Filter = "所有文件|*.*|Excel文件(*.xls;*.xlsx)|*.xls;*.xlsx";
            ofd.Title = "请选择一个文件";
            ofd.Multiselect = false;
            if (ofd.ShowDialog()==DialogResult.OK)
            {
                txtPath.Text = ofd.FileName;
                txtMD.Text = GetMd5FromFile(ofd.FileName);
            }
        }

        private void txtPath_TextChanged(object sender, EventArgs e)
        {
           

        }

        private static string GetMd5FromFile(string path)
        {
            using (MD5 md=MD5.Create())
            {
                using (FileStream fsRead = File.OpenRead(path))
                {
                    byte[] buffer = md.ComputeHash(fsRead);
                    md.Clear();
                    StringBuilder sbMD5 = new StringBuilder();
                    for (int i = 0; i < buffer.Length; i++)
                    {
                        sbMD5.Append(buffer[i].ToString("X2"));
                    }
                    return sbMD5.ToString().ToLower();
                }
            }
        }

        private void txtPath_Leave(object sender, EventArgs e)
        {
          
            string path = txtPath.Text;

            if (path.Trim()=="")
            {
                txtMD.Text = "在编辑框中输入字符串 ->自动计算出MD5值";
            }
            else
            {
                if (Directory.Exists(path))
                {
                    OpenFileDialog ofd = new OpenFileDialog();
                    ofd.InitialDirectory = path;
                    ofd.Filter = "所有文件|*.*|Excel文件(*.xls;*.xlsx)|*.xls;*.xlsx";
                    ofd.Title = "请选择一个文件";
                    ofd.Multiselect = false;
                    if (ofd.ShowDialog() == DialogResult.OK)
                    {
                        txtPath.Text = ofd.FileName;
                        txtMD.Text = GetMd5FromFile(ofd.FileName);
                    }
                    return;
                }

                if (File.Exists(path) == true)
                {
                    txtMD.Text = GetMd5FromFile(path);
                }
                else
                {
                    MessageBox.Show("文件不存在");
                }
            }
           
        }
    }
}