Tool.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. using UnityEngine;
  6. namespace TapTap.AntiAddiction.Internal
  7. {
  8. public static class Tool
  9. {
  10. public static int GetMonthDayCount(int year, int month)
  11. {
  12. switch (month)
  13. {
  14. case 1:
  15. case 3:
  16. case 5:
  17. case 7:
  18. case 8:
  19. case 10:
  20. case 12:
  21. return 31;
  22. case 2:
  23. return IsLeapYear(year) ? 29 : 28;
  24. default:
  25. return 30;
  26. }
  27. }
  28. public static bool IsLeapYear(int year)
  29. {
  30. return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
  31. }
  32. public static string EncryptString(string str)
  33. {
  34. MD5 md5 = MD5.Create();
  35. // 将字符串转换成字节数组
  36. byte[] byteOld = Encoding.UTF8.GetBytes(str);
  37. // 调用加密方法
  38. byte[] byteNew = md5.ComputeHash(byteOld);
  39. // 将加密结果转换为字符串
  40. StringBuilder sb = new StringBuilder();
  41. foreach (byte b in byteNew)
  42. {
  43. // 将字节转换成16进制表示的字符串,
  44. sb.Append(b.ToString("x2"));
  45. }
  46. // 返回加密的字符串
  47. return sb.ToString();
  48. }
  49. /// <summary>
  50. /// Encrypt the content with public key (in string)
  51. /// </summary>
  52. /// <param name="input"></param>
  53. /// <param name="key"></param>
  54. /// <returns></returns>
  55. public static string RsaEncrypt(string input, string key)
  56. {
  57. //https://stackoverflow.com/questions/11506891/how-to-load-the-rsa-public-key-from-file-in-c-sharp
  58. try
  59. {
  60. var base64Info = Convert.FromBase64String(key);
  61. RSACryptoServiceProvider rsa = DecodeX509PublicKey(base64Info);
  62. var encodeInfo = Encoding.UTF8.GetBytes(input);
  63. var encryptInfo = rsa.Encrypt(encodeInfo, false);
  64. return Convert.ToBase64String(encryptInfo);
  65. }
  66. catch (Exception e)
  67. {
  68. Debug.LogErrorFormat($"RSA Encrypt Error! input: {input} key: {key}/n Msg: {e.Message}");
  69. }
  70. return null;
  71. }
  72. private static bool CompareBytearrays(byte[] a, byte[] b)
  73. {
  74. if (a.Length != b.Length)
  75. return false;
  76. int i = 0;
  77. foreach (byte c in a)
  78. {
  79. if (c != b[i])
  80. return false;
  81. i++;
  82. }
  83. return true;
  84. }
  85. private static RSACryptoServiceProvider DecodeX509PublicKey(byte[] x509Key)
  86. {
  87. // encoded OID sequence for PKCS #1 rsaEncryption szOID_RSA_RSA = "1.2.840.113549.1.1.1"
  88. byte[] seqOid = { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00 };
  89. // --------- Set up stream to read the asn.1 encoded SubjectPublicKeyInfo blob ------
  90. MemoryStream mem = new MemoryStream(x509Key);
  91. BinaryReader binaryReader = new BinaryReader(mem); //wrap Memory Stream with BinaryReader for easy reading
  92. try
  93. {
  94. var twoBytes = binaryReader.ReadUInt16();
  95. if (twoBytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
  96. binaryReader.ReadByte(); //advance 1 byte
  97. else if (twoBytes == 0x8230)
  98. binaryReader.ReadInt16(); //advance 2 bytes
  99. else
  100. return null;
  101. var seq = binaryReader.ReadBytes(15);
  102. if (!CompareBytearrays(seq, seqOid)) //make sure Sequence for OID is correct
  103. return null;
  104. twoBytes = binaryReader.ReadUInt16();
  105. if (twoBytes == 0x8103) //data read as little endian order (actual data order for Bit String is 03 81)
  106. binaryReader.ReadByte(); //advance 1 byte
  107. else if (twoBytes == 0x8203)
  108. binaryReader.ReadInt16(); //advance 2 bytes
  109. else
  110. return null;
  111. var bt = binaryReader.ReadByte();
  112. if (bt != 0x00) //expect null byte next
  113. return null;
  114. twoBytes = binaryReader.ReadUInt16();
  115. if (twoBytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
  116. binaryReader.ReadByte(); //advance 1 byte
  117. else if (twoBytes == 0x8230)
  118. binaryReader.ReadInt16(); //advance 2 bytes
  119. else
  120. return null;
  121. twoBytes = binaryReader.ReadUInt16();
  122. byte lowByte;
  123. byte highByte = 0x00;
  124. if (twoBytes == 0x8102) //data read as little endian order (actual data order for Integer is 02 81)
  125. lowByte = binaryReader.ReadByte(); // read next bytes which is bytes in modulus
  126. else if (twoBytes == 0x8202)
  127. {
  128. highByte = binaryReader.ReadByte(); //advance 2 bytes
  129. lowByte = binaryReader.ReadByte();
  130. }
  131. else
  132. return null;
  133. byte[] modInt = { lowByte, highByte, 0x00, 0x00 }; //reverse byte order since asn.1 key uses big endian order
  134. int modSize = BitConverter.ToInt32(modInt, 0);
  135. byte firstByte = binaryReader.ReadByte();
  136. binaryReader.BaseStream.Seek(-1, SeekOrigin.Current);
  137. if (firstByte == 0x00)
  138. { //if first byte (highest order) of modulus is zero, don't include it
  139. binaryReader.ReadByte(); //skip this null byte
  140. modSize -= 1; //reduce modulus buffer size by 1
  141. }
  142. byte[] modulus = binaryReader.ReadBytes(modSize); //read the modulus bytes
  143. if (binaryReader.ReadByte() != 0x02) //expect an Integer for the exponent data
  144. return null;
  145. int expBytes = binaryReader.ReadByte(); // should only need one byte for actual exponent data (for all useful values)
  146. byte[] exponent = binaryReader.ReadBytes(expBytes);
  147. // ------- create RSACryptoServiceProvider instance and initialize with public key -----
  148. RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
  149. RSAParameters rsaKeyInfo = new RSAParameters
  150. {
  151. Modulus = modulus,
  152. Exponent = exponent
  153. };
  154. rsa.ImportParameters(rsaKeyInfo);
  155. return rsa;
  156. }
  157. catch (Exception)
  158. {
  159. return null;
  160. }
  161. finally { binaryReader.Close(); }
  162. }
  163. }
  164. }