ClientCrypto.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #if UNITY_EDITOR
  2. using System;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. using UnityEngine;
  6. public static class ClientCrypto
  7. {
  8. private const string Secret = "ios-platform-api-v2";
  9. public static string EncryptString(string value)
  10. {
  11. using (var sha256 = SHA256.Create())
  12. using (var aes = Aes.Create())
  13. {
  14. aes.Key = sha256.ComputeHash(Encoding.UTF8.GetBytes(Secret));
  15. aes.Mode = CipherMode.CBC;
  16. aes.Padding = PaddingMode.PKCS7;
  17. aes.GenerateIV();
  18. byte[] plainBytes = Encoding.UTF8.GetBytes(value);
  19. byte[] cipherBytes;
  20. using (var encryptor = aes.CreateEncryptor())
  21. {
  22. cipherBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
  23. }
  24. byte[] output = new byte[aes.IV.Length + cipherBytes.Length];
  25. Buffer.BlockCopy(aes.IV, 0, output, 0, aes.IV.Length);
  26. Buffer.BlockCopy(cipherBytes, 0, output, aes.IV.Length, cipherBytes.Length);
  27. return "v2:" + Base64UrlEncode(output);
  28. }
  29. }
  30. public static string DecryptString(string value)
  31. {
  32. if (string.IsNullOrEmpty(value) || !value.StartsWith("v2:"))
  33. {
  34. throw new ArgumentException("Invalid encrypted string format >> " + value);
  35. }
  36. byte[] bytes = Base64UrlDecode(value.Substring(3));
  37. byte[] iv = new byte[16];
  38. byte[] cipherBytes = new byte[bytes.Length - 16];
  39. Buffer.BlockCopy(bytes, 0, iv, 0, 16);
  40. Buffer.BlockCopy(bytes, 16, cipherBytes, 0, cipherBytes.Length);
  41. using (var sha256 = SHA256.Create())
  42. using (var aes = Aes.Create())
  43. {
  44. aes.Key = sha256.ComputeHash(Encoding.UTF8.GetBytes(Secret));
  45. aes.IV = iv;
  46. aes.Mode = CipherMode.CBC;
  47. aes.Padding = PaddingMode.PKCS7;
  48. using (var decryptor = aes.CreateDecryptor())
  49. {
  50. byte[] plainBytes = decryptor.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
  51. return Encoding.UTF8.GetString(plainBytes);
  52. }
  53. }
  54. }
  55. public static string EncryptJson(string json)
  56. {
  57. return EncryptString(json);
  58. }
  59. public static string DecryptJson(string encrypted)
  60. {
  61. try
  62. {
  63. return DecryptString(encrypted);
  64. }
  65. catch (Exception e)
  66. {
  67. Debug.LogError($"error: {e}");
  68. return string.Empty;
  69. }
  70. }
  71. private static string Base64UrlEncode(byte[] bytes)
  72. {
  73. return Convert.ToBase64String(bytes)
  74. .TrimEnd('=')
  75. .Replace('+', '-')
  76. .Replace('/', '_');
  77. }
  78. private static byte[] Base64UrlDecode(string value)
  79. {
  80. string base64 = value.Replace('-', '+').Replace('_', '/');
  81. switch (base64.Length % 4)
  82. {
  83. case 2:
  84. base64 += "==";
  85. break;
  86. case 3:
  87. base64 += "=";
  88. break;
  89. }
  90. return Convert.FromBase64String(base64);
  91. }
  92. }
  93. #endif