Encrypt.cs 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace Masuit.Tools.Security
  7. {
  8. /// <summary>
  9. /// 常用的加密解密算法
  10. /// </summary>
  11. public static class Encrypt
  12. {
  13. #region DES对称加密解密
  14. /// <summary>
  15. /// 加密字符串
  16. /// 加密密钥必须为8位
  17. /// </summary>
  18. /// <param name="strText">被加密的字符串</param>
  19. /// <param name="strEncrKey">8位长度密钥</param>
  20. /// <returns>加密后的数据</returns>
  21. public static string DesEncrypt(this string strText, string strEncrKey)
  22. {
  23. if (strEncrKey.Length < 8)
  24. {
  25. throw new Exception("密钥长度无效,密钥必须是8位!");
  26. }
  27. StringBuilder ret = new StringBuilder();
  28. using var des = DES.Create();
  29. byte[] inputByteArray = Encoding.Default.GetBytes(strText);
  30. des.Key = Encoding.ASCII.GetBytes(strEncrKey.Substring(0, 8));
  31. des.IV = Encoding.ASCII.GetBytes(strEncrKey.Substring(0, 8));
  32. MemoryStream ms = new MemoryStream();
  33. using var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  34. cs.Write(inputByteArray, 0, inputByteArray.Length);
  35. cs.FlushFinalBlock();
  36. foreach (byte b in ms.ToArray())
  37. {
  38. ret.AppendFormat($"{b:X2}");
  39. }
  40. return ret.ToString();
  41. }
  42. /// <summary>
  43. /// 加密字符串
  44. /// 加密密钥必须为8位
  45. /// </summary>
  46. /// <param name="strText">被加密的字符串</param>
  47. /// <param name="desKey"></param>
  48. /// <param name="desIV"></param>
  49. /// <returns>加密后的数据</returns>
  50. public static string DesEncrypt(this string strText, byte[] desKey, byte[] desIV)
  51. {
  52. StringBuilder ret = new StringBuilder();
  53. using var des = DES.Create();
  54. byte[] inputByteArray = Encoding.Default.GetBytes(strText);
  55. des.Key = desKey;
  56. des.IV = desIV;
  57. MemoryStream ms = new MemoryStream();
  58. using var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  59. cs.Write(inputByteArray, 0, inputByteArray.Length);
  60. cs.FlushFinalBlock();
  61. foreach (byte b in ms.ToArray())
  62. {
  63. ret.AppendFormat($"{b:X2}");
  64. }
  65. return ret.ToString();
  66. }
  67. /// <summary>
  68. /// DES加密文件
  69. /// </summary>
  70. /// <param name="fin">文件输入流</param>
  71. /// <param name="outFilePath">文件输出路径</param>
  72. /// <param name="strEncrKey">加密密钥</param>
  73. public static void DesEncrypt(this FileStream fin, string outFilePath, string strEncrKey)
  74. {
  75. byte[] iv =
  76. {
  77. 0x12,
  78. 0x34,
  79. 0x56,
  80. 0x78,
  81. 0x90,
  82. 0xAB,
  83. 0xCD,
  84. 0xEF
  85. };
  86. var byKey = Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
  87. using var fout = new FileStream(outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
  88. fout.SetLength(0);
  89. byte[] bin = new byte[100];
  90. long rdlen = 0;
  91. long totlen = fin.Length;
  92. using var des = DES.Create();
  93. var encStream = new CryptoStream(fout, des.CreateEncryptor(byKey, iv), CryptoStreamMode.Write);
  94. while (rdlen < totlen)
  95. {
  96. var len = fin.Read(bin, 0, 100);
  97. encStream.Write(bin, 0, len);
  98. rdlen += len;
  99. }
  100. }
  101. /// <summary>
  102. /// DES加密文件
  103. /// </summary>
  104. /// <param name="fin">文件输入流</param>
  105. /// <param name="outFilePath">文件输出路径</param>
  106. /// <param name="desKey"></param>
  107. /// <param name="desIV"></param>
  108. public static void DesEncrypt(this FileStream fin, string outFilePath, byte[] desKey, byte[] desIV)
  109. {
  110. using var fout = new FileStream(outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
  111. fout.SetLength(0);
  112. byte[] bin = new byte[100];
  113. long rdlen = 0;
  114. long totlen = fin.Length;
  115. using var des = DES.Create();
  116. var encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
  117. while (rdlen < totlen)
  118. {
  119. var len = fin.Read(bin, 0, 100);
  120. encStream.Write(bin, 0, len);
  121. rdlen += len;
  122. }
  123. }
  124. /// <summary>
  125. /// DES解密文件
  126. /// </summary>
  127. /// <param name="fin">输入文件流</param>
  128. /// <param name="outFilePath">文件输出路径</param>
  129. /// <param name="sDecrKey">解密密钥</param>
  130. public static void DesDecrypt(this FileStream fin, string outFilePath, string sDecrKey)
  131. {
  132. byte[] iv =
  133. {
  134. 0x12,
  135. 0x34,
  136. 0x56,
  137. 0x78,
  138. 0x90,
  139. 0xAB,
  140. 0xCD,
  141. 0xEF
  142. };
  143. var byKey = Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
  144. using var fout = new FileStream(outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
  145. fout.SetLength(0);
  146. byte[] bin = new byte[100];
  147. long rdlen = 0;
  148. long totlen = fin.Length;
  149. using var des = DES.Create();
  150. var encStream = new CryptoStream(fout, des.CreateDecryptor(byKey, iv), CryptoStreamMode.Write);
  151. while (rdlen < totlen)
  152. {
  153. var len = fin.Read(bin, 0, 100);
  154. encStream.Write(bin, 0, len);
  155. rdlen += len;
  156. }
  157. }
  158. /// <summary>
  159. /// DES解密文件
  160. /// </summary>
  161. /// <param name="fin">输入文件流</param>
  162. /// <param name="outFilePath">文件输出路径</param>
  163. /// <param name="desKey"></param>
  164. /// <param name="desIV"></param>
  165. public static void DesDecrypt(this FileStream fin, string outFilePath, byte[] desKey, byte[] desIV)
  166. {
  167. using var fout = new FileStream(outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
  168. fout.SetLength(0);
  169. byte[] bin = new byte[100];
  170. long rdlen = 0;
  171. long totlen = fin.Length;
  172. using var des = DES.Create();
  173. var encStream = new CryptoStream(fout, des.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write);
  174. while (rdlen < totlen)
  175. {
  176. var len = fin.Read(bin, 0, 100);
  177. encStream.Write(bin, 0, len);
  178. rdlen += len;
  179. }
  180. }
  181. /// <summary>
  182. /// DES解密算法
  183. /// 密钥为8位
  184. /// </summary>
  185. /// <param name="pToDecrypt">需要解密的字符串</param>
  186. /// <param name="sKey">密钥</param>
  187. /// <returns>解密后的数据</returns>
  188. public static string DesDecrypt(this string pToDecrypt, string sKey)
  189. {
  190. if (sKey.Length < 8)
  191. {
  192. throw new Exception("密钥长度无效,密钥必须是8位!");
  193. }
  194. var ms = new MemoryStream();
  195. using var des = DES.Create();
  196. var inputByteArray = new byte[pToDecrypt.Length / 2];
  197. for (int x = 0; x < pToDecrypt.Length / 2; x++)
  198. {
  199. int i = Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16);
  200. inputByteArray[x] = (byte)i;
  201. }
  202. des.Key = Encoding.ASCII.GetBytes(sKey.Substring(0, 8));
  203. des.IV = Encoding.ASCII.GetBytes(sKey.Substring(0, 8));
  204. using var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  205. cs.Write(inputByteArray, 0, inputByteArray.Length);
  206. cs.FlushFinalBlock();
  207. return Encoding.Default.GetString(ms.ToArray());
  208. }
  209. /// <summary>
  210. /// DES解密算法
  211. /// 密钥为8位
  212. /// </summary>
  213. /// <param name="pToDecrypt">需要解密的字符串</param>
  214. /// <param name="desKey"></param>
  215. /// <param name="desIV"></param>
  216. /// <returns>解密后的数据</returns>
  217. public static string DesDecrypt(this string pToDecrypt, byte[] desKey, byte[] desIV)
  218. {
  219. var ms = new MemoryStream();
  220. using var des = DES.Create();
  221. var inputByteArray = new byte[pToDecrypt.Length / 2];
  222. for (int x = 0; x < pToDecrypt.Length / 2; x++)
  223. {
  224. int i = Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16);
  225. inputByteArray[x] = (byte)i;
  226. }
  227. des.Key = desKey;
  228. des.IV = desIV;
  229. using var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  230. cs.Write(inputByteArray, 0, inputByteArray.Length);
  231. cs.FlushFinalBlock();
  232. return Encoding.Default.GetString(ms.ToArray());
  233. }
  234. #endregion
  235. #region 对称加密算法AES加密解密
  236. private static byte[] Keys = { 0x41, 0x72, 0x65, 0x79, 0x6F, 0x75, 0x6D, 0x79, 0x53, 0x6E, 0x6F, 0x77, 0x6D, 0x61, 0x6E, 0x3F };
  237. /// <summary>
  238. /// 生成符合AES加密规则的密钥
  239. /// </summary>
  240. /// <param name="length"></param>
  241. /// <returns></returns>
  242. public static string GenerateAesKey(int length)
  243. {
  244. using var aes = Aes.Create();
  245. aes.KeySize = length;
  246. aes.BlockSize = 128;
  247. aes.GenerateKey();
  248. return Convert.ToBase64String(aes.Key);
  249. }
  250. /// <summary>
  251. /// 对称加密算法AES(块式加密算法)
  252. /// </summary>
  253. /// <param name="encryptString">待加密字符串</param>
  254. /// <param name="encryptKey">加密密钥,须半角字符</param>
  255. /// <param name="mode">加密模式</param>
  256. /// <returns>加密结果字符串</returns>
  257. public static string AESEncrypt(this string encryptString, string encryptKey, CipherMode mode = CipherMode.CBC)
  258. {
  259. encryptKey = GetSubString(encryptKey, 32, "");
  260. encryptKey = encryptKey.PadRight(32, ' ');
  261. using var aes = Aes.Create("AesManaged");
  262. aes.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 32));
  263. aes.IV = Keys;
  264. aes.Mode = mode;
  265. using ICryptoTransform rijndaelEncrypt = aes.CreateEncryptor();
  266. byte[] inputData = Encoding.UTF8.GetBytes(encryptString);
  267. byte[] encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData, 0, inputData.Length);
  268. return Convert.ToBase64String(encryptedData);
  269. }
  270. /// <summary>
  271. /// 对称加密算法AES加密(块式加密算法)
  272. /// </summary>
  273. /// <param name="encryptString">待加密字符串</param>
  274. /// <param name="options">加密选项</param>
  275. /// <returns>加密结果字符串</returns>
  276. public static string AESEncrypt(this string encryptString, Aes options)
  277. {
  278. using ICryptoTransform rijndaelEncrypt = options.CreateEncryptor();
  279. byte[] inputData = Encoding.UTF8.GetBytes(encryptString);
  280. byte[] encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData, 0, inputData.Length);
  281. return Convert.ToBase64String(encryptedData);
  282. }
  283. /// <summary>
  284. /// 对称加密算法AES加密(块式加密算法)
  285. /// </summary>
  286. /// <param name="encryptString">待加密字符串</param>
  287. /// <param name="encryptKey">加密密钥,须半角字符</param>
  288. /// <param name="mode">加密模式</param>
  289. /// <returns>加密结果字符串</returns>
  290. public static string AESEncrypt(this string encryptString, byte[] encryptKey, CipherMode mode = CipherMode.CBC)
  291. {
  292. using var aes = Aes.Create("AesManaged");
  293. aes.Key = encryptKey;
  294. aes.IV = Keys;
  295. aes.Mode = mode;
  296. using ICryptoTransform rijndaelEncrypt = aes.CreateEncryptor();
  297. byte[] inputData = Encoding.UTF8.GetBytes(encryptString);
  298. byte[] encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData, 0, inputData.Length);
  299. return Convert.ToBase64String(encryptedData);
  300. }
  301. /// <summary>
  302. /// 对称加密算法AES解密字符串
  303. /// </summary>
  304. /// <param name="decryptString">待解密的字符串</param>
  305. /// <param name="decryptKey">解密密钥,和加密密钥相同</param>
  306. /// <param name="mode">加密模式</param>
  307. /// <returns>解密成功返回解密后的字符串,失败返回空</returns>
  308. public static string AESDecrypt(this string decryptString, string decryptKey, CipherMode mode = CipherMode.CBC)
  309. {
  310. try
  311. {
  312. decryptKey = GetSubString(decryptKey, 32, "");
  313. decryptKey = decryptKey.PadRight(32, ' ');
  314. using var aes = Aes.Create("AesManaged");
  315. aes.Key = Encoding.UTF8.GetBytes(decryptKey);
  316. aes.IV = Keys;
  317. aes.Mode = mode;
  318. using ICryptoTransform rijndaelDecrypt = aes.CreateDecryptor();
  319. byte[] inputData = Convert.FromBase64String(decryptString);
  320. byte[] decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData, 0, inputData.Length);
  321. return Encoding.UTF8.GetString(decryptedData);
  322. }
  323. catch
  324. {
  325. return string.Empty;
  326. }
  327. }
  328. /// <summary>
  329. /// 对称加密算法AES解密字符串
  330. /// </summary>
  331. /// <param name="decryptString">待解密的字符串</param>
  332. /// <param name="options">加密选项</param>
  333. /// <returns>解密成功返回解密后的字符串,失败返回空</returns>
  334. public static string AESDecrypt(this string decryptString, Aes options)
  335. {
  336. try
  337. {
  338. using ICryptoTransform rijndaelDecrypt = options.CreateDecryptor();
  339. byte[] inputData = Convert.FromBase64String(decryptString);
  340. byte[] decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData, 0, inputData.Length);
  341. return Encoding.UTF8.GetString(decryptedData);
  342. }
  343. catch
  344. {
  345. return string.Empty;
  346. }
  347. }
  348. /// <summary>
  349. /// 对称加密算法AES解密字符串
  350. /// </summary>
  351. /// <param name="decryptString">待解密的字符串</param>
  352. /// <param name="decryptKey">解密密钥,和加密密钥相同</param>
  353. /// <param name="mode">加密模式</param>
  354. /// <returns>解密成功返回解密后的字符串,失败返回空</returns>
  355. public static string AESDecrypt(this string decryptString, byte[] decryptKey, CipherMode mode = CipherMode.CBC)
  356. {
  357. try
  358. {
  359. using var aes = Aes.Create("AesManaged");
  360. aes.Key = decryptKey;
  361. aes.IV = Keys;
  362. aes.Mode = mode;
  363. using ICryptoTransform rijndaelDecrypt = aes.CreateDecryptor();
  364. byte[] inputData = Convert.FromBase64String(decryptString);
  365. byte[] decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData, 0, inputData.Length);
  366. return Encoding.UTF8.GetString(decryptedData);
  367. }
  368. catch
  369. {
  370. return string.Empty;
  371. }
  372. }
  373. /// <summary>
  374. /// 按字节长度(按字节,一个汉字为2个字节)取得某字符串的一部分
  375. /// </summary>
  376. /// <param name="sourceString">源字符串</param>
  377. /// <param name="length">所取字符串字节长度</param>
  378. /// <param name="tailString">附加字符串(当字符串不够长时,尾部所添加的字符串,一般为"...")</param>
  379. /// <returns>某字符串的一部分</returns>
  380. private static string GetSubString(this string sourceString, int length, string tailString)
  381. {
  382. return GetSubString(sourceString, 0, length, tailString);
  383. }
  384. /// <summary>
  385. /// 按字节长度(按字节,一个汉字为2个字节)取得某字符串的一部分
  386. /// </summary>
  387. /// <param name="sourceString">源字符串</param>
  388. /// <param name="startIndex">索引位置,以0开始</param>
  389. /// <param name="length">所取字符串字节长度</param>
  390. /// <param name="tailString">附加字符串(当字符串不够长时,尾部所添加的字符串,一般为"...")</param>
  391. /// <returns>某字符串的一部分</returns>
  392. private static string GetSubString(this string sourceString, int startIndex, int length, string tailString)
  393. {
  394. //当是日文或韩文时(注:中文的范围:\u4e00 - \u9fa5, 日文在\u0800 - \u4e00, 韩文为\xAC00-\xD7A3)
  395. if (Regex.IsMatch(sourceString, "[\u0800-\u4e00]+") || Regex.IsMatch(sourceString, "[\xAC00-\xD7A3]+"))
  396. {
  397. //当截取的起始位置超出字段串长度时
  398. if (startIndex >= sourceString.Length)
  399. {
  400. return string.Empty;
  401. }
  402. return sourceString.Substring(startIndex, length + startIndex > sourceString.Length ? (sourceString.Length - startIndex) : length);
  403. }
  404. //中文字符,如"中国人民abcd123"
  405. if (length <= 0)
  406. {
  407. return string.Empty;
  408. }
  409. byte[] bytesSource = Encoding.Default.GetBytes(sourceString);
  410. //当字符串长度大于起始位置
  411. if (bytesSource.Length > startIndex)
  412. {
  413. int endIndex = bytesSource.Length;
  414. //当要截取的长度在字符串的有效长度范围内
  415. if (bytesSource.Length > (startIndex + length))
  416. {
  417. endIndex = length + startIndex;
  418. }
  419. else
  420. {
  421. //当不在有效范围内时,只取到字符串的结尾
  422. length = bytesSource.Length - startIndex;
  423. tailString = "";
  424. }
  425. var anResultFlag = new int[length];
  426. int nFlag = 0;
  427. //字节大于127为双字节字符
  428. for (int i = startIndex; i < endIndex; i++)
  429. {
  430. if (bytesSource[i] > 127)
  431. {
  432. nFlag++;
  433. if (nFlag == 3)
  434. {
  435. nFlag = 1;
  436. }
  437. }
  438. else
  439. {
  440. nFlag = 0;
  441. }
  442. anResultFlag[i] = nFlag;
  443. }
  444. //最后一个字节为双字节字符的一半
  445. if ((bytesSource[endIndex - 1] > 127) && (anResultFlag[length - 1] == 1))
  446. {
  447. length++;
  448. }
  449. byte[] bsResult = new byte[length];
  450. Array.Copy(bytesSource, startIndex, bsResult, 0, length);
  451. var myResult = Encoding.Default.GetString(bsResult);
  452. myResult += tailString;
  453. return myResult;
  454. }
  455. return string.Empty;
  456. }
  457. /// <summary>
  458. /// 加密文件流
  459. /// </summary>
  460. /// <param name="fs">需要加密的文件流</param>
  461. /// <param name="decryptKey">加密密钥</param>
  462. /// <param name="mode">加密模式</param>
  463. /// <returns>加密流</returns>
  464. public static CryptoStream AESEncryptStrream(this FileStream fs, string decryptKey, CipherMode mode = CipherMode.CBC)
  465. {
  466. decryptKey = GetSubString(decryptKey, 32, "");
  467. decryptKey = decryptKey.PadRight(32, ' ');
  468. using var aes = Aes.Create("AesManaged");
  469. aes.Key = Encoding.UTF8.GetBytes(decryptKey);
  470. aes.IV = Keys;
  471. aes.Mode = mode;
  472. using var encrypto = aes.CreateEncryptor();
  473. return new CryptoStream(fs, encrypto, CryptoStreamMode.Write);
  474. }
  475. /// <summary>
  476. /// 加密文件流
  477. /// </summary>
  478. /// <param name="fs">需要加密的文件流</param>
  479. /// <param name="decryptKey">加密密钥</param>
  480. /// <param name="mode">加密模式</param>
  481. /// <returns>加密流</returns>
  482. public static CryptoStream AESEncryptStrream(this FileStream fs, byte[] decryptKey, CipherMode mode = CipherMode.CBC)
  483. {
  484. using var aes = Aes.Create("AesManaged");
  485. aes.Key = decryptKey;
  486. aes.IV = Keys;
  487. aes.Mode = mode;
  488. using var encrypto = aes.CreateEncryptor();
  489. return new CryptoStream(fs, encrypto, CryptoStreamMode.Write);
  490. }
  491. /// <summary>
  492. /// 解密文件流
  493. /// </summary>
  494. /// <param name="fs">需要解密的文件流</param>
  495. /// <param name="decryptKey">解密密钥</param>
  496. /// <param name="mode">加密模式</param>
  497. /// <returns>加密流</returns>
  498. public static CryptoStream AESDecryptStream(this FileStream fs, string decryptKey, CipherMode mode = CipherMode.CBC)
  499. {
  500. decryptKey = GetSubString(decryptKey, 32, "");
  501. decryptKey = decryptKey.PadRight(32, ' ');
  502. using var aes = Aes.Create("AesManaged");
  503. aes.Key = Encoding.UTF8.GetBytes(decryptKey);
  504. aes.IV = Keys;
  505. aes.Mode = mode;
  506. using var decrypto = aes.CreateDecryptor();
  507. return new CryptoStream(fs, decrypto, CryptoStreamMode.Read);
  508. }
  509. /// <summary>
  510. /// 解密文件流
  511. /// </summary>
  512. /// <param name="fs">需要解密的文件流</param>
  513. /// <param name="decryptKey">解密密钥</param>
  514. /// <param name="mode">加密模式</param>
  515. /// <returns>加密流</returns>
  516. public static CryptoStream AESDecryptStream(this FileStream fs, byte[] decryptKey, CipherMode mode = CipherMode.CBC)
  517. {
  518. using var aes = Aes.Create("AesManaged");
  519. aes.Key = decryptKey;
  520. aes.IV = Keys;
  521. aes.Mode = mode;
  522. using var decrypto = aes.CreateDecryptor();
  523. return new CryptoStream(fs, decrypto, CryptoStreamMode.Read);
  524. }
  525. /// <summary>
  526. /// 对指定文件AES加密
  527. /// </summary>
  528. /// <param name="input">源文件流</param>
  529. /// <param name="key">加密密钥</param>
  530. /// <param name="mode">加密模式</param>
  531. /// <param name="outputPath">输出文件路径</param>
  532. public static void AESEncryptFile(this FileStream input, string outputPath, string key, CipherMode mode = CipherMode.CBC)
  533. {
  534. using var fren = new FileStream(outputPath, FileMode.Create);
  535. using var enfr = AESEncryptStrream(fren, key, mode);
  536. byte[] bytearrayinput = new byte[input.Length];
  537. _ = input.Read(bytearrayinput, 0, bytearrayinput.Length);
  538. enfr.Write(bytearrayinput, 0, bytearrayinput.Length);
  539. }
  540. /// <summary>
  541. /// 对指定的文件AES解密
  542. /// </summary>
  543. /// <param name="input">源文件流</param>
  544. /// <param name="key">解密密钥</param>
  545. /// <param name="mode">加密模式</param>
  546. /// <param name="outputPath">输出文件路径</param>
  547. public static void AESDecryptFile(this FileStream input, string outputPath, string key, CipherMode mode = CipherMode.CBC)
  548. {
  549. using FileStream frde = new FileStream(outputPath, FileMode.Create);
  550. using CryptoStream defr = AESDecryptStream(input, key, mode);
  551. byte[] bytearrayoutput = new byte[1024];
  552. while (true)
  553. {
  554. var count = defr.Read(bytearrayoutput, 0, bytearrayoutput.Length);
  555. frde.Write(bytearrayoutput, 0, count);
  556. if (count < bytearrayoutput.Length)
  557. {
  558. break;
  559. }
  560. }
  561. }
  562. #endregion
  563. #region Base64加密解密
  564. /// <summary>
  565. /// Base64加密
  566. /// </summary>
  567. /// <param name="str">需要加密的字符串</param>
  568. /// <returns>加密后的数据</returns>
  569. public static string Base64Encrypt(this string str)
  570. {
  571. byte[] encbuff = Encoding.UTF8.GetBytes(str);
  572. return Convert.ToBase64String(encbuff);
  573. }
  574. /// <summary>
  575. /// Base64解密
  576. /// </summary>
  577. /// <param name="str">需要解密的字符串</param>
  578. /// <returns>解密后的数据</returns>
  579. public static string Base64Decrypt(this string str)
  580. {
  581. try
  582. {
  583. byte[] decbuff = Convert.FromBase64String(str);
  584. return Encoding.UTF8.GetString(decbuff);
  585. }
  586. catch
  587. {
  588. return str;
  589. }
  590. }
  591. #endregion
  592. /// <summary>
  593. /// SHA256函数
  594. /// </summary>
  595. /// <param name="str">原始字符串</param>
  596. /// <returns>SHA256结果(返回长度为44字节的字符串)</returns>
  597. public static string SHA256(this string str)
  598. {
  599. byte[] sha256Data = Encoding.UTF8.GetBytes(str);
  600. using var sha256 = System.Security.Cryptography.SHA256.Create();
  601. byte[] result = sha256.ComputeHash(sha256Data);
  602. return Convert.ToBase64String(result); //返回长度为44字节的字符串
  603. }
  604. #region MD5摘要算法
  605. #region 对字符串进行MD5摘要
  606. /// <summary>
  607. /// 对字符串进行MD5摘要
  608. /// </summary>
  609. /// <param name="message">需要摘要的字符串</param>
  610. /// <returns>MD5摘要字符串</returns>
  611. public static string MDString(this string message)
  612. {
  613. MD5 md5 = MD5.Create();
  614. byte[] buffer = Encoding.Default.GetBytes(message);
  615. byte[] bytes = md5.ComputeHash(buffer);
  616. return GetHexString(bytes);
  617. }
  618. /// <summary>
  619. /// 对字符串进行MD5二次摘要
  620. /// </summary>
  621. /// <param name="message">需要摘要的字符串</param>
  622. /// <returns>MD5摘要字符串</returns>
  623. public static string MDString2(this string message) => MDString(MDString(message));
  624. /// <summary>
  625. /// MD5 三次摘要算法
  626. /// </summary>
  627. /// <param name="s">需要摘要的字符串</param>
  628. /// <returns>MD5摘要字符串</returns>
  629. public static string MDString3(this string s)
  630. {
  631. using MD5 md5 = MD5.Create();
  632. byte[] bytes = Encoding.ASCII.GetBytes(s);
  633. byte[] bytes1 = md5.ComputeHash(bytes);
  634. byte[] bytes2 = md5.ComputeHash(bytes1);
  635. byte[] bytes3 = md5.ComputeHash(bytes2);
  636. return GetHexString(bytes3);
  637. }
  638. /// <summary>
  639. /// 对字符串进行MD5加盐摘要
  640. /// </summary>
  641. /// <param name="message">需要摘要的字符串</param>
  642. /// <param name="salt">盐</param>
  643. /// <returns>MD5摘要字符串</returns>
  644. public static string MDString(this string message, string salt) => MDString(message + salt);
  645. /// <summary>
  646. /// 对字符串进行MD5二次加盐摘要
  647. /// </summary>
  648. /// <param name="message">需要摘要的字符串</param>
  649. /// <param name="salt">盐</param>
  650. /// <returns>MD5摘要字符串</returns>
  651. public static string MDString2(this string message, string salt) => MDString(MDString(message + salt), salt);
  652. /// <summary>
  653. /// MD5 三次摘要算法
  654. /// </summary>
  655. /// <param name="s">需要摘要的字符串</param>
  656. /// <param name="salt">盐</param>
  657. /// <returns>MD5摘要字符串</returns>
  658. public static string MDString3(this string s, string salt)
  659. {
  660. using MD5 md5 = MD5.Create();
  661. byte[] bytes = Encoding.ASCII.GetBytes(s + salt);
  662. byte[] bytes1 = md5.ComputeHash(bytes);
  663. byte[] bytes2 = md5.ComputeHash(bytes1);
  664. byte[] bytes3 = md5.ComputeHash(bytes2);
  665. return GetHexString(bytes3);
  666. }
  667. #endregion
  668. #region 获取文件的MD5值
  669. /// <summary>
  670. /// 获取文件的MD5值
  671. /// </summary>
  672. /// <param name="fileName">需要求MD5值的文件的文件名及路径</param>
  673. /// <returns>MD5摘要字符串</returns>
  674. public static string MDFile(this string fileName)
  675. {
  676. using var fs = new BufferedStream(File.Open(fileName, FileMode.Open, FileAccess.Read), 1048576);
  677. using MD5 md5 = MD5.Create();
  678. byte[] bytes = md5.ComputeHash(fs);
  679. return GetHexString(bytes);
  680. }
  681. /// <summary>
  682. /// 计算文件的sha256
  683. /// </summary>
  684. /// <param name="stream"></param>
  685. /// <returns></returns>
  686. public static string SHA256(this Stream stream)
  687. {
  688. using var fs = new BufferedStream(stream, 1048576);
  689. using var sha = System.Security.Cryptography.SHA256.Create();
  690. byte[] checksum = sha.ComputeHash(fs);
  691. return BitConverter.ToString(checksum).Replace("-", string.Empty);
  692. }
  693. /// <summary>
  694. /// 获取数据流的MD5摘要值
  695. /// </summary>
  696. /// <param name="stream"></param>
  697. /// <returns>MD5摘要字符串</returns>
  698. public static string MDString(this Stream stream)
  699. {
  700. using var fs = new BufferedStream(stream, 1048576);
  701. using MD5 md5 = MD5.Create();
  702. byte[] bytes = md5.ComputeHash(fs);
  703. var mdstr = GetHexString(bytes);
  704. stream.Position = 0;
  705. return mdstr;
  706. }
  707. public static string GetHexString(byte[] bytes)
  708. {
  709. var hexArray = new char[bytes.Length << 1];
  710. for (var i = 0; i < hexArray.Length; i += 2)
  711. {
  712. var b = bytes[i >> 1];
  713. hexArray[i] = GetHexValue(b >> 4); // b / 16
  714. hexArray[i + 1] = GetHexValue(b & 0xF); // b % 16
  715. }
  716. return new string(hexArray, 0, hexArray.Length);
  717. static char GetHexValue(int i)
  718. {
  719. if (i < 10)
  720. {
  721. return (char)(i + '0');
  722. }
  723. return (char)(i - 10 + 'a');
  724. }
  725. }
  726. #endregion
  727. #endregion MD5摘要算法
  728. }
  729. /// <summary>
  730. /// RC2加密解密算法
  731. /// </summary>
  732. public static class RC2Crypt
  733. {
  734. private static ASCIIEncoding _asciiEncoding;
  735. private static byte[] _iv;
  736. private static byte[] _key;
  737. private static RC2 _rc2Csp;
  738. private static UnicodeEncoding _textConverter;
  739. static RC2Crypt()
  740. {
  741. InitializeComponent();
  742. }
  743. private static void InitializeComponent()
  744. {
  745. _key = new byte[]
  746. {
  747. 106,
  748. 51,
  749. 25,
  750. 141,
  751. 157,
  752. 142,
  753. 23,
  754. 111,
  755. 234,
  756. 159,
  757. 187,
  758. 154,
  759. 215,
  760. 34,
  761. 37,
  762. 204
  763. };
  764. _iv = new byte[]
  765. {
  766. 135,
  767. 186,
  768. 133,
  769. 136,
  770. 184,
  771. 149,
  772. 153,
  773. 144
  774. };
  775. _asciiEncoding = new ASCIIEncoding();
  776. _textConverter = new UnicodeEncoding();
  777. _rc2Csp = RC2.Create();
  778. }
  779. #region 将文本数据加密后写入一个文件
  780. /// <summary>
  781. /// 将文本数据加密后写入一个文件,其中,这个文件是用InitBinFile建立的,这个文件将被分成十块,
  782. /// 用来分别保存10组不同的数据,第一个byte位保留,第2位到第21位分别用来存放每块数据的长度,但
  783. /// 一个byte的取值为0-127,所以,用两个byte来存放一个长度。
  784. /// </summary>
  785. /// <param name="toEncryptText">要加密的文本数据</param>
  786. /// <param name="filePath">要写入的文件</param>
  787. /// <param name="dataIndex">写入第几块,取值为1--10</param>
  788. /// <returns>是否操作成功</returns>
  789. public static bool EncryptToFile(this string toEncryptText, string filePath, int dataIndex)
  790. {
  791. var r = false;
  792. if ((dataIndex > 10) && (dataIndex < 1))
  793. {
  794. return r;
  795. }
  796. //打开要写入的文件,主要是为了保持原文件的内容不丢失
  797. var tmpFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, 1024, true);
  798. var index = new byte[10261];
  799. //将读取的内容写到byte数组
  800. tmpFileStream.Read(index, 0, 10261);
  801. tmpFileStream.Close();
  802. //定义基本的加密转换运算
  803. using var encryptor = _rc2Csp.CreateEncryptor(_key, _iv);
  804. var msEncrypt = new MemoryStream();
  805. //在此加密转换流中,加密将从csEncrypt,加密后,结果在msEncrypt流中。
  806. using var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
  807. //将要加密的文本转换成UTF-16 编码,保存在tmp数组。
  808. var tmp = _textConverter.GetBytes(toEncryptText);
  809. //将tmp输入csEncrypt,将通过Encryptor来加密。
  810. csEncrypt.Write(tmp, 0, tmp.Length);
  811. //输出到msEnctypt
  812. csEncrypt.FlushFinalBlock();
  813. //将流转成byte[]
  814. var encrypted = msEncrypt.ToArray();
  815. if (encrypted.Length > 1024)
  816. return false;
  817. //得到加密后数据的大小,将结果存在指定的位置。
  818. index[dataIndex * 2 - 1] = Convert.ToByte(Convert.ToString(encrypted.Length / 128));
  819. index[dataIndex * 2] = Convert.ToByte(Convert.ToString(encrypted.Length % 128));
  820. //将加密后的结果写入index(覆盖)
  821. for (var i = 0; i < encrypted.Length; i++)
  822. index[1024 * (dataIndex - 1) + 21 + i] = encrypted[i];
  823. //建立文件流
  824. tmpFileStream = new FileStream(filePath, FileMode.Truncate, FileAccess.Write, FileShare.None, 1024, true);
  825. //写文件
  826. tmpFileStream.Write(index, 0, 10261);
  827. tmpFileStream.Flush();
  828. r = true;
  829. tmpFileStream.Close();
  830. return r;
  831. }
  832. #endregion
  833. #region 从一个文件中解密出一段文本,其中,这个文件是由InitBinFile建立的,并且由 EncryptToFile加密的
  834. /// <summary>
  835. /// 从一个文件中解密出一段文本,其中,这个文件是由InitBinFile建立的,并且由 EncryptToFile加密的
  836. /// </summary>
  837. /// <param name="filePath">要解密的文件</param>
  838. /// <param name="dataIndex">要从哪一个块中解密</param>
  839. /// <returns>解密后的文本</returns>
  840. public static string DecryptFromFile(this string filePath, int dataIndex)
  841. {
  842. if (dataIndex > 10 && dataIndex < 1)
  843. {
  844. return "";
  845. }
  846. using var tmpFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, 1024, true);
  847. using var decryptor = _rc2Csp.CreateDecryptor(_key, _iv);
  848. var msDecrypt = new MemoryStream();
  849. using var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write);
  850. var index = new byte[10261];
  851. tmpFileStream.Read(index, 0, 10261);
  852. var count = index[dataIndex * 2 - 1] * 128 + index[dataIndex * 2];
  853. var tmp = new byte[count];
  854. Array.Copy(index, 1024 * (dataIndex - 1) + 21, tmp, 0, count);
  855. csDecrypt.Write(tmp, 0, count);
  856. csDecrypt.FlushFinalBlock();
  857. var decrypted = msDecrypt.ToArray();
  858. return _textConverter.GetString(decrypted, 0, decrypted.Length);
  859. }
  860. #endregion
  861. #region 将一段文本加密后保存到一个文件
  862. /// <summary>
  863. /// 将一段文本加密后保存到一个文件
  864. /// </summary>
  865. /// <param name="toEncryptText">要加密的文本数据</param>
  866. /// <param name="filePath">要保存的文件</param>
  867. /// <returns>是否加密成功</returns>
  868. public static void EncryptToFile(this string toEncryptText, string filePath)
  869. {
  870. using var tmpFileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, 1024, true);
  871. using var encryptor = _rc2Csp.CreateEncryptor(_key, _iv);
  872. var msEncrypt = new MemoryStream();
  873. using var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
  874. var tmp = _textConverter.GetBytes(toEncryptText);
  875. csEncrypt.Write(tmp, 0, tmp.Length);
  876. csEncrypt.FlushFinalBlock();
  877. var encrypted = msEncrypt.ToArray();
  878. tmpFileStream.Write(encrypted, 0, encrypted.Length);
  879. }
  880. #endregion
  881. #region 将一个被加密的文件解密
  882. /// <summary>
  883. /// 将一个被加密的文件解密
  884. /// </summary>
  885. /// <param name="filePath">要解密的文件</param>
  886. /// <returns>解密后的文本</returns>
  887. public static string DecryptFromFile(this string filePath)
  888. {
  889. using var tmpFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, 1024, true);
  890. using var decryptor = _rc2Csp.CreateDecryptor(_key, _iv);
  891. var msDecrypt = new MemoryStream();
  892. using var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write);
  893. var tmp = new byte[tmpFileStream.Length];
  894. tmpFileStream.Read(tmp, 0, tmp.Length);
  895. csDecrypt.Write(tmp, 0, tmp.Length);
  896. csDecrypt.FlushFinalBlock();
  897. var decrypted = msDecrypt.ToArray();
  898. return _textConverter.GetString(decrypted, 0, decrypted.Length);
  899. }
  900. #endregion
  901. #region 将文本数据加密后写入一个文件
  902. /// <summary>
  903. /// 将文本数据加密后写入一个文件,其中,这个文件是用InitBinFile建立的,这个文件将被分成十块,
  904. /// 用来分别保存10组不同的数据,第一个byte位保留,第2位到第21位分别用来存放每块数据的长度,但
  905. /// 一个byte的取值为0-127,所以,用两个byte来存放一个长度。
  906. /// </summary>
  907. /// <param name="toEncryptText">要加密的文本数据</param>
  908. /// <param name="filePath">要写入的文件</param>
  909. /// <param name="dataIndex">写入第几块,取值为1--10</param>
  910. /// <param name="IV">初始化向量</param>
  911. /// <param name="Key">加密密匙</param>
  912. /// <returns>是否操作成功</returns>
  913. public static void EncryptToFile(this string toEncryptText, string filePath, int dataIndex, byte[] IV, byte[] Key)
  914. {
  915. if ((dataIndex > 10) && (dataIndex < 1))
  916. {
  917. return;
  918. }
  919. //打开要写入的文件,主要是为了保持原文件的内容不丢失
  920. using var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, 1024, true);
  921. var index = new byte[10261];
  922. //将读取的内容写到byte数组
  923. fs.Read(index, 0, 10261);
  924. //定义基本的加密转换运算
  925. using var encryptor = _rc2Csp.CreateEncryptor(Key, IV);
  926. var msEncrypt = new MemoryStream();
  927. //在此加密转换流中,加密将从csEncrypt,加密后,结果在msEncrypt流中。
  928. using var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
  929. var tmp = _textConverter.GetBytes(toEncryptText);
  930. //将tmp输入csEncrypt,将通过Encryptor来加密。
  931. csEncrypt.Write(tmp, 0, tmp.Length);
  932. //输出到msEnctypt
  933. csEncrypt.FlushFinalBlock();
  934. //将流转成byte[]
  935. var encrypted = msEncrypt.ToArray();
  936. if (encrypted.Length > 1024)
  937. {
  938. return;
  939. }
  940. //得到加密后数据的大小,将结果存在指定的位置。
  941. index[dataIndex * 2 - 1] = Convert.ToByte(Convert.ToString(encrypted.Length / 128));
  942. index[dataIndex * 2] = Convert.ToByte(Convert.ToString(encrypted.Length % 128));
  943. //将加密后的结果写入index(覆盖)
  944. for (int i = 0; i < encrypted.Length; i++)
  945. {
  946. index[1024 * (dataIndex - 1) + 21 + i] = encrypted[i];
  947. }
  948. //建立文件流
  949. using var newStream = new FileStream(filePath, FileMode.Truncate, FileAccess.Write, FileShare.None, 1024, true);
  950. newStream.Write(index, 0, 10261);
  951. newStream.Flush();
  952. }
  953. #endregion
  954. #region 从一个文件中解密出一段文本
  955. /// <summary>
  956. /// 从一个文件中解密出一段文本,其中,这个文件是由InitBinFile建立的,并且由 EncryptToFile加密的
  957. /// </summary>
  958. /// <param name="filePath">要解密的文件</param>
  959. /// <param name="dataIndex">要从哪一个块中解密</param>
  960. /// <param name="iv">初始化向量</param>
  961. /// <param name="key">解密密匙</param>
  962. /// <returns>解密后的文本</returns>
  963. public static string DecryptFromFile(this string filePath, int dataIndex, byte[] iv, byte[] key)
  964. {
  965. if ((dataIndex > 10) && (dataIndex < 1))
  966. {
  967. return "";
  968. }
  969. using var tmpFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, 1024, true);
  970. using var decryptor = _rc2Csp.CreateDecryptor(key, iv);
  971. var msDecrypt = new MemoryStream();
  972. using var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write);
  973. var index = new byte[10261];
  974. tmpFileStream.Read(index, 0, 10261);
  975. var count = index[dataIndex * 2 - 1] * 128 + index[dataIndex * 2];
  976. var tmp = new byte[count];
  977. Array.Copy(index, 1024 * (dataIndex - 1) + 21, tmp, 0, count);
  978. csDecrypt.Write(tmp, 0, count);
  979. csDecrypt.FlushFinalBlock();
  980. var decrypted = msDecrypt.ToArray();
  981. return _textConverter.GetString(decrypted, 0, decrypted.Length);
  982. }
  983. #endregion
  984. #region 将一段文本加密后保存到一个文件
  985. /// <summary>
  986. /// 将一段文本加密后保存到一个文件
  987. /// </summary>
  988. /// <param name="toEncryptText">要加密的文本数据</param>
  989. /// <param name="filePath">要保存的文件</param>
  990. /// <param name="iv">初始化向量</param>
  991. /// <param name="key">加密密匙</param>
  992. /// <returns>是否加密成功</returns>
  993. public static void EncryptToFile(this string toEncryptText, string filePath, byte[] iv, byte[] key)
  994. {
  995. using var tmpFileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, 1024, true);
  996. using var encryptor = _rc2Csp.CreateEncryptor(key, iv);
  997. var msEncrypt = new MemoryStream();
  998. using var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
  999. var tmp = _textConverter.GetBytes(toEncryptText);
  1000. csEncrypt.Write(tmp, 0, tmp.Length);
  1001. csEncrypt.FlushFinalBlock();
  1002. var encrypted = msEncrypt.ToArray();
  1003. tmpFileStream.Write(encrypted, 0, encrypted.Length);
  1004. tmpFileStream.Flush();
  1005. }
  1006. #endregion
  1007. #region 将一个被加密的文件解密
  1008. /// <summary>
  1009. /// 将一个被加密的文件解密
  1010. /// </summary>
  1011. /// <param name="filePath">要解密的文件</param>
  1012. /// <param name="iv">初始化向量</param>
  1013. /// <param name="key">解密密匙</param>
  1014. /// <returns>解密后的文本</returns>
  1015. public static string DecryptFromFile(this string filePath, byte[] iv, byte[] key)
  1016. {
  1017. using var tmpFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, 1024, true);
  1018. using var decryptor = _rc2Csp.CreateDecryptor(key, iv);
  1019. var msDecrypt = new MemoryStream();
  1020. using var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write);
  1021. var tmp = new byte[tmpFileStream.Length];
  1022. tmpFileStream.Read(tmp, 0, tmp.Length);
  1023. csDecrypt.Write(tmp, 0, tmp.Length);
  1024. csDecrypt.FlushFinalBlock();
  1025. var decrypted = msDecrypt.ToArray();
  1026. return _textConverter.GetString(decrypted, 0, decrypted.Length);
  1027. }
  1028. #endregion
  1029. #region 设置加密或解密的初始化向量
  1030. /// <summary>
  1031. /// 设置加密或解密的初始化向量
  1032. /// </summary>
  1033. /// <param name="s">长度等于8的ASCII字符集的字符串</param>
  1034. public static void SetIV(this string s)
  1035. {
  1036. if (s.Length != 8)
  1037. {
  1038. _iv = null;
  1039. return;
  1040. }
  1041. try
  1042. {
  1043. _iv = _asciiEncoding.GetBytes(s);
  1044. }
  1045. catch (Exception)
  1046. {
  1047. _iv = null;
  1048. }
  1049. }
  1050. #endregion
  1051. #region 设置加密或解密的密匙
  1052. /// <summary>
  1053. /// 设置加密或解密的密匙
  1054. /// </summary>
  1055. /// <param name="s">长度等于16的ASCII字符集的字符串</param>
  1056. public static void SetKey(this string s)
  1057. {
  1058. if (s.Length != 16)
  1059. {
  1060. _key = null;
  1061. return;
  1062. }
  1063. try
  1064. {
  1065. _key = _asciiEncoding.GetBytes(s);
  1066. }
  1067. catch (Exception)
  1068. {
  1069. _key = null;
  1070. }
  1071. }
  1072. #endregion
  1073. }
  1074. }