简单说一下加密解密:

加密解密有挺多中的方式去实现,今天呢我做为宇宙第一帅的人就给大家来分享以下源代码!听清楚是源代码哦!!

话不多说上干货

1:MD5加密/解密

  1. 1 using System;
  2. 2 using System.Collections.Generic;
  3. 3 using System.Text;
  4. 4 using System.Web;
  5. 5 using System.Text.RegularExpressions;
  6. 6
  7. 7 namespace hic.Common
  8. 8 {
  9. 9 public class SecurityHelper
  10. 10 {
  11. 11 /// <summary>
  12. 12 /// MD5字符串加密
  13. 13 /// </summary>
  14. 14 /// <param name="source">待加密字符串</param>
  15. 15 /// <returns></returns>
  16. 16 public static string MD5(string source)
  17. 17 {
  18. 18 return MD5(source, true);
  19. 19 }
  20. 20
  21. 21 /// <summary>
  22. 22 /// MD5字符串加密
  23. 23 /// </summary>
  24. 24 /// <param name="source">待加密字符串</param>
  25. 25 /// <param name="ishalf">加密是16位还是32位,如果为true则是16位。</param>
  26. 26 /// <returns></returns>
  27. 27 public static string MD5(string source, bool ishalf)
  28. 28 {
  29. 29 string outputStr = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(source, "MD5").ToLower();
  30. 30 if (ishalf)//16位MD5加密(取32位加密的9~25字符)
  31. 31 outputStr = outputStr.Substring(8, 16);
  32. 32 return outputStr;
  33. 33 }
  34. 34
  35. 35 /// <summary>
  36. 36 /// 对字符串进行Base64编码
  37. 37 /// </summary>
  38. 38 /// <param name="source">待编码字符串</param>
  39. 39 /// <returns></returns>
  40. 40 public static string EncodeBase64(string source)
  41. 41 {
  42. 42 UnicodeEncoding code = new UnicodeEncoding();
  43. 43 byte[] bytes = code.GetBytes(source);
  44. 44 return Convert.ToBase64String(bytes);
  45. 45 }
  46. 46
  47. 47 /// <summary>
  48. 48 /// 对字符串进行Base64解码
  49. 49 /// </summary>
  50. 50 /// <param name="source">待解码字符串</param>
  51. 51 /// <returns></returns>
  52. 52 public static string DecodeBase64(string source)
  53. 53 {
  54. 54 UnicodeEncoding code = new UnicodeEncoding();
  55. 55 byte[] bytes = Convert.FromBase64String(source);
  56. 56 return code.GetString(bytes);
  57. 57 }
  58. 58
  59. 59 /// <summary>
  60. 60 /// 检查当前IP是否是受限IP
  61. 61 /// </summary>
  62. 62 /// <param name="LimitedIP">受限的IP,格式如:192.168.1.110|212.235.*.*|232.*.*.*</param>
  63. 63 /// <returns>返回true表示IP未受到限制</returns>
  64. 64 public static bool CheckIPIsLimited(string limitedIP)
  65. 65 {
  66. 66 string currentIP = GetUserIP();
  67. 67 if (limitedIP == null || limitedIP.Trim() == string.Empty)
  68. 68 return true;
  69. 69 limitedIP.Replace(".", @"\.");
  70. 70 limitedIP.Replace("*", @"[^\.]{1,3}");
  71. 71 Regex reg = new Regex(limitedIP, RegexOptions.Compiled);
  72. 72 Match match = reg.Match(currentIP);
  73. 73 return !match.Success;
  74. 74 }
  75. 75
  76. 76 /// <summary>
  77. 77 /// 得到用户IP
  78. 78 /// </summary>
  79. 79 /// <returns></returns>
  80. 80 public static string GetUserIP()
  81. 81 {
  82. 82 return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
  83. 83 }
  84. 84 }
  85. 85 }

View Code

2:DES加密/解密类

  1. 1 using System;
  2. 2 using System.Security.Cryptography;
  3. 3 using System.Text;
  4. 4 namespace Maticsoft.Common.DEncrypt
  5. 5 {
  6. 6 /// <summary>
  7. 7 /// DES加密/解密类。
  8. 8 /// LiTianPing
  9. 9 /// </summary>
  10. 10 public class DESEncrypt
  11. 11 {
  12. 12 public DESEncrypt()
  13. 13 {
  14. 14 }
  15. 15
  16. 16 #region ========加密========
  17. 17
  18. 18 /// <summary>
  19. 19 /// 加密
  20. 20 /// </summary>
  21. 21 /// <param name="Text"></param>
  22. 22 /// <returns></returns>
  23. 23 public static string Encrypt(string Text)
  24. 24 {
  25. 25 return Encrypt(Text,"MATICSOFT");
  26. 26 }
  27. 27 /// <summary>
  28. 28 /// 加密数据
  29. 29 /// </summary>
  30. 30 /// <param name="Text"></param>
  31. 31 /// <param name="sKey"></param>
  32. 32 /// <returns></returns>
  33. 33 public static string Encrypt(string Text,string sKey)
  34. 34 {
  35. 35 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  36. 36 byte[] inputByteArray;
  37. 37 inputByteArray=Encoding.Default.GetBytes(Text);
  38. 38 des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  39. 39 des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  40. 40 System.IO.MemoryStream ms=new System.IO.MemoryStream();
  41. 41 CryptoStream cs=new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write);
  42. 42 cs.Write(inputByteArray,0,inputByteArray.Length);
  43. 43 cs.FlushFinalBlock();
  44. 44 StringBuilder ret=new StringBuilder();
  45. 45 foreach( byte b in ms.ToArray())
  46. 46 {
  47. 47 ret.AppendFormat("{0:X2}",b);
  48. 48 }
  49. 49 return ret.ToString();
  50. 50 }
  51. 51
  52. 52 #endregion
  53. 53
  54. 54 #region ========解密========
  55. 55
  56. 56
  57. 57 /// <summary>
  58. 58 /// 解密
  59. 59 /// </summary>
  60. 60 /// <param name="Text"></param>
  61. 61 /// <returns></returns>
  62. 62 public static string Decrypt(string Text)
  63. 63 {
  64. 64 return Decrypt(Text,"MATICSOFT");
  65. 65 }
  66. 66 /// <summary>
  67. 67 /// 解密数据
  68. 68 /// </summary>
  69. 69 /// <param name="Text"></param>
  70. 70 /// <param name="sKey"></param>
  71. 71 /// <returns></returns>
  72. 72 public static string Decrypt(string Text,string sKey)
  73. 73 {
  74. 74 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  75. 75 int len;
  76. 76 len=Text.Length/2;
  77. 77 byte[] inputByteArray = new byte[len];
  78. 78 int x,i;
  79. 79 for(x=0;x<len;x++)
  80. 80 {
  81. 81 i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
  82. 82 inputByteArray[x]=(byte)i;
  83. 83 }
  84. 84 des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  85. 85 des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  86. 86 System.IO.MemoryStream ms=new System.IO.MemoryStream();
  87. 87 CryptoStream cs=new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write);
  88. 88 cs.Write(inputByteArray,0,inputByteArray.Length);
  89. 89 cs.FlushFinalBlock();
  90. 90 return Encoding.Default.GetString(ms.ToArray());
  91. 91 }
  92. 92
  93. 93 #endregion
  94. 94
  95. 95
  96. 96 }
  97. 97 }

View Code

3:RSA加密解密及RSA签名和验证

  1. 1 using System;
  2. 2 using System.Text;
  3. 3 using System.Security.Cryptography;
  4. 4 namespace Maticsoft.Common.DEncrypt
  5. 5 {
  6. 6 /// <summary>
  7. 7 /// RSA加密解密及RSA签名和验证
  8. 8 /// </summary>
  9. 9 public class RSACryption
  10. 10 {
  11. 11 public RSACryption()
  12. 12 {
  13. 13 }
  14. 14
  15. 15
  16. 16 #region RSA 加密解密
  17. 17
  18. 18 #region RSA 的密钥产生
  19. 19
  20. 20 /// <summary>
  21. 21 /// RSA 的密钥产生 产生私钥 和公钥
  22. 22 /// </summary>
  23. 23 /// <param name="xmlKeys"></param>
  24. 24 /// <param name="xmlPublicKey"></param>
  25. 25 public void RSAKey(out string xmlKeys,out string xmlPublicKey)
  26. 26 {
  27. 27 System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
  28. 28 xmlKeys=rsa.ToXmlString(true);
  29. 29 xmlPublicKey = rsa.ToXmlString(false);
  30. 30 }
  31. 31 #endregion
  32. 32
  33. 33 #region RSA的加密函数
  34. 34 //##############################################################################
  35. 35 //RSA 方式加密
  36. 36 //说明KEY必须是XML的行式,返回的是字符串
  37. 37 //在有一点需要说明!!该加密方式有 长度 限制的!!
  38. 38 //##############################################################################
  39. 39
  40. 40 //RSA的加密函数 string
  41. 41 public string RSAEncrypt(string xmlPublicKey,string m_strEncryptString )
  42. 42 {
  43. 43
  44. 44 byte[] PlainTextBArray;
  45. 45 byte[] CypherTextBArray;
  46. 46 string Result;
  47. 47 RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
  48. 48 rsa.FromXmlString(xmlPublicKey);
  49. 49 PlainTextBArray = (new UnicodeEncoding()).GetBytes(m_strEncryptString);
  50. 50 CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);
  51. 51 Result=Convert.ToBase64String(CypherTextBArray);
  52. 52 return Result;
  53. 53
  54. 54 }
  55. 55 //RSA的加密函数 byte[]
  56. 56 public string RSAEncrypt(string xmlPublicKey,byte[] EncryptString )
  57. 57 {
  58. 58
  59. 59 byte[] CypherTextBArray;
  60. 60 string Result;
  61. 61 RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
  62. 62 rsa.FromXmlString(xmlPublicKey);
  63. 63 CypherTextBArray = rsa.Encrypt(EncryptString, false);
  64. 64 Result=Convert.ToBase64String(CypherTextBArray);
  65. 65 return Result;
  66. 66
  67. 67 }
  68. 68 #endregion
  69. 69
  70. 70 #region RSA的解密函数
  71. 71 //RSA的解密函数 string
  72. 72 public string RSADecrypt(string xmlPrivateKey, string m_strDecryptString )
  73. 73 {
  74. 74 byte[] PlainTextBArray;
  75. 75 byte[] DypherTextBArray;
  76. 76 string Result;
  77. 77 System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
  78. 78 rsa.FromXmlString(xmlPrivateKey);
  79. 79 PlainTextBArray =Convert.FromBase64String(m_strDecryptString);
  80. 80 DypherTextBArray=rsa.Decrypt(PlainTextBArray, false);
  81. 81 Result=(new UnicodeEncoding()).GetString(DypherTextBArray);
  82. 82 return Result;
  83. 83
  84. 84 }
  85. 85
  86. 86 //RSA的解密函数 byte
  87. 87 public string RSADecrypt(string xmlPrivateKey, byte[] DecryptString )
  88. 88 {
  89. 89 byte[] DypherTextBArray;
  90. 90 string Result;
  91. 91 System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
  92. 92 rsa.FromXmlString(xmlPrivateKey);
  93. 93 DypherTextBArray=rsa.Decrypt(DecryptString, false);
  94. 94 Result=(new UnicodeEncoding()).GetString(DypherTextBArray);
  95. 95 return Result;
  96. 96
  97. 97 }
  98. 98 #endregion
  99. 99
  100. 100 #endregion
  101. 101
  102. 102 #region RSA数字签名
  103. 103
  104. 104 #region 获取Hash描述表
  105. 105 //获取Hash描述表
  106. 106 public bool GetHash(string m_strSource, ref byte[] HashData)
  107. 107 {
  108. 108 //从字符串中取得Hash描述
  109. 109 byte[] Buffer;
  110. 110 System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
  111. 111 Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(m_strSource);
  112. 112 HashData = MD5.ComputeHash(Buffer);
  113. 113
  114. 114 return true;
  115. 115 }
  116. 116
  117. 117 //获取Hash描述表
  118. 118 public bool GetHash(string m_strSource, ref string strHashData)
  119. 119 {
  120. 120
  121. 121 //从字符串中取得Hash描述
  122. 122 byte[] Buffer;
  123. 123 byte[] HashData;
  124. 124 System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
  125. 125 Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(m_strSource);
  126. 126 HashData = MD5.ComputeHash(Buffer);
  127. 127
  128. 128 strHashData = Convert.ToBase64String(HashData);
  129. 129 return true;
  130. 130
  131. 131 }
  132. 132
  133. 133 //获取Hash描述表
  134. 134 public bool GetHash(System.IO.FileStream objFile, ref byte[] HashData)
  135. 135 {
  136. 136
  137. 137 //从文件中取得Hash描述
  138. 138 System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
  139. 139 HashData = MD5.ComputeHash(objFile);
  140. 140 objFile.Close();
  141. 141
  142. 142 return true;
  143. 143
  144. 144 }
  145. 145
  146. 146 //获取Hash描述表
  147. 147 public bool GetHash(System.IO.FileStream objFile, ref string strHashData)
  148. 148 {
  149. 149
  150. 150 //从文件中取得Hash描述
  151. 151 byte[] HashData;
  152. 152 System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
  153. 153 HashData = MD5.ComputeHash(objFile);
  154. 154 objFile.Close();
  155. 155
  156. 156 strHashData = Convert.ToBase64String(HashData);
  157. 157
  158. 158 return true;
  159. 159
  160. 160 }
  161. 161 #endregion
  162. 162
  163. 163 #region RSA签名
  164. 164 //RSA签名
  165. 165 public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref byte[] EncryptedSignatureData)
  166. 166 {
  167. 167
  168. 168 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  169. 169
  170. 170 RSA.FromXmlString(p_strKeyPrivate);
  171. 171 System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
  172. 172 //设置签名的算法为MD5
  173. 173 RSAFormatter.SetHashAlgorithm("MD5");
  174. 174 //执行签名
  175. 175 EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
  176. 176
  177. 177 return true;
  178. 178
  179. 179 }
  180. 180
  181. 181 //RSA签名
  182. 182 public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref string m_strEncryptedSignatureData)
  183. 183 {
  184. 184
  185. 185 byte[] EncryptedSignatureData;
  186. 186
  187. 187 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  188. 188
  189. 189 RSA.FromXmlString(p_strKeyPrivate);
  190. 190 System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
  191. 191 //设置签名的算法为MD5
  192. 192 RSAFormatter.SetHashAlgorithm("MD5");
  193. 193 //执行签名
  194. 194 EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
  195. 195
  196. 196 m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);
  197. 197
  198. 198 return true;
  199. 199
  200. 200 }
  201. 201
  202. 202 //RSA签名
  203. 203 public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref byte[] EncryptedSignatureData)
  204. 204 {
  205. 205
  206. 206 byte[] HashbyteSignature;
  207. 207
  208. 208 HashbyteSignature = Convert.FromBase64String(m_strHashbyteSignature);
  209. 209 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  210. 210
  211. 211 RSA.FromXmlString(p_strKeyPrivate);
  212. 212 System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
  213. 213 //设置签名的算法为MD5
  214. 214 RSAFormatter.SetHashAlgorithm("MD5");
  215. 215 //执行签名
  216. 216 EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
  217. 217
  218. 218 return true;
  219. 219
  220. 220 }
  221. 221
  222. 222 //RSA签名
  223. 223 public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref string m_strEncryptedSignatureData)
  224. 224 {
  225. 225
  226. 226 byte[] HashbyteSignature;
  227. 227 byte[] EncryptedSignatureData;
  228. 228
  229. 229 HashbyteSignature = Convert.FromBase64String(m_strHashbyteSignature);
  230. 230 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  231. 231
  232. 232 RSA.FromXmlString(p_strKeyPrivate);
  233. 233 System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
  234. 234 //设置签名的算法为MD5
  235. 235 RSAFormatter.SetHashAlgorithm("MD5");
  236. 236 //执行签名
  237. 237 EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
  238. 238
  239. 239 m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);
  240. 240
  241. 241 return true;
  242. 242
  243. 243 }
  244. 244 #endregion
  245. 245
  246. 246 #region RSA 签名验证
  247. 247
  248. 248 public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, byte[] DeformatterData)
  249. 249 {
  250. 250
  251. 251 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  252. 252
  253. 253 RSA.FromXmlString(p_strKeyPublic);
  254. 254 System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
  255. 255 //指定解密的时候HASH算法为MD5
  256. 256 RSADeformatter.SetHashAlgorithm("MD5");
  257. 257
  258. 258 if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
  259. 259 {
  260. 260 return true;
  261. 261 }
  262. 262 else
  263. 263 {
  264. 264 return false;
  265. 265 }
  266. 266
  267. 267 }
  268. 268
  269. 269 public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, byte[] DeformatterData)
  270. 270 {
  271. 271
  272. 272 byte[] HashbyteDeformatter;
  273. 273
  274. 274 HashbyteDeformatter = Convert.FromBase64String(p_strHashbyteDeformatter);
  275. 275
  276. 276 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  277. 277
  278. 278 RSA.FromXmlString(p_strKeyPublic);
  279. 279 System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
  280. 280 //指定解密的时候HASH算法为MD5
  281. 281 RSADeformatter.SetHashAlgorithm("MD5");
  282. 282
  283. 283 if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
  284. 284 {
  285. 285 return true;
  286. 286 }
  287. 287 else
  288. 288 {
  289. 289 return false;
  290. 290 }
  291. 291
  292. 292 }
  293. 293
  294. 294 public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, string p_strDeformatterData)
  295. 295 {
  296. 296
  297. 297 byte[] DeformatterData;
  298. 298
  299. 299 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  300. 300
  301. 301 RSA.FromXmlString(p_strKeyPublic);
  302. 302 System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
  303. 303 //指定解密的时候HASH算法为MD5
  304. 304 RSADeformatter.SetHashAlgorithm("MD5");
  305. 305
  306. 306 DeformatterData =Convert.FromBase64String(p_strDeformatterData);
  307. 307
  308. 308 if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
  309. 309 {
  310. 310 return true;
  311. 311 }
  312. 312 else
  313. 313 {
  314. 314 return false;
  315. 315 }
  316. 316
  317. 317 }
  318. 318
  319. 319 public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, string p_strDeformatterData)
  320. 320 {
  321. 321
  322. 322 byte[] DeformatterData;
  323. 323 byte[] HashbyteDeformatter;
  324. 324
  325. 325 HashbyteDeformatter = Convert.FromBase64String(p_strHashbyteDeformatter);
  326. 326 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  327. 327
  328. 328 RSA.FromXmlString(p_strKeyPublic);
  329. 329 System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
  330. 330 //指定解密的时候HASH算法为MD5
  331. 331 RSADeformatter.SetHashAlgorithm("MD5");
  332. 332
  333. 333 DeformatterData =Convert.FromBase64String(p_strDeformatterData);
  334. 334
  335. 335 if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
  336. 336 {
  337. 337 return true;
  338. 338 }
  339. 339 else
  340. 340 {
  341. 341 return false;
  342. 342 }
  343. 343
  344. 344 }
  345. 345
  346. 346
  347. 347 #endregion
  348. 348
  349. 349
  350. 350 #endregion
  351. 351
  352. 352 }
  353. 353 }

View Code

4:哈希加密/解密

  1. 1 using System;
  2. 2 using System.Text;
  3. 3 using System.Security.Cryptography;
  4. 4 namespace Maticsoft.Common.DEncrypt
  5. 5 {
  6. 6 /// <summary>
  7. 7 /// 得到随机安全码(哈希加密)。
  8. 8 /// </summary>
  9. 9 public class HashEncode
  10. 10 {
  11. 11 public HashEncode()
  12. 12 {
  13. 13 //
  14. 14 // TODO: 在此处添加构造函数逻辑
  15. 15 //
  16. 16 }
  17. 17 /// <summary>
  18. 18 /// 得到随机哈希加密字符串
  19. 19 /// </summary>
  20. 20 /// <returns></returns>
  21. 21 public static string GetSecurity()
  22. 22 {
  23. 23 string Security = HashEncoding(GetRandomValue());
  24. 24 return Security;
  25. 25 }
  26. 26 /// <summary>
  27. 27 /// 得到一个随机数值
  28. 28 /// </summary>
  29. 29 /// <returns></returns>
  30. 30 public static string GetRandomValue()
  31. 31 {
  32. 32 Random Seed = new Random();
  33. 33 string RandomVaule = Seed.Next(1, int.MaxValue).ToString();
  34. 34 return RandomVaule;
  35. 35 }
  36. 36 /// <summary>
  37. 37 /// 哈希加密一个字符串
  38. 38 /// </summary>
  39. 39 /// <param name="Security"></param>
  40. 40 /// <returns></returns>
  41. 41 public static string HashEncoding(string Security)
  42. 42 {
  43. 43 byte[] Value;
  44. 44 UnicodeEncoding Code = new UnicodeEncoding();
  45. 45 byte[] Message = Code.GetBytes(Security);
  46. 46 SHA512Managed Arithmetic = new SHA512Managed();
  47. 47 Value = Arithmetic.ComputeHash(Message);
  48. 48 Security = "";
  49. 49 foreach(byte o in Value)
  50. 50 {
  51. 51 Security += (int) o + "O";
  52. 52 }
  53. 53 return Security;
  54. 54 }
  55. 55 }
  56. 56 }

View Code

5:Encrypt

  1. 1 using System;
  2. 2 using System.Security.Cryptography;
  3. 3 using System.Text;
  4. 4 namespace Maticsoft.Common.DEncrypt
  5. 5 {
  6. 6 /// <summary>
  7. 7 /// Encrypt 的摘要说明。
  8. 8 /// LiTianPing
  9. 9 /// </summary>
  10. 10 public class DEncrypt
  11. 11 {
  12. 12 /// <summary>
  13. 13 /// 构造方法
  14. 14 /// </summary>
  15. 15 public DEncrypt()
  16. 16 {
  17. 17 }
  18. 18
  19. 19 #region 使用 缺省密钥字符串 加密/解密string
  20. 20
  21. 21 /// <summary>
  22. 22 /// 使用缺省密钥字符串加密string
  23. 23 /// </summary>
  24. 24 /// <param name="original">明文</param>
  25. 25 /// <returns>密文</returns>
  26. 26 public static string Encrypt(string original)
  27. 27 {
  28. 28 return Encrypt(original,"MATICSOFT");
  29. 29 }
  30. 30 /// <summary>
  31. 31 /// 使用缺省密钥字符串解密string
  32. 32 /// </summary>
  33. 33 /// <param name="original">密文</param>
  34. 34 /// <returns>明文</returns>
  35. 35 public static string Decrypt(string original)
  36. 36 {
  37. 37 return Decrypt(original,"MATICSOFT",System.Text.Encoding.Default);
  38. 38 }
  39. 39
  40. 40 #endregion
  41. 41
  42. 42 #region 使用 给定密钥字符串 加密/解密string
  43. 43 /// <summary>
  44. 44 /// 使用给定密钥字符串加密string
  45. 45 /// </summary>
  46. 46 /// <param name="original">原始文字</param>
  47. 47 /// <param name="key">密钥</param>
  48. 48 /// <param name="encoding">字符编码方案</param>
  49. 49 /// <returns>密文</returns>
  50. 50 public static string Encrypt(string original, string key)
  51. 51 {
  52. 52 byte[] buff = System.Text.Encoding.Default.GetBytes(original);
  53. 53 byte[] kb = System.Text.Encoding.Default.GetBytes(key);
  54. 54 return Convert.ToBase64String(Encrypt(buff,kb));
  55. 55 }
  56. 56 /// <summary>
  57. 57 /// 使用给定密钥字符串解密string
  58. 58 /// </summary>
  59. 59 /// <param name="original">密文</param>
  60. 60 /// <param name="key">密钥</param>
  61. 61 /// <returns>明文</returns>
  62. 62 public static string Decrypt(string original, string key)
  63. 63 {
  64. 64 return Decrypt(original,key,System.Text.Encoding.Default);
  65. 65 }
  66. 66
  67. 67 /// <summary>
  68. 68 /// 使用给定密钥字符串解密string,返回指定编码方式明文
  69. 69 /// </summary>
  70. 70 /// <param name="encrypted">密文</param>
  71. 71 /// <param name="key">密钥</param>
  72. 72 /// <param name="encoding">字符编码方案</param>
  73. 73 /// <returns>明文</returns>
  74. 74 public static string Decrypt(string encrypted, string key,Encoding encoding)
  75. 75 {
  76. 76 byte[] buff = Convert.FromBase64String(encrypted);
  77. 77 byte[] kb = System.Text.Encoding.Default.GetBytes(key);
  78. 78 return encoding.GetString(Decrypt(buff,kb));
  79. 79 }
  80. 80 #endregion
  81. 81
  82. 82 #region 使用 缺省密钥字符串 加密/解密/byte[]
  83. 83 /// <summary>
  84. 84 /// 使用缺省密钥字符串解密byte[]
  85. 85 /// </summary>
  86. 86 /// <param name="encrypted">密文</param>
  87. 87 /// <param name="key">密钥</param>
  88. 88 /// <returns>明文</returns>
  89. 89 public static byte[] Decrypt(byte[] encrypted)
  90. 90 {
  91. 91 byte[] key = System.Text.Encoding.Default.GetBytes("MATICSOFT");
  92. 92 return Decrypt(encrypted,key);
  93. 93 }
  94. 94 /// <summary>
  95. 95 /// 使用缺省密钥字符串加密
  96. 96 /// </summary>
  97. 97 /// <param name="original">原始数据</param>
  98. 98 /// <param name="key">密钥</param>
  99. 99 /// <returns>密文</returns>
  100. 100 public static byte[] Encrypt(byte[] original)
  101. 101 {
  102. 102 byte[] key = System.Text.Encoding.Default.GetBytes("MATICSOFT");
  103. 103 return Encrypt(original,key);
  104. 104 }
  105. 105 #endregion
  106. 106
  107. 107 #region 使用 给定密钥 加密/解密/byte[]
  108. 108
  109. 109 /// <summary>
  110. 110 /// 生成MD5摘要
  111. 111 /// </summary>
  112. 112 /// <param name="original">数据源</param>
  113. 113 /// <returns>摘要</returns>
  114. 114 public static byte[] MakeMD5(byte[] original)
  115. 115 {
  116. 116 MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
  117. 117 byte[] keyhash = hashmd5.ComputeHash(original);
  118. 118 hashmd5 = null;
  119. 119 return keyhash;
  120. 120 }
  121. 121
  122. 122
  123. 123 /// <summary>
  124. 124 /// 使用给定密钥加密
  125. 125 /// </summary>
  126. 126 /// <param name="original">明文</param>
  127. 127 /// <param name="key">密钥</param>
  128. 128 /// <returns>密文</returns>
  129. 129 public static byte[] Encrypt(byte[] original, byte[] key)
  130. 130 {
  131. 131 TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
  132. 132 des.Key = MakeMD5(key);
  133. 133 des.Mode = CipherMode.ECB;
  134. 134
  135. 135 return des.CreateEncryptor().TransformFinalBlock(original, 0, original.Length);
  136. 136 }
  137. 137
  138. 138 /// <summary>
  139. 139 /// 使用给定密钥解密数据
  140. 140 /// </summary>
  141. 141 /// <param name="encrypted">密文</param>
  142. 142 /// <param name="key">密钥</param>
  143. 143 /// <returns>明文</returns>
  144. 144 public static byte[] Decrypt(byte[] encrypted, byte[] key)
  145. 145 {
  146. 146 TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
  147. 147 des.Key = MakeMD5(key);
  148. 148 des.Mode = CipherMode.ECB;
  149. 149
  150. 150 return des.CreateDecryptor().TransformFinalBlock(encrypted, 0, encrypted.Length);
  151. 151 }
  152. 152
  153. 153 #endregion
  154. 154
  155. 155
  156. 156
  157. 157
  158. 158 }
  159. 159 }

View Code

 

希望能给大家带来帮助。

版权声明:本文为sshr原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/sshr/p/10638232.html