| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- using System;
- using System.Security.Cryptography;
- using System.Text;
- using UnityEngine;
- #if UNITY_EDITOR
- using UnityEditor;
- public class CryptoTools : EditorWindow
- {
- private string input = string.Empty;
- private string output = string.Empty;
- private string errorMessage = string.Empty;
- private Vector2 inputScrollPosition;
- private Vector2 outputScrollPosition;
- [MenuItem("Tools/Crypto Tools")]
- private static void OpenWindow()
- {
- var window = GetWindow<CryptoTools>("Crypto Tools");
- window.minSize = new Vector2(520f, 420f);
- window.Show();
- }
- private void OnGUI()
- {
- EditorGUILayout.Space(8f);
- EditorGUILayout.LabelField("ClientCrypto 字符串加密/解密", EditorStyles.boldLabel);
- EditorGUILayout.Space(4f);
- EditorGUILayout.LabelField("输入");
- inputScrollPosition = EditorGUILayout.BeginScrollView(
- inputScrollPosition,
- GUILayout.MinHeight(120f));
- input = EditorGUILayout.TextArea(input, GUILayout.ExpandHeight(true));
- EditorGUILayout.EndScrollView();
- EditorGUILayout.Space(6f);
- using (new EditorGUILayout.HorizontalScope())
- {
- if (GUILayout.Button("加密", GUILayout.Height(30f)))
- {
- TransformInput(ClientCrypto.EncryptString);
- }
- if (GUILayout.Button("解密", GUILayout.Height(30f)))
- {
- TransformInput(ClientCrypto.DecryptString);
- }
- if (GUILayout.Button("清空", GUILayout.Width(80f), GUILayout.Height(30f)))
- {
- input = string.Empty;
- output = string.Empty;
- errorMessage = string.Empty;
- GUI.FocusControl(null);
- }
- }
- if (!string.IsNullOrEmpty(errorMessage))
- {
- EditorGUILayout.Space(6f);
- EditorGUILayout.HelpBox(errorMessage, MessageType.Error);
- }
- EditorGUILayout.Space(8f);
- EditorGUILayout.LabelField("输出");
- outputScrollPosition = EditorGUILayout.BeginScrollView(
- outputScrollPosition,
- GUILayout.MinHeight(120f));
- output = EditorGUILayout.TextArea(output, GUILayout.ExpandHeight(true));
- EditorGUILayout.EndScrollView();
- using (new EditorGUI.DisabledScope(string.IsNullOrEmpty(output)))
- {
- using (new EditorGUILayout.HorizontalScope())
- {
- if (GUILayout.Button("复制输出"))
- {
- EditorGUIUtility.systemCopyBuffer = output;
- ShowNotification(new GUIContent("已复制"));
- }
- if (GUILayout.Button("输出作为输入"))
- {
- input = output;
- errorMessage = string.Empty;
- }
- }
- }
- }
- private void TransformInput(Func<string, string> transform)
- {
- try
- {
- output = transform(input);
- errorMessage = string.Empty;
- GUI.FocusControl(null);
- }
- catch (Exception exception)
- {
- output = string.Empty;
- errorMessage = exception.Message;
- }
- }
- }
- #endif
- public static class ClientCrypto
- {
- private const string Secret = "ios-platform-api-v2";
- public static string EncryptString(string value)
- {
- using (var sha256 = SHA256.Create())
- using (var aes = Aes.Create())
- {
- aes.Key = sha256.ComputeHash(Encoding.UTF8.GetBytes(Secret));
- aes.Mode = CipherMode.CBC;
- aes.Padding = PaddingMode.PKCS7;
- aes.GenerateIV();
- byte[] plainBytes = Encoding.UTF8.GetBytes(value);
- byte[] cipherBytes;
- using (var encryptor = aes.CreateEncryptor())
- {
- cipherBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
- }
- byte[] output = new byte[aes.IV.Length + cipherBytes.Length];
- Buffer.BlockCopy(aes.IV, 0, output, 0, aes.IV.Length);
- Buffer.BlockCopy(cipherBytes, 0, output, aes.IV.Length, cipherBytes.Length);
- return "v2:" + Base64UrlEncode(output);
- }
- }
- public static string DecryptString(string value)
- {
- if (string.IsNullOrEmpty(value) || !value.StartsWith("v2:"))
- {
- throw new ArgumentException("加密字符串格式不正确 >> " + value);
- }
- byte[] bytes = Base64UrlDecode(value.Substring(3));
- byte[] iv = new byte[16];
- byte[] cipherBytes = new byte[bytes.Length - 16];
- Buffer.BlockCopy(bytes, 0, iv, 0, 16);
- Buffer.BlockCopy(bytes, 16, cipherBytes, 0, cipherBytes.Length);
- using (var sha256 = SHA256.Create())
- using (var aes = Aes.Create())
- {
- aes.Key = sha256.ComputeHash(Encoding.UTF8.GetBytes(Secret));
- aes.IV = iv;
- aes.Mode = CipherMode.CBC;
- aes.Padding = PaddingMode.PKCS7;
- using (var decryptor = aes.CreateDecryptor())
- {
- byte[] plainBytes = decryptor.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
- return Encoding.UTF8.GetString(plainBytes);
- }
- }
- }
- public static string EncryptJson(string json)
- {
- return EncryptString(json);
- }
- public static string DecryptJson(string encrypted)
- {
- try
- {
- return DecryptString(encrypted);
- }
- catch (Exception e)
- {
- Debug.LogError($"error: {e}");
- return string.Empty;
- }
- }
- private static string Base64UrlEncode(byte[] bytes)
- {
- return Convert.ToBase64String(bytes)
- .TrimEnd('=')
- .Replace('+', '-')
- .Replace('/', '_');
- }
- private static byte[] Base64UrlDecode(string value)
- {
- string base64 = value.Replace('-', '+').Replace('_', '/');
- switch (base64.Length % 4)
- {
- case 2:
- base64 += "==";
- break;
- case 3:
- base64 += "=";
- break;
- }
- return Convert.FromBase64String(base64);
- }
- }
|