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());
             }
         } 
 
    }
 }