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 } 以上代码不仅仅只适用于PDF文件,对于其他一些格式也可以用,这主要是取决于文件的格式规范。
|