FileEncryptTool.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using Y.Utils.DataUtils.EncryptUtils;
  7. namespace Y.Utils.IOUtils.FileUtils
  8. {
  9. public class FileEncryptTool
  10. {
  11. private static string FileTypeDesc = "Oreo.FileMan.EncryptFile";
  12. private static int FileHeadLength = 256;
  13. private static int FileBuffer = 1024 * 1024;
  14. /// <summary>
  15. /// 文件加密
  16. /// </summary>
  17. /// <param name="srcFile"></param>
  18. /// <param name="dstFile"></param>
  19. /// <param name="password"></param>
  20. /// <returns>
  21. /// 1:操作成功
  22. /// -11:要加密的文件不存在
  23. /// -12:加密后的目标文件已存在
  24. /// -404:未知错误,操作失败
  25. /// </returns>
  26. public static int Encrypt(string srcFile, string dstFile, string password)
  27. {
  28. DateTime begin = DateTime.Now;
  29. if (!File.Exists(srcFile)) return -11; //要加密的文件不存在
  30. if (File.Exists(dstFile)) return -12;//加密后的目标文件已存在
  31. string fmtPwd = AesTool.FmtPassword(password);
  32. string pwdMd5 = MD5Tool.Encrypt(MD5Tool.Encrypt(fmtPwd));
  33. string md5 = FileTool.GetMD5(srcFile);
  34. using (FileStream fsRead = new FileStream(srcFile, FileMode.Open))
  35. {
  36. using (FileStream fsWrite = new FileStream(dstFile, FileMode.Create))
  37. {
  38. try
  39. {
  40. //文件头部数据定义
  41. List<byte[]> headdata = new List<byte[]>()
  42. {
  43. Encoding.Default.GetBytes(FileTypeDesc),
  44. Encoding.Default.GetBytes(md5),
  45. Encoding.Default.GetBytes(AesTool.Encrypt(fmtPwd,AesTool.DefaultPassword)),
  46. Encoding.Default.GetBytes(pwdMd5),
  47. Encoding.Default.GetBytes(DateTime.Now.ToString())
  48. };
  49. //写入长度
  50. for (int i = 0; i < FileHeadLength; i++)
  51. {
  52. if (headdata.Count > i)
  53. {
  54. byte[] length = BitConverter.GetBytes(headdata[i].Length);
  55. fsWrite.Write(length, 0, length.Length);
  56. }
  57. else
  58. {
  59. byte[] length = BitConverter.GetBytes(0);
  60. fsWrite.Write(length, 0, length.Length);
  61. }
  62. }
  63. //写入数据
  64. for (int i = 0; i < headdata.Count; i++)
  65. {
  66. fsWrite.Write(headdata[i], 0, headdata[i].Length);
  67. }
  68. //写入文件源数据
  69. int readCount = 0;
  70. byte[] buffer = new byte[FileBuffer];
  71. while ((readCount = fsRead.Read(buffer, 0, buffer.Length)) > 0)
  72. {
  73. if (readCount != buffer.Length)
  74. {
  75. byte[] temp = new byte[readCount];
  76. Buffer.BlockCopy(buffer, 0, temp, 0, readCount);
  77. byte[] enbyte = AesTool.Encrypt(temp, fmtPwd);
  78. fsWrite.Write(enbyte, 0, enbyte.Length);
  79. }
  80. else
  81. {
  82. byte[] enbyte = AesTool.Encrypt(buffer, fmtPwd);
  83. fsWrite.Write(enbyte, 0, enbyte.Length);
  84. }
  85. }
  86. return 1;//操作成功
  87. }
  88. catch (Exception e) { }
  89. }
  90. //加密失败后,删除加密的文件
  91. try { File.Delete(dstFile); } catch (Exception e) { }
  92. }
  93. return -404;//未知错误,操作失败
  94. }
  95. /// <summary>
  96. /// 文件解密
  97. /// </summary>
  98. /// <param name="srcFile"></param>
  99. /// <param name="dstFile"></param>
  100. /// <param name="password"></param>
  101. /// <returns>
  102. /// 1:操作成功
  103. /// -11:要解密的文件不存在
  104. /// -12:解密后的目标文件已存在
  105. /// -90:解锁密码错误
  106. /// -404:未知错误,操作失败
  107. /// </returns>
  108. public static int Decrypt(string srcFile, string dstFile, string password)
  109. {
  110. if (!File.Exists(srcFile)) return -11;//要解密的文件不存在
  111. if (File.Exists(dstFile)) return -12;//解密后的目标文件已存在
  112. string fmtPwd = AesTool.FmtPassword(password);
  113. string pwdMd5 = MD5Tool.Encrypt(MD5Tool.Encrypt(fmtPwd));
  114. List<string> headdata = new List<string>();
  115. using (FileStream fsRead = new FileStream(srcFile, FileMode.Open))
  116. {
  117. using (FileStream fsWrite = new FileStream(dstFile, FileMode.Create))
  118. {
  119. try
  120. {
  121. byte[] headlength = new byte[4 * FileHeadLength];
  122. if (fsRead.Read(headlength, 0, headlength.Length) == headlength.Length)
  123. {
  124. for (int i = 0; i < FileHeadLength; i++)
  125. {
  126. int datalong = BitConverter.ToInt32(headlength, i * 4);
  127. byte[] tempdata = new byte[datalong];
  128. fsRead.Read(tempdata, 0, datalong);
  129. headdata.Add(Encoding.Default.GetString(tempdata));
  130. }
  131. }
  132. if (pwdMd5 != headdata[3]) return -90;//解锁密码错误
  133. int readCount = 0;
  134. byte[] buffer = new byte[FileBuffer + 16];
  135. while ((readCount = fsRead.Read(buffer, 0, buffer.Length)) > 0)
  136. {
  137. if (readCount != buffer.Length)
  138. {
  139. byte[] temp = new byte[readCount];
  140. Buffer.BlockCopy(buffer, 0, temp, 0, readCount);
  141. byte[] debyte = AesTool.Decrypt(temp, fmtPwd);
  142. fsWrite.Write(debyte, 0, debyte.Length);
  143. }
  144. else
  145. {
  146. byte[] debyte = AesTool.Decrypt(buffer, fmtPwd);
  147. fsWrite.Write(debyte, 0, debyte.Length);
  148. }
  149. }
  150. }
  151. catch (Exception e) { }
  152. }
  153. }
  154. string md5 = FileTool.GetMD5(dstFile);
  155. if (md5 == headdata[1])
  156. {
  157. return 1;//操作成功
  158. }
  159. else
  160. {
  161. //解密失败后,删除解密的文件
  162. try { File.Delete(dstFile); } catch (Exception e) { }
  163. }
  164. return -404;//未知错误,操作失败
  165. }
  166. }
  167. }