有关MD5的一些代码,转自别人的程序,这里做个记号
from: http://www.cnblogs.com/studyzy/archive/2007/03/30/694179.html
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
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
18 string result = MD5Buffer(pdfFile, 0, pdfFile.Length );//对Buffer中的字节内容算MD5
19 result = MD5String(result +key);//这儿点的key相当于一个密钥,这样一般人就是知道使用MD5算法,但是若不知道这个字符串还是无法计算出正确的MD5
20
21 byte[] md5 = System.Text.Encoding.ASCII.GetBytes(result);//将字符串转换成字节数组以便写人到文件中
22
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
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
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
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
60 return false;
61
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
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
78 }
79 }
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
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
18 string result = MD5Buffer(pdfFile, 0, pdfFile.Length );//对Buffer中的字节内容算MD5
19 result = MD5String(result +key);//这儿点的key相当于一个密钥,这样一般人就是知道使用MD5算法,但是若不知道这个字符串还是无法计算出正确的MD5
20
21 byte[] md5 = System.Text.Encoding.ASCII.GetBytes(result);//将字符串转换成字节数组以便写人到文件中
22
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
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
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
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
60 return false;
61
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
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
78 }
79 }