这段程序是想读取D盘下叫Test的PDF文件,然后计算MD5值,再加密,再提取新的MD5值。将原文件的MD5值和加密后的MD5值以及水印值分别返回到Textbox。
现在报错【非静态的字段、方法或属性“WindowsFormsApplication1.Form1.textBox1”要求对象引用】,小的菜鸟一只,还请各位多多帮忙,非常急啊!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public void textBox1_TextChanged(object sender, EventArgs e)
        {
    
        }        public void textBox2_TextChanged(object sender, EventArgs e)
        {        }
         
        public void textBox3_TextChanged(object sender, EventArgs e)
        {        }        
        
        
 
        public class MD5
        {
            /// <summary>
            /// 对给定文件路径的文件加上标签
            /// </summary>
            /// <param name="D:/Test.pdf">要加密的文件的路径</param>
            /// <returns>标签的值</returns>
            public static string MD5pdf(string path, string key)
            {
                            try
                {                    FileStream get_file = new FileStream("D:/Test.pdf", 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, 0, pdfFile.Length);//对Buffer中的字节内容算MD5
                    textBox1.Text = result;
                    result = MD5String(result + key);//这儿点的key相当于一个密钥,这样一般人就是知道使用MD5算法,但是若不知道这个字符串还是无法计算出正确的MD5                    byte[] md5 = System.Text.Encoding.ASCII.GetBytes(result);//将字符串转换成字节数组以便写人到文件中                    FileStream fsWrite = new FileStream("D:/Test.pdf", FileMode.Open, FileAccess.ReadWrite);
                    fsWrite.Write(pdfFile, 0, pdfFile.Length);//将pdf文件,MD5值 重新写入到文件中。
                    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="D:/Test.pdf"></param>
            /// <returns>是否加了标签或是否标签值与内容值一致</returns>
            public static bool Check(string path, string key)
            {
                try
                {
                    FileStream get_file = new FileStream("D:/Test.pdf", 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, 0, pdfFile.Length - 32);//对pdf文件除最后32位以外的字节计算MD5,这个32是因为标签位为32位。
                    textBox2.Text = result;
                    result = MD5String(result + key);                    string md5 = System.Text.Encoding.ASCII.GetString(pdfFile, pdfFile.Length - 32, 32);//读取pdf文件最后32位,其中保存的就是MD5值
                    return result == md5;
                    textBox3.Text = result;
                }
                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);            }
        }        
              
    }
}