OtherEncrypt.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Security.Cryptography;
  2. using System.Text;
  3. using UnityEngine;
  4. namespace WS
  5. {
  6. ///<summary>异或 Sha1</summary>
  7. public class OtherEncrypt
  8. {
  9. /// <summary>异或因子</summary>
  10. private static readonly byte[] xorScale = new byte[] { 45, 66, 38, 55, 23, 254, 9, 165, 90, 19, 41, 45, 201, 58, 55, 37, 254, 185, 165, 169, 19, 171 };
  11. /// <summary>随机字符串</summary>
  12. private static readonly string[] RandomScale = new string[]
  13. {
  14. "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
  15. "*","+","-","/",".",">","<","&","%","#","@","$","!","^",
  16. "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
  17. "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
  18. };
  19. ///<summary>异或</summary>
  20. public static byte[] Xor(byte[] buffer)
  21. {
  22. int iScaleLen = xorScale.Length;
  23. for (int i = 0; i < buffer.Length; i++)
  24. {
  25. buffer[i] = (byte)(buffer[i] ^ xorScale[i % iScaleLen]);
  26. }
  27. return buffer;
  28. }
  29. ///<summary>Sha1 加密</summary>
  30. public static string Sha1(string str)
  31. {
  32. byte[] buffer = Encoding.Default.GetBytes(str);
  33. byte[] data = SHA1.Create().ComputeHash(buffer);
  34. StringBuilder sub = new StringBuilder();
  35. foreach (var t in data)
  36. {
  37. sub.Append(t.ToString("X2"));
  38. }
  39. return sub.ToString();
  40. }
  41. ///<summary>获取随机字符串</summary>
  42. public static string GetRandomString(int count)
  43. {
  44. int maxcount = RandomScale.Length;
  45. StringBuilder sb = new StringBuilder();
  46. for (int i = 0; i < count; i++)
  47. {
  48. int index = Random.Range(0, maxcount);
  49. sb.Append(RandomScale[index]);
  50. }
  51. return sb.ToString();
  52. }
  53. }
  54. }