⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 md5.cs

📁 对PDF文件添加Tag,防止别人修改pdf文件
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace HashFile
{
   public class MD5
    {
        ///// <summary>
        ///// 实现对一个文件md5的读取,path为文件路径
        ///// </summary>
        ///// <param name="path"></param>
        ///// <returns></returns>
        //public static string md5_hash(string path)
        //{
        //    try
        //    {
        //        FileStream get_file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
         
        //        System.Security.Cryptography.MD5CryptoServiceProvider get_md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
        //        byte[] hash_byte = get_md5.ComputeHash(get_file);
                
        //        string resule = System.BitConverter.ToString(hash_byte);
        //        resule = resule.Replace("-", "");
        //        get_file.Close();
        //        return resule;
        //    }
        //    catch (Exception e)
        //    {

        //        return e.ToString();

        //    }
        //}

       /// <summary>
       /// 对给定文件路径的文件加上标签
       /// </summary>
       /// <param name="path">要加密的文件的路径</param>
       /// <returns>标签的值</returns>
       public static string MD5pdf(string path,string key)
       {

           try
           {
               FileStream get_file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
               byte[] pdfFile = new byte[get_file.Length];
               get_file.Read(pdfFile, 0, (int)get_file.Length);//将文件流读取到Buffer中
               get_file.Close();


               string result = MD5Buffer(pdfFile, 10, pdfFile.Length - 10);//对Buffer中的10个字节以后的内容算MD5
               result = MD5String(result +key);//这儿点的key=“Microsoft.Finance”相当于一个密钥,这样一般人就是知道使用MD5算法,但是若不知道这个字符串还是无法计算出正确的MD5

               byte[] md5 = System.Text.Encoding.ASCII.GetBytes(result);//将字符串转换成字节数组以便写人到文件中

               FileStream fsWrite = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
               fsWrite.Write(pdfFile, 0, 10);//将pdf头,MD5值,和pdf文件的剩下部分重新写入到文件中。
               fsWrite.Write(md5, 0, md5.Length);
               fsWrite.Write(pdfFile, 10, pdfFile.Length - 10);
               fsWrite.Close();

               return result;
           }
           catch (Exception e)
           {

               return e.ToString();

           }
       }
       /// <summary>
       /// 对给定路径的文件进行验证
       /// </summary>
       /// <param name="path"></param>
       /// <returns>是否加了标签或是否标签值与内容值一致</returns>
       public static bool Check(string path,string key)
       {
           try
           {
               FileStream get_file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);

              
               byte[] pdfFile = new byte[get_file.Length];
               get_file.Read(pdfFile, 0, (int)get_file.Length);
               get_file.Close();
               string result = MD5Buffer(pdfFile, 42, pdfFile.Length - 42);//这个42是因为标签位为32位,前面的PDF头占10位。
               result = MD5String(result + key);

               string md5 = System.Text.Encoding.ASCII.GetString(pdfFile, 10, 32);//读取其中保存的MD5
               return result == md5;
           }
           catch
           {

               return false;

           }
       }
       private static string MD5Buffer(byte[] pdfFile, int index, int count)
       {
           System.Security.Cryptography.MD5CryptoServiceProvider get_md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
           byte[] hash_byte = get_md5.ComputeHash(pdfFile, index, count);

           string result = System.BitConverter.ToString(hash_byte);
           result = result.Replace("-", "");
           return result;
       }
       private static string MD5String(string str)
       {
           byte[] MD5Source = System.Text.Encoding.ASCII.GetBytes(str);
           return MD5Buffer(MD5Source, 0, MD5Source.Length);

       }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -