Encrypt.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. namespace BaseLibRWFile
  6. {
  7. /// <summary>
  8. /// AES加密解密
  9. /// </summary>
  10. public class AES
  11. {
  12. #region 加密
  13. #region 加密字符串
  14. /// <summary>
  15. /// AES 加密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
  16. /// </summary>
  17. /// <param name="EncryptString">待加密密文</param>
  18. /// <param name="EncryptKey">加密密钥</param>
  19. public static string AESEncrypt(string EncryptString, string EncryptKey)
  20. {
  21. return Convert.ToBase64String(AESEncrypt(Encoding.Default.GetBytes(EncryptString), EncryptKey));
  22. }
  23. #endregion
  24. #region 加密字节数组
  25. /// <summary>
  26. /// AES 加密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
  27. /// </summary>
  28. /// <param name="EncryptString">待加密密文</param>
  29. /// <param name="EncryptKey">加密密钥</param>
  30. public static byte[] AESEncrypt(byte[] EncryptByte, string EncryptKey)
  31. {
  32. if (EncryptByte.Length == 0) { throw (new Exception("明文不得为空")); }
  33. if (string.IsNullOrEmpty(EncryptKey)) { throw (new Exception("密钥不得为空")); }
  34. byte[] m_strEncrypt;
  35. byte[] m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
  36. byte[] m_salt = Convert.FromBase64String("gsf4jvkyhye5/d7k8OrLgM==");
  37. Rijndael m_AESProvider = Rijndael.Create();
  38. try
  39. {
  40. MemoryStream m_stream = new MemoryStream();
  41. PasswordDeriveBytes pdb = new PasswordDeriveBytes(EncryptKey, m_salt);
  42. ICryptoTransform transform = m_AESProvider.CreateEncryptor(pdb.GetBytes(32), m_btIV);
  43. CryptoStream m_csstream = new CryptoStream(m_stream, transform, CryptoStreamMode.Write);
  44. m_csstream.Write(EncryptByte, 0, EncryptByte.Length);
  45. m_csstream.FlushFinalBlock();
  46. m_strEncrypt = m_stream.ToArray();
  47. m_stream.Close(); m_stream.Dispose();
  48. m_csstream.Close(); m_csstream.Dispose();
  49. }
  50. catch (IOException ex) { throw ex; }
  51. catch (CryptographicException ex) { throw ex; }
  52. catch (ArgumentException ex) { throw ex; }
  53. catch (Exception ex) { throw ex; }
  54. finally { m_AESProvider.Clear(); }
  55. return m_strEncrypt;
  56. }
  57. #endregion
  58. #endregion
  59. #region 解密
  60. #region 解密字符串
  61. /// <summary>
  62. /// AES 解密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
  63. /// </summary>
  64. /// <param name="DecryptString">待解密密文</param>
  65. /// <param name="DecryptKey">解密密钥</param>
  66. public static string AESDecrypt(string DecryptString, string DecryptKey)
  67. {
  68. return Convert.ToBase64String(AESDecrypt(Encoding.Default.GetBytes(DecryptString), DecryptKey));
  69. }
  70. #endregion
  71. #region 解密字节数组
  72. /// <summary>
  73. /// AES 解密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
  74. /// </summary>
  75. /// <param name="DecryptString">待解密密文</param>
  76. /// <param name="DecryptKey">解密密钥</param>
  77. public static byte[] AESDecrypt(byte[] DecryptByte, string DecryptKey)
  78. {
  79. if (DecryptByte.Length == 0) { throw (new Exception("密文不得为空")); }
  80. if (string.IsNullOrEmpty(DecryptKey)) { throw (new Exception("密钥不得为空")); }
  81. byte[] m_strDecrypt;
  82. byte[] m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
  83. byte[] m_salt = Convert.FromBase64String("gsf4jvkyhye5/d7k8OrLgM==");
  84. Rijndael m_AESProvider = Rijndael.Create();
  85. try
  86. {
  87. MemoryStream m_stream = new MemoryStream();
  88. PasswordDeriveBytes pdb = new PasswordDeriveBytes(DecryptKey, m_salt);
  89. ICryptoTransform transform = m_AESProvider.CreateDecryptor(pdb.GetBytes(32), m_btIV);
  90. CryptoStream m_csstream = new CryptoStream(m_stream, transform, CryptoStreamMode.Write);
  91. m_csstream.Write(DecryptByte, 0, DecryptByte.Length);
  92. m_csstream.FlushFinalBlock();
  93. m_strDecrypt = m_stream.ToArray();
  94. m_stream.Close(); m_stream.Dispose();
  95. m_csstream.Close(); m_csstream.Dispose();
  96. }
  97. catch (IOException ex) { throw ex; }
  98. catch (CryptographicException ex) { throw ex; }
  99. catch (ArgumentException ex) { throw ex; }
  100. catch (Exception ex) { throw ex; }
  101. finally { m_AESProvider.Clear(); }
  102. return m_strDecrypt;
  103. }
  104. #endregion
  105. #endregion
  106. }
  107. /// <summary>
  108. /// 文件加密类
  109. /// </summary>
  110. public class FileEncrypt
  111. {
  112. #region 变量
  113. /// <summary>
  114. /// 一次处理的明文字节数
  115. /// </summary>
  116. public static readonly int encryptSize = 10000000;
  117. /// <summary>
  118. /// 一次处理的密文字节数
  119. /// </summary>
  120. public static readonly int decryptSize = 10000016;
  121. #endregion
  122. #region 加密文件
  123. /// <summary>
  124. /// 加密文件
  125. /// </summary>
  126. public static void EncryptFile(string path, string pwd)
  127. {
  128. try
  129. {
  130. if (File.Exists(path + ".temp")) File.Delete(path + ".temp");
  131. using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
  132. {
  133. if (fs.Length > 0)
  134. {
  135. using (FileStream fsnew = new FileStream(path + ".temp", FileMode.OpenOrCreate, FileAccess.Write))
  136. {
  137. if (File.Exists(path + ".temp")) File.SetAttributes(path + ".temp", FileAttributes.Hidden);
  138. int blockCount = ((int)fs.Length - 1) / encryptSize + 1;
  139. for (int i = 0; i < blockCount; i++)
  140. {
  141. int size = encryptSize;
  142. if (i == blockCount - 1) size = (int)(fs.Length - i * encryptSize);
  143. byte[] bArr = new byte[size];
  144. fs.Read(bArr, 0, size);
  145. byte[] result = AES.AESEncrypt(bArr, pwd);
  146. fsnew.Write(result, 0, result.Length);
  147. fsnew.Flush();
  148. //refreshFileProgress(blockCount, i + 1); //更新进度
  149. }
  150. fsnew.Close();
  151. fsnew.Dispose();
  152. }
  153. fs.Close();
  154. fs.Dispose();
  155. FileAttributes fileAttr = File.GetAttributes(path);
  156. File.SetAttributes(path, FileAttributes.Archive);
  157. File.Delete(path);
  158. File.Move(path + ".temp", path);
  159. File.SetAttributes(path, fileAttr);
  160. }
  161. }
  162. }
  163. catch (Exception ex)
  164. {
  165. File.Delete(path + ".temp");
  166. throw ex;
  167. }
  168. }
  169. #endregion
  170. #region 解密文件
  171. /// <summary>
  172. /// 解密文件
  173. /// </summary>
  174. public static void DecryptFile(string path, string pwd)
  175. {
  176. try
  177. {
  178. if (File.Exists(path + ".temp")) File.Delete(path + ".temp");
  179. using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
  180. {
  181. if (fs.Length > 0)
  182. {
  183. using (FileStream fsnew = new FileStream(path + ".temp", FileMode.OpenOrCreate, FileAccess.Write))
  184. {
  185. if (File.Exists(path + ".temp")) File.SetAttributes(path + ".temp", FileAttributes.Hidden);
  186. int blockCount = ((int)fs.Length - 1) / decryptSize + 1;
  187. for (int i = 0; i < blockCount; i++)
  188. {
  189. int size = decryptSize;
  190. if (i == blockCount - 1) size = (int)(fs.Length - i * decryptSize);
  191. byte[] bArr = new byte[size];
  192. fs.Read(bArr, 0, size);
  193. byte[] result = AES.AESDecrypt(bArr, pwd);
  194. fsnew.Write(result, 0, result.Length);
  195. fsnew.Flush();
  196. //refreshFileProgress(blockCount, i + 1); //更新进度
  197. }
  198. fsnew.Close();
  199. fsnew.Dispose();
  200. }
  201. fs.Close();
  202. fs.Dispose();
  203. FileAttributes fileAttr = File.GetAttributes(path);
  204. File.SetAttributes(path, FileAttributes.Archive);
  205. File.Delete(path);
  206. File.Move(path + ".temp", path);
  207. File.SetAttributes(path, fileAttr);
  208. }
  209. }
  210. }
  211. catch (Exception ex)
  212. {
  213. File.Delete(path + ".temp");
  214. throw ex;
  215. }
  216. }
  217. #endregion
  218. }
  219. /// <summary>
  220. /// 更新文件加密进度
  221. /// </summary>
  222. public delegate void RefreshFileProgress(int max, int value);
  223. /// <summary>
  224. /// 文件夹加密类
  225. /// </summary>
  226. public class DirectoryEncrypt
  227. {
  228. #region 加密文件夹及其子文件夹中的所有文件
  229. /// <summary>
  230. /// 加密文件夹及其子文件夹中的所有文件
  231. /// </summary>
  232. public static void EncryptDirectory(string dirPath, string pwd, RefreshDirProgress refreshDirProgress, RefreshFileProgress refreshFileProgress)
  233. {
  234. string[] filePaths = Directory.GetFiles(dirPath, "*", SearchOption.AllDirectories);
  235. for (int i = 0; i < filePaths.Length; i++)
  236. {
  237. FileEncrypt.EncryptFile(filePaths[i], pwd);
  238. refreshDirProgress(filePaths.Length, i + 1);
  239. }
  240. }
  241. #endregion
  242. #region 解密文件夹及其子文件夹中的所有文件
  243. /// <summary>
  244. /// 解密文件夹及其子文件夹中的所有文件
  245. /// </summary>
  246. public static void DecryptDirectory(string dirPath, string pwd, RefreshDirProgress refreshDirProgress, RefreshFileProgress refreshFileProgress)
  247. {
  248. string[] filePaths = Directory.GetFiles(dirPath, "*", SearchOption.AllDirectories);
  249. for (int i = 0; i < filePaths.Length; i++)
  250. {
  251. FileEncrypt.DecryptFile(filePaths[i], pwd);
  252. refreshDirProgress(filePaths.Length, i + 1);
  253. }
  254. }
  255. #endregion
  256. }
  257. /// <summary>
  258. /// 更新文件夹加密进度
  259. /// </summary>
  260. public delegate void RefreshDirProgress(int max, int value);
  261. }