颐和api

SecureHelper.cs 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Security.Cryptography;
  5. using System.Text.RegularExpressions;
  6. using System.Linq;
  7. namespace MadRunFabric.Common
  8. {
  9. /// <summary>
  10. /// 安装帮助类
  11. /// </summary>
  12. public static class SecureHelper
  13. {
  14. private static Encoding _encoding { get => Encoding.UTF8; }
  15. private static string BsToStr(byte[] bs) => string.Join(string.Empty, bs.Select(x => x.ToString("x2"))).Replace("-", string.Empty);
  16. /// <summary>
  17. /// 获取MD5
  18. /// </summary>
  19. /// <param name="str"></param>
  20. /// <returns></returns>
  21. public static string GetMD5(string str)
  22. {
  23. using (var md5 = new MD5CryptoServiceProvider())
  24. {
  25. var bs = _encoding.GetBytes(str);
  26. bs = md5.ComputeHash(bs);
  27. return BsToStr(bs);
  28. }
  29. }
  30. /// <summary>
  31. /// 获取md5
  32. /// </summary>
  33. /// <param name="_bs"></param>
  34. /// <returns></returns>
  35. public static string GetMD5(byte[] _bs)
  36. {
  37. using (var md5 = new MD5CryptoServiceProvider())
  38. {
  39. var bs = md5.ComputeHash(_bs);
  40. return BsToStr(bs);
  41. }
  42. }
  43. /// <summary>
  44. /// 读取文件MD5
  45. /// </summary>
  46. /// <param name="fileName"></param>
  47. /// <returns></returns>
  48. public static string GetFileMD5(string fileName)
  49. {
  50. using (var file = new FileStream(fileName, FileMode.Open))
  51. {
  52. using (var md5 = new MD5CryptoServiceProvider())
  53. {
  54. var bs = md5.ComputeHash(file);
  55. return BsToStr(bs);
  56. }
  57. }
  58. }
  59. /// <summary>
  60. /// 获取sha1
  61. /// </summary>
  62. /// <param name="str"></param>
  63. /// <returns></returns>
  64. public static string GetSHA1(string str)
  65. {
  66. using (var sha1 = new SHA1CryptoServiceProvider())
  67. {
  68. var bs = _encoding.GetBytes(str);
  69. bs = sha1.ComputeHash(bs);
  70. return BsToStr(bs);
  71. }
  72. }
  73. /// <summary>
  74. /// SHA256函数
  75. /// </summary>
  76. /// /// <param name="str">原始字符串</param>
  77. /// <returns>SHA256结果</returns>
  78. public static string GetSHA256(string str)
  79. {
  80. using (var Sha256 = new SHA256CryptoServiceProvider())
  81. {
  82. var bs = _encoding.GetBytes(str);
  83. bs = Sha256.ComputeHash(bs);
  84. return BsToStr(bs);
  85. }
  86. }
  87. /// <summary>
  88. /// 获取hmac md5
  89. /// </summary>
  90. /// <param name="str"></param>
  91. /// <param name="password"></param>
  92. /// <returns></returns>
  93. public static string GetHMACMD5(string str, string password)
  94. {
  95. using (var hmac_md5 = new HMACMD5())
  96. {
  97. hmac_md5.Key = _encoding.GetBytes(password);
  98. var bs = hmac_md5.ComputeHash(_encoding.GetBytes(str));
  99. return BsToStr(bs);
  100. }
  101. }
  102. /// <summary>
  103. /// 已测试
  104. /// </summary>
  105. /// <param name="str"></param>
  106. /// <param name="key"></param>
  107. /// <returns></returns>
  108. public static string GetHMACSHA256(string str, string key)
  109. {
  110. using (var hmac_sha256 = new HMACSHA256(_encoding.GetBytes(key)))
  111. {
  112. var data = hmac_sha256.ComputeHash(_encoding.GetBytes(str));
  113. return BitConverter.ToString(data);
  114. }
  115. }
  116. /// <summary>
  117. /// 获取hmac sha1
  118. /// </summary>
  119. /// <param name="str"></param>
  120. /// <param name="password"></param>
  121. /// <returns></returns>
  122. public static string GetHMACSHA1(string str, string password)
  123. {
  124. using (var hmac_sha1 = new HMACSHA1())
  125. {
  126. hmac_sha1.Key = _encoding.GetBytes(password);
  127. var bs = hmac_sha1.ComputeHash(_encoding.GetBytes(str));
  128. return BsToStr(bs);
  129. }
  130. }
  131. #region DES加密解密
  132. private static readonly string txtKey = "PatrickpanP=";
  133. private static readonly string txtIV = "LiuJineagel=";
  134. public static string DESEncrypt(string Text, string key) => DESEncrypt(Text, key, key);
  135. public static string DESDecrypt(string Text, string key) => DESDecrypt(Text, key, key);
  136. /// <summary>
  137. /// 加密数据
  138. /// </summary>
  139. public static string DESEncrypt(string Text, string txtKey, string txtIV)
  140. {
  141. using (var des = new DESCryptoServiceProvider())
  142. {
  143. byte[] inputByteArray;
  144. inputByteArray = _encoding.GetBytes(Text);
  145. //des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  146. //des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  147. des.Key = Convert.FromBase64String(txtKey);
  148. des.IV = Convert.FromBase64String(txtIV);
  149. using (var ms = new MemoryStream())
  150. {
  151. using (var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
  152. {
  153. cs.Write(inputByteArray, 0, inputByteArray.Length);
  154. cs.FlushFinalBlock();
  155. StringBuilder ret = new StringBuilder();
  156. foreach (byte b in ms.ToArray())
  157. {
  158. ret.AppendFormat("{0:X2}", b);
  159. }
  160. return ret.ToString();
  161. }
  162. }
  163. }
  164. }
  165. /// <summary>
  166. /// 解密数据
  167. /// </summary>
  168. public static string DESDecrypt(string Text, string txtKey, string txtIV)
  169. {
  170. using (var des = new DESCryptoServiceProvider())
  171. {
  172. int len;
  173. len = Text.Length / 2;
  174. byte[] inputByteArray = new byte[len];
  175. int x, i;
  176. for (x = 0; x < len; x++)
  177. {
  178. i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
  179. inputByteArray[x] = (byte)i;
  180. }
  181. //des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  182. //des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  183. des.Key = Convert.FromBase64String(txtKey);
  184. des.IV = Convert.FromBase64String(txtIV);
  185. using (var ms = new MemoryStream())
  186. {
  187. using (var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
  188. {
  189. cs.Write(inputByteArray, 0, inputByteArray.Length);
  190. cs.FlushFinalBlock();
  191. return _encoding.GetString(ms.ToArray());
  192. }
  193. }
  194. }
  195. }
  196. #endregion
  197. //AES密钥向量
  198. private static readonly byte[] _aeskeys = new byte[] {
  199. 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD,
  200. 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF
  201. };
  202. /// <summary>
  203. /// AES加密
  204. /// </summary>
  205. /// <param name="encryptStr">加密字符串</param>
  206. /// <param name="encryptKey">密钥</param>
  207. /// <returns></returns>
  208. [Obsolete("未测试")]
  209. public static string AESEncrypt(string encryptStr, string encryptKey)
  210. {
  211. if (string.IsNullOrWhiteSpace(encryptStr))
  212. return string.Empty;
  213. encryptKey = StringHelper.SubString(encryptKey, 32);
  214. encryptKey = encryptKey.PadRight(32, ' ');
  215. //分组加密算法
  216. using (SymmetricAlgorithm des = Rijndael.Create())
  217. {
  218. byte[] inputByteArray = _encoding.GetBytes(encryptStr);//得到需要加密的字节数组
  219. //设置密钥及密钥向量
  220. des.Key = _encoding.GetBytes(encryptKey);
  221. des.IV = _aeskeys;
  222. byte[] cipherBytes = null;
  223. using (MemoryStream ms = new MemoryStream())
  224. {
  225. using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
  226. {
  227. cs.Write(inputByteArray, 0, inputByteArray.Length);
  228. cs.FlushFinalBlock();
  229. cipherBytes = ms.ToArray();//得到加密后的字节数组
  230. }
  231. }
  232. return Convert.ToBase64String(cipherBytes);
  233. }
  234. }
  235. /// <summary>
  236. /// AES解密
  237. /// </summary>
  238. /// <param name="decryptStr">解密字符串</param>
  239. /// <param name="decryptKey">密钥</param>
  240. /// <returns></returns>
  241. [Obsolete("未测试")]
  242. public static string AESDecrypt(string decryptStr, string decryptKey)
  243. {
  244. if (string.IsNullOrWhiteSpace(decryptStr))
  245. return string.Empty;
  246. decryptKey = StringHelper.SubString(decryptKey, 32);
  247. decryptKey = decryptKey.PadRight(32, ' ');
  248. byte[] cipherText = Convert.FromBase64String(decryptStr);
  249. using (SymmetricAlgorithm des = Rijndael.Create())
  250. {
  251. des.Key = _encoding.GetBytes(decryptKey);
  252. des.IV = _aeskeys;
  253. byte[] decryptBytes = new byte[cipherText.Length];
  254. using (MemoryStream ms = new MemoryStream(cipherText))
  255. {
  256. using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read))
  257. {
  258. cs.Read(decryptBytes, 0, decryptBytes.Length);
  259. }
  260. }
  261. return _encoding.GetString(decryptBytes).Replace("\0", "");//将字符串后尾的'\0'去掉}
  262. }
  263. }
  264. }
  265. }