ClientCrypto.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. using UnityEngine;
  5. #if UNITY_EDITOR
  6. using UnityEditor;
  7. public class CryptoTools : EditorWindow
  8. {
  9. private string input = string.Empty;
  10. private string output = string.Empty;
  11. private string errorMessage = string.Empty;
  12. private Vector2 inputScrollPosition;
  13. private Vector2 outputScrollPosition;
  14. [MenuItem("Tools/Crypto Tools")]
  15. private static void OpenWindow()
  16. {
  17. var window = GetWindow<CryptoTools>("Crypto Tools");
  18. window.minSize = new Vector2(520f, 420f);
  19. window.Show();
  20. }
  21. private void OnGUI()
  22. {
  23. EditorGUILayout.Space(8f);
  24. EditorGUILayout.LabelField("ClientCrypto 字符串加密/解密", EditorStyles.boldLabel);
  25. EditorGUILayout.Space(4f);
  26. EditorGUILayout.LabelField("输入");
  27. inputScrollPosition = EditorGUILayout.BeginScrollView(
  28. inputScrollPosition,
  29. GUILayout.MinHeight(120f));
  30. input = EditorGUILayout.TextArea(input, GUILayout.ExpandHeight(true));
  31. EditorGUILayout.EndScrollView();
  32. EditorGUILayout.Space(6f);
  33. using (new EditorGUILayout.HorizontalScope())
  34. {
  35. if (GUILayout.Button("加密", GUILayout.Height(30f)))
  36. {
  37. TransformInput(ClientCrypto.EncryptString);
  38. }
  39. if (GUILayout.Button("解密", GUILayout.Height(30f)))
  40. {
  41. TransformInput(ClientCrypto.DecryptString);
  42. }
  43. if (GUILayout.Button("清空", GUILayout.Width(80f), GUILayout.Height(30f)))
  44. {
  45. input = string.Empty;
  46. output = string.Empty;
  47. errorMessage = string.Empty;
  48. GUI.FocusControl(null);
  49. }
  50. }
  51. if (!string.IsNullOrEmpty(errorMessage))
  52. {
  53. EditorGUILayout.Space(6f);
  54. EditorGUILayout.HelpBox(errorMessage, MessageType.Error);
  55. }
  56. EditorGUILayout.Space(8f);
  57. EditorGUILayout.LabelField("输出");
  58. outputScrollPosition = EditorGUILayout.BeginScrollView(
  59. outputScrollPosition,
  60. GUILayout.MinHeight(120f));
  61. output = EditorGUILayout.TextArea(output, GUILayout.ExpandHeight(true));
  62. EditorGUILayout.EndScrollView();
  63. using (new EditorGUI.DisabledScope(string.IsNullOrEmpty(output)))
  64. {
  65. using (new EditorGUILayout.HorizontalScope())
  66. {
  67. if (GUILayout.Button("复制输出"))
  68. {
  69. EditorGUIUtility.systemCopyBuffer = output;
  70. ShowNotification(new GUIContent("已复制"));
  71. }
  72. if (GUILayout.Button("输出作为输入"))
  73. {
  74. input = output;
  75. errorMessage = string.Empty;
  76. }
  77. }
  78. }
  79. }
  80. private void TransformInput(Func<string, string> transform)
  81. {
  82. try
  83. {
  84. output = transform(input);
  85. errorMessage = string.Empty;
  86. GUI.FocusControl(null);
  87. }
  88. catch (Exception exception)
  89. {
  90. output = string.Empty;
  91. errorMessage = exception.Message;
  92. }
  93. }
  94. }
  95. #endif
  96. public static class ClientCrypto
  97. {
  98. private const string Secret = "ios-platform-api-v2";
  99. public static string EncryptString(string value)
  100. {
  101. using (var sha256 = SHA256.Create())
  102. using (var aes = Aes.Create())
  103. {
  104. aes.Key = sha256.ComputeHash(Encoding.UTF8.GetBytes(Secret));
  105. aes.Mode = CipherMode.CBC;
  106. aes.Padding = PaddingMode.PKCS7;
  107. aes.GenerateIV();
  108. byte[] plainBytes = Encoding.UTF8.GetBytes(value);
  109. byte[] cipherBytes;
  110. using (var encryptor = aes.CreateEncryptor())
  111. {
  112. cipherBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
  113. }
  114. byte[] output = new byte[aes.IV.Length + cipherBytes.Length];
  115. Buffer.BlockCopy(aes.IV, 0, output, 0, aes.IV.Length);
  116. Buffer.BlockCopy(cipherBytes, 0, output, aes.IV.Length, cipherBytes.Length);
  117. return "v2:" + Base64UrlEncode(output);
  118. }
  119. }
  120. public static string DecryptString(string value)
  121. {
  122. if (string.IsNullOrEmpty(value) || !value.StartsWith("v2:"))
  123. {
  124. throw new ArgumentException("加密字符串格式不正确 >> " + value);
  125. }
  126. byte[] bytes = Base64UrlDecode(value.Substring(3));
  127. byte[] iv = new byte[16];
  128. byte[] cipherBytes = new byte[bytes.Length - 16];
  129. Buffer.BlockCopy(bytes, 0, iv, 0, 16);
  130. Buffer.BlockCopy(bytes, 16, cipherBytes, 0, cipherBytes.Length);
  131. using (var sha256 = SHA256.Create())
  132. using (var aes = Aes.Create())
  133. {
  134. aes.Key = sha256.ComputeHash(Encoding.UTF8.GetBytes(Secret));
  135. aes.IV = iv;
  136. aes.Mode = CipherMode.CBC;
  137. aes.Padding = PaddingMode.PKCS7;
  138. using (var decryptor = aes.CreateDecryptor())
  139. {
  140. byte[] plainBytes = decryptor.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
  141. return Encoding.UTF8.GetString(plainBytes);
  142. }
  143. }
  144. }
  145. public static string EncryptJson(string json)
  146. {
  147. return EncryptString(json);
  148. }
  149. public static string DecryptJson(string encrypted)
  150. {
  151. try
  152. {
  153. return DecryptString(encrypted);
  154. }
  155. catch (Exception e)
  156. {
  157. Debug.LogError($"error: {e}");
  158. return string.Empty;
  159. }
  160. }
  161. private static string Base64UrlEncode(byte[] bytes)
  162. {
  163. return Convert.ToBase64String(bytes)
  164. .TrimEnd('=')
  165. .Replace('+', '-')
  166. .Replace('/', '_');
  167. }
  168. private static byte[] Base64UrlDecode(string value)
  169. {
  170. string base64 = value.Replace('-', '+').Replace('_', '/');
  171. switch (base64.Length % 4)
  172. {
  173. case 2:
  174. base64 += "==";
  175. break;
  176. case 3:
  177. base64 += "=";
  178. break;
  179. }
  180. return Convert.FromBase64String(base64);
  181. }
  182. }