用C#实现pdf文件的完整性验证
2011-09-13 11:55 沐海 阅读(1212) 评论(0) 编辑 收藏 举报用C#实现pdf文件的完整性验证
现在对文件的完整性验证,防止文件被篡改的技术已经比较成熟,一般使用数字签名,数字水印等,最近我在一个项目中也遇到了防篡改的需求。该项目要求用户将原始发票用专门的扫描程序扫描成pdf文件,然后将该pdf文件传到服务器上,在上传的同时必须要验证这个pdf是没有被手工修改过的。我刚一接触到这个需求想到的就是使用数字水印,要不然就直接使用PDF的数字签名功能,不过这些方法都感觉比较比较复杂,一大堆的英文文档也没有心思去研究,于是琢磨了半天,写了一个简化版的数字水印程序,实现了pdf文件完整性验证。
验证的基本思路是:
对文件全部内容计算其MD5值,这样无论用户修改了文件的任何一个地方,那么生成的MD5的是完全不一样的,我们可以将这个MD5写到文件的一个隐藏区,一般二进制文件格式都有文件头和文件体部分,而文件头是用户看不到的,一般也会预留一部分字节用于以后扩展,或可以在文件头写入特殊标记的数据。于是研究了一下pdf文件的格式,试着往其第10个字节插入了MD5值,结果文件虽然可以使用,但是每次打开的时候都会提示“文件修复”。原来是写在头上面的内容将pdf文件的字节数和文件中对象的地址改变了,导致了文件错误,原因找到了那么解决办法也就有了,为了不改变pdf文件中对象的地址,那么我们将这个md5写在文件尾不就可以了嘛!于是在客户端(扫描程序)将扫描出的pdf文件流计算MD5值,然后将该文件流和MD5值一起写到硬盘上,形成一个添加了MD5值的pdf文件。文件可以正常打开和使用,而且用户也不会看到我们添加的这个MD5值。
在服务器端,我们将上传上来的文件流除了最后32个字节以为的部分计算MD5值(这儿取32个字节是因为最后这32字节是我们写的MD5),将前面部分算出的MD5和最后32个字节的MD5进行比较,如果一样那么说明这个文件从扫描程序生成以后没有被人为篡改过,否则说明该文件要么不是用我们这个扫描程序生成的要么就是被篡改了。这样验证通过以后我们才将该文件流写到服务器硬盘上。
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
public class MD5
2
{
3
/// <summary>
4
/// 对给定文件路径的文件加上标签
5
/// </summary>
6
/// <param name="path">要加密的文件的路径</param>
7
/// <returns>标签的值</returns>
8
public static string MD5pdf(string path,string key)
9
{
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
try
12
{
13
FileStream get_file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
14
byte[] pdfFile = new byte[get_file.Length];
15
get_file.Read(pdfFile, 0, (int)get_file.Length);//将文件流读取到Buffer中
16
get_file.Close();
17![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
string result = MD5Buffer(pdfFile, 0, pdfFile.Length );//对Buffer中的字节内容算MD5
19
result = MD5String(result +key);//这儿点的key相当于一个密钥,这样一般人就是知道使用MD5算法,但是若不知道这个字符串还是无法计算出正确的MD5
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
byte[] md5 = System.Text.Encoding.ASCII.GetBytes(result);//将字符串转换成字节数组以便写人到文件中
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
FileStream fsWrite = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
24
fsWrite.Write(pdfFile, 0, pdfFile.Length);//将pdf文件,MD5值 重新写入到文件中。
25
fsWrite.Write(md5, 0, md5.Length);
26
//fsWrite.Write(pdfFile, 10, pdfFile.Length - 10);
27
fsWrite.Close();
28![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
return result;
30
}
31
catch (Exception e)
32
{
33
return e.ToString();
34
}
35
}
36
/// <summary>
37
/// 对给定路径的文件进行验证
38
/// </summary>
39
/// <param name="path"></param>
40
/// <returns>是否加了标签或是否标签值与内容值一致</returns>
41
public static bool Check(string path,string key)
42
{
43
try
44
{
45
FileStream get_file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
46![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
48
byte[] pdfFile = new byte[get_file.Length];
49
get_file.Read(pdfFile, 0, (int)get_file.Length);
50
get_file.Close();
51
string result = MD5Buffer(pdfFile, 0, pdfFile.Length - 32);//对pdf文件除最后32位以外的字节计算MD5,这个32是因为标签位为32位。
52
result = MD5String(result + key);
53![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
string md5 = System.Text.Encoding.ASCII.GetString(pdfFile, pdfFile.Length - 32, 32);//读取pdf文件最后32位,其中保存的就是MD5值
55
return result == md5;
56
}
57
catch
58
{
59![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
return false;
61![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
}
63
}
64
private static string MD5Buffer(byte[] pdfFile, int index, int count)
65
{
66
System.Security.Cryptography.MD5CryptoServiceProvider get_md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
67
byte[] hash_byte = get_md5.ComputeHash(pdfFile, index, count);
68![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
string result = System.BitConverter.ToString(hash_byte);
70
result = result.Replace("-", "");
71
return result;
72
}
73
private static string MD5String(string str)
74
{
75
byte[] MD5Source = System.Text.Encoding.ASCII.GetBytes(str);
76
return MD5Buffer(MD5Source, 0, MD5Source.Length);
77![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
78
}
79
}
以上代码不仅仅只适用于PDF文件,对于其他一些格式也可以用,这主要是取决于文件的格式规范。关于PDF,官方有相关文档,不过大家觉得麻烦可以看看这篇文章:http://blog.csdn.net/pdfMaker/archive/2006/01/09/573990.aspx
验证的基本思路是:
对文件全部内容计算其MD5值,这样无论用户修改了文件的任何一个地方,那么生成的MD5的是完全不一样的,我们可以将这个MD5写到文件的一个隐藏区,一般二进制文件格式都有文件头和文件体部分,而文件头是用户看不到的,一般也会预留一部分字节用于以后扩展,或可以在文件头写入特殊标记的数据。于是研究了一下pdf文件的格式,试着往其第10个字节插入了MD5值,结果文件虽然可以使用,但是每次打开的时候都会提示“文件修复”。原来是写在头上面的内容将pdf文件的字节数和文件中对象的地址改变了,导致了文件错误,原因找到了那么解决办法也就有了,为了不改变pdf文件中对象的地址,那么我们将这个md5写在文件尾不就可以了嘛!于是在客户端(扫描程序)将扫描出的pdf文件流计算MD5值,然后将该文件流和MD5值一起写到硬盘上,形成一个添加了MD5值的pdf文件。文件可以正常打开和使用,而且用户也不会看到我们添加的这个MD5值。
在服务器端,我们将上传上来的文件流除了最后32个字节以为的部分计算MD5值(这儿取32个字节是因为最后这32字节是我们写的MD5),将前面部分算出的MD5和最后32个字节的MD5进行比较,如果一样那么说明这个文件从扫描程序生成以后没有被人为篡改过,否则说明该文件要么不是用我们这个扫描程序生成的要么就是被篡改了。这样验证通过以后我们才将该文件流写到服务器硬盘上。
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
73
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
74
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
75
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
77
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
78
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
79
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
以上代码不仅仅只适用于PDF文件,对于其他一些格式也可以用,这主要是取决于文件的格式规范。关于PDF,官方有相关文档,不过大家觉得麻烦可以看看这篇文章:http://blog.csdn.net/pdfMaker/archive/2006/01/09/573990.aspx
【出自博客园深蓝居,转载请注明作者出处】
本人声明:
个人主页:沐海(http://www.cnblogs.com/mahaisong)
以上文章都是经过本人设计实践和阅读其他文档得出。如果需要探讨或指教可以留言或加我QQ!欢迎交流!