远程参数加解密
This commit is contained in:
parent
3e91855d79
commit
f243a3703e
|
@ -6,6 +6,9 @@ public class AppSDKManager : MonoBehaviour
|
|||
{
|
||||
public static AppSDKManager Instance;
|
||||
|
||||
//包名
|
||||
public const string PackageName = "com.rush.cash.earn.fast.real.money.game";
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Instance = this;
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using AdjustSdk;
|
||||
using Firebase;
|
||||
using System.Globalization;
|
||||
using Firebase.Extensions;
|
||||
using Firebase.RemoteConfig;
|
||||
using Script.Utils;
|
||||
using SDK.Utils;
|
||||
using UnityEngine;
|
||||
|
||||
public class FireBaseRemoteConfigManager : NormalSingleton<FireBaseRemoteConfigManager>
|
||||
|
@ -41,19 +40,17 @@ public class FireBaseRemoteConfigManager : NormalSingleton<FireBaseRemoteConfigM
|
|||
/// <param name="key"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<int> GetRemoteConfigInt(string key, int defaultValue = 0)
|
||||
public int GetRemoteConfigInt(string key, int defaultValue = 0, bool IsEncrypt = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
// await FirebaseRemoteConfig.DefaultInstance.FetchAsync(TimeSpan.Zero);
|
||||
// await FirebaseRemoteConfig.DefaultInstance.ActivateAsync();
|
||||
|
||||
string newKey = GetKey(key, IsEncrypt);
|
||||
// 获取值
|
||||
var configValue = FirebaseRemoteConfig.DefaultInstance.GetValue(key);
|
||||
var configValue = FirebaseRemoteConfig.DefaultInstance.GetValue(newKey);
|
||||
|
||||
if (configValue.Source == ValueSource.RemoteValue)
|
||||
{
|
||||
return (int)configValue.LongValue;
|
||||
return (int)GetValueLong(newKey, configValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -74,16 +71,16 @@ public class FireBaseRemoteConfigManager : NormalSingleton<FireBaseRemoteConfigM
|
|||
/// <param name="key"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
public string GetRemoteConfigString(string key, string defaultValue = "")
|
||||
public string GetRemoteConfigString(string key, string defaultValue = "", bool IsEncrypt = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
string newKey = GetKey(key, IsEncrypt);
|
||||
// 获取值
|
||||
var configValue = FirebaseRemoteConfig.DefaultInstance.GetValue(key);
|
||||
|
||||
var configValue = FirebaseRemoteConfig.DefaultInstance.GetValue(newKey);
|
||||
if (configValue.Source == ValueSource.RemoteValue)
|
||||
{
|
||||
return configValue.StringValue;
|
||||
return GetValueStr(newKey, configValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -104,16 +101,17 @@ public class FireBaseRemoteConfigManager : NormalSingleton<FireBaseRemoteConfigM
|
|||
/// <param name="key"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
public bool GetRemoteConfigBool(string key, bool defaultValue = false)
|
||||
public bool GetRemoteConfigBool(string key, bool defaultValue = false, bool IsEncrypt = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
string newKey = GetKey(key, IsEncrypt);
|
||||
// 获取值
|
||||
var configValue = FirebaseRemoteConfig.DefaultInstance.GetValue(key);
|
||||
var configValue = FirebaseRemoteConfig.DefaultInstance.GetValue(newKey);
|
||||
|
||||
if (configValue.Source == ValueSource.RemoteValue)
|
||||
{
|
||||
return configValue.BooleanValue;
|
||||
return GetValueBool(newKey, configValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -134,16 +132,17 @@ public class FireBaseRemoteConfigManager : NormalSingleton<FireBaseRemoteConfigM
|
|||
/// <param name="key"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
public float GetRemoteConfigBool(string key, float defaultValue = 0)
|
||||
public float GetRemoteConfigBool(string key, float defaultValue = 0, bool IsEncrypt = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
string newKey = GetKey(key, IsEncrypt);
|
||||
// 获取值
|
||||
var configValue = FirebaseRemoteConfig.DefaultInstance.GetValue(key);
|
||||
var configValue = FirebaseRemoteConfig.DefaultInstance.GetValue(newKey);
|
||||
|
||||
if (configValue.Source == ValueSource.RemoteValue)
|
||||
{
|
||||
return (float)configValue.DoubleValue;
|
||||
return (float)GetValueDouble(newKey, configValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -157,4 +156,48 @@ public class FireBaseRemoteConfigManager : NormalSingleton<FireBaseRemoteConfigM
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否是加密
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private bool IsEncrypt(string key)
|
||||
{
|
||||
return key.StartsWith("CCB", StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
private string GetKey(string key, bool isEncrypt)
|
||||
{
|
||||
//ccb开头 忽略大小写
|
||||
return isEncrypt ? $"ccb{EncryptionUtils.AesEncrypt(AppSDKManager.PackageName, key)}" : key;
|
||||
}
|
||||
|
||||
private string GetValueStr(string key, ConfigValue configValue)
|
||||
{
|
||||
return IsEncrypt(key) ? EncryptionUtils.AesDecrypt(AppSDKManager.PackageName, configValue.StringValue) : configValue.StringValue;
|
||||
}
|
||||
|
||||
private long GetValueLong(string key, ConfigValue configValue)
|
||||
{
|
||||
return IsEncrypt(key) ? long.Parse(EncryptionUtils.AesDecrypt(AppSDKManager.PackageName, configValue.StringValue)) : configValue.LongValue;
|
||||
}
|
||||
|
||||
private bool GetValueBool(string key, ConfigValue configValue)
|
||||
{
|
||||
return IsEncrypt(key) ? bool.Parse(EncryptionUtils.AesDecrypt(AppSDKManager.PackageName, configValue.StringValue)) : configValue.BooleanValue;
|
||||
}
|
||||
|
||||
private double GetValueDouble(string key, ConfigValue configValue)
|
||||
{
|
||||
if (IsEncrypt(key))
|
||||
{
|
||||
string valueStr = EncryptionUtils.AesDecrypt(AppSDKManager.PackageName, configValue.StringValue);
|
||||
if (float.TryParse(valueStr, NumberStyles.Float, CultureInfo.InvariantCulture, out float result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return configValue.DoubleValue;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
|
||||
namespace SDK.Utils
|
||||
{
|
||||
public static class EncryptionUtils
|
||||
{
|
||||
private static string GetKey(string packageName)
|
||||
{
|
||||
var keyBytes = Encoding.UTF8.GetBytes(packageName);
|
||||
byte[] keyHash;
|
||||
using (var md5 = MD5.Create())
|
||||
{
|
||||
keyHash = md5.ComputeHash(keyBytes);
|
||||
}
|
||||
return BitConverter.ToString(keyHash).Replace("-", "").ToUpper();
|
||||
}
|
||||
|
||||
// Aes 加密
|
||||
public static string AesEncrypt(string packageName, string content)
|
||||
{
|
||||
string key = GetKey(packageName);
|
||||
var contentBytes = Encoding.UTF8.GetBytes(content);
|
||||
var keyBytes = Encoding.UTF8.GetBytes(key);
|
||||
byte[] keyHash;
|
||||
using (var md5 = MD5.Create())
|
||||
{
|
||||
keyHash = md5.ComputeHash(keyBytes);
|
||||
}
|
||||
using var aesAlg = Aes.Create();
|
||||
aesAlg.Key = keyHash;
|
||||
aesAlg.Mode = CipherMode.ECB;
|
||||
aesAlg.Padding = PaddingMode.PKCS7;
|
||||
var encryptor = aesAlg.CreateEncryptor();
|
||||
var encryptedBytes = encryptor.TransformFinalBlock(contentBytes, 0, contentBytes.Length);
|
||||
return BitConverter.ToString(encryptedBytes).Replace("-", "").ToLower();
|
||||
}
|
||||
|
||||
// Aes 解密
|
||||
public static string AesDecrypt(string packageName, string encrypted)
|
||||
{
|
||||
string key = GetKey(packageName);
|
||||
var keyBytes = Encoding.UTF8.GetBytes(key);
|
||||
var encryptedBytes = new byte[encrypted.Length / 2];
|
||||
for (var i = 0; i < encryptedBytes.Length; i++)
|
||||
{
|
||||
encryptedBytes[i] = Convert.ToByte(encrypted.Substring(i * 2, 2), 16);
|
||||
}
|
||||
// 使用传统的MD5创建方式
|
||||
byte[] keyHash;
|
||||
using (var md5 = MD5.Create())
|
||||
{
|
||||
keyHash = md5.ComputeHash(keyBytes);
|
||||
}
|
||||
using var aesAlg = Aes.Create();
|
||||
aesAlg.Key = keyHash;
|
||||
aesAlg.Mode = CipherMode.ECB;
|
||||
aesAlg.Padding = PaddingMode.PKCS7;
|
||||
var decryptor = aesAlg.CreateDecryptor();
|
||||
var decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
|
||||
return Encoding.UTF8.GetString(decryptedBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 3073440da29b60344965c2aebf6ead1a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Reference in New Issue