HashEncode.cs 1.3 KB

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