• 计算文件的MD5值上传到服务器 下载验证文件是否被篡改


    using System;
    using System.Windows.Forms;
    using System.Security.Cryptography;
    using System.IO;

    namespace 计算文件的MD5
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                //创建与服务器的连接
                FtpClient fc = new FtpClient("127.0.0.1","test","123");
                //上传本地文件到服务器命名为02...文件
                fc.Upload(new FileInfo("textFilePath.Text"), "02" + DateTime.Now.ToString("yyyyMMdd")+".xls");
                //获取内容的MD5
                string md5 = MD5Value("textFilePath.text");
                //获取文件的MD5加上密钥之后的新的MD5
                string newMD5 = GetMD5(md5+"tianwang");//文件的MD5加上密钥
                //流处理,将新的MD5值写入文本文件
                File.WriteAllText(@"c:\02" + DateTime.Now.ToString("yyyyMMdd") + ".text",newMD5);
                //上传文本文件到远程服务器
                fc.Upload(new FileInfo(@"c:\02" + DateTime.Now.ToString("yyyyMMdd") + ".text"), @"c:\02" + DateTime.Now.ToString("yyyyMMdd") + ".text");
            }
            /// <summary>
            /// 计算文件MD5值
            /// </summary>
            /// <param name="str">需要计算的文件路径</param>
            /// <returns>MD5值</returns>
            public static string MD5Value(String filepath)
            {
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] md5ch;
                using (FileStream fs = File.OpenRead(filepath))
                {
                    md5ch = md5.ComputeHash(fs);
                }
                md5.Clear();
                string strMd5 = "";
                for (int i = 0; i < md5ch.Length - 1; i++)
                {
                    strMd5 += md5ch[i].ToString("x").PadLeft(2, '0');
                }
                return strMd5;
            }
            /// <summary>
            /// 计算字符串的MD5
            /// </summary>
            /// <param name="sDataIn"></param>
            /// <returns></returns>
            public static string GetMD5(string sDataIn)
            {
                MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                byte[] bytValue, bytHash;
                bytValue = System.Text.Encoding.UTF8.GetBytes(sDataIn);
                bytHash = md5.ComputeHash(bytValue);
                md5.Clear();
                string sTemp = "";
                for (int i = 0; i < bytHash.Length; i++)
                {
                    sTemp += bytHash[i].ToString("X").PadLeft(2, '0');
                }
                return sTemp.ToLower();
            }

           

          

            /// 判断服务器上的文件是否被篡改,如果没有则读取
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button2_Click(object sender, EventArgs e)
            {
                //获取当前系统时间
                string datestr = DateTime.Now.ToString("yyyyMMdd");
                FtpClient fc = new FtpClient("127.0.0.1","test","123");
                //将服务器上个文件下载下来
                fc.Download("02" + datestr + ".xls", @"c:\02" + datestr + ".xls");

                //计算下载下来的xls文件的MD5值+密钥
                string md5 = MD5Value(@"c:\02" + datestr + ".xls");
                string newMD5 = GetMD5(md5 + "tianwang");

                //下载服务器上的保留的MD5txt文件MD5的值
                fc.Download("02" + datestr + ".txt", @"c:\t.text");
                string txt = File.ReadAllText(@"c:\t.txt");

                //检查文件是否被篡改
                if(txt!=newMD5)
                {
                    MessageBox.Show("文件被篡改");
                    return;
                }

                using(FileStream stream=File.OpenRead(@"c:\02.xls"))
                {
                    HSSFWorkbook wb = new HSSFWorkbook(stream);
                    HSSFSheet sheet = wb.GetSheetAt(0);
                    MessageBox.Show(sheet.LastRowNum.ToString());
                }
            }

        }
    }

    感谢来访,共同学习!
  • 相关阅读:
    RII K25A 语音空中飞鼠 红外学习步骤
    淘宝导航栏颜色修改
    上海巨人网络参与网络诈骗整个流程
    xp的停止更新对我们有什么影响?
    USB3.0 和usb 2.0的区别
    一些有意思的脑际急转弯
    淘宝上 1200左右的组装电脑,真的性价比很高么?
    【图文教程】用“iz3d”软件将您的游戏打造为红蓝3D游戏。
    网上下载的“上下3D”和“左右3D”影片该如何播放?
    电脑cmos是什么?和bois的区别?
  • 原文地址:https://www.cnblogs.com/dingxiaowei/p/3058763.html
Copyright © 2020-2023  润新知