查看: 1762|回复: 0

[.NET源码] ASP.NET中DES加密与解密MD5加密帮助类的实现代码

发表于 2018-1-1 08:00:00
  1. public class TrialHelper
  2. { //默认密钥向量
  3. private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  4. /// <summary>
  5. /// DES加密字符串
  6. /// </summary>
  7. /// <param name="encryptString">待加密的字符串</param>
  8. /// <param name="encryptKey">加密密钥,要求为8位</param>
  9. /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
  10. public static string EncryptDES( string encryptString, string encryptKey = "" )
  11. {
  12. try
  13. {
  14. if (string.IsNullOrEmpty(encryptKey) || encryptKey.Length < 8)
  15. {
  16. encryptKey = "winform01";
  17. }
  18. byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
  19. byte[] rgbIV = Keys;
  20. byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
  21. DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
  22. MemoryStream mStream = new MemoryStream();
  23. CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
  24. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  25. cStream.FlushFinalBlock();
  26. return Convert.ToBase64String(mStream.ToArray());
  27. }
  28. catch
  29. {
  30. return encryptString;
  31. }
  32. }
  33. /// <summary>
  34. /// DES解密字符串
  35. /// </summary>
  36. /// <param name="decryptString">待解密的字符串</param>
  37. /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
  38. /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
  39. public static string DecryptDES( string decryptString, string decryptKey = "" )
  40. {
  41. try
  42. {
  43. if (string.IsNullOrEmpty(decryptKey) || decryptKey.Length < 8)
  44. {
  45. decryptKey = "winform01";
  46. }
  47. byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
  48. byte[] rgbIV = Keys;
  49. byte[] inputByteArray = Convert.FromBase64String(decryptString);
  50. DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
  51. MemoryStream mStream = new MemoryStream();
  52. CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
  53. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  54. cStream.FlushFinalBlock();
  55. return Encoding.UTF8.GetString(mStream.ToArray());
  56. }
  57. catch
  58. {
  59. return decryptString;
  60. }
  61. }
  62. /// <summary>
  63. /// MD5数据加密
  64. /// </summary>
  65. /// <param name="sDataIn">加密字段</param>
  66. /// <returns>加密后的字符串</returns>
  67. public static string GetMD5( string sDataIn )
  68. {
  69. System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  70. byte[] bytValue, bytHash;
  71. bytValue = System.Text.Encoding.UTF8.GetBytes(sDataIn);
  72. bytHash = md5.ComputeHash(bytValue);
  73. md5.Clear();
  74. string sTemp = "";
  75. for (int i = 0; i < bytHash.Length; i++)
  76. {
  77. sTemp += bytHash[i].ToString("x").PadLeft(2, '0');
  78. }
  79. return sTemp;
  80. }
  81. }
复制代码

调用:

  1. //获取登录信息
  2. loginRecord.Name = tbName.Text.Trim();
  3. loginRecord.MD5Pwd = TrialHelper.GetMD5(tbPwd.Password); //保存到数据库MD5加密方式
  4. loginRecord.Pwd = TrialHelper.EncryptDES(tbPwd.Password);//记住密码des加密方式,保存到本地
复制代码

以上所述是小编给大家介绍的asp.net中DES加密与解密MD5加密帮助类,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对程序员之家网站的支持!



回复

使用道具 举报