miduo_client/Assets/LuaFramework/Scripts/Utility/MEncyptUtil.cs

142 lines
3.8 KiB
C#
Raw Normal View History

2021-12-01 17:24:36 +08:00
using UnityEngine;
using System.Collections.Generic;
using System.Reflection;
using LuaInterface;
using System;
using System.IO;
using UnityEditor;
namespace GameLogic {
public static class MEncyptUtil {
//
//加密/解密
static byte[] crypt(byte[] data, string key)
{
int[] ri = new int[data.Length];
byte[] cl = System.Text.Encoding.Default.GetBytes(key);
for(long i = 0; i<data.Length; i++)
{
ri[i] = data[i] ^ cl[i % cl.Length];
}
byte[] rb = new byte[ri.Length * sizeof(int)];
Buffer.BlockCopy(ri, 0, rb, 0, rb.Length);
byte[] r = new byte[ri.Length];
int x = 0;
for (int i = 0; i < rb.Length; i++)
{
if(i%4 == 0)
{
r[x++] = rb[i];
}
}
return r;
}
// 加载AB包
public static byte[] DecyptAB(byte[] data, string key, ulong offset)
{
byte[] rb ;
if (key == null || key == "")
{
2021-12-01 21:32:16 +08:00
rb = data;
2021-12-01 17:24:36 +08:00
}
else {
rb = crypt(data, key);
}
byte[] r = new byte[rb.Length - (int)offset];
for (int i = (int)offset; i < rb.Length; i++)
{
r[i - (int)offset] = rb[i];
}
return r;
}
2021-12-01 21:32:16 +08:00
// 文件加密
public static void EncyptAB(string path, string key)
2021-12-01 17:24:36 +08:00
{
2021-12-01 21:32:16 +08:00
// 闪屏先不加密了
if (Path.GetFileName(path).Equals("splashpanel.unity3d"))
2021-12-01 17:24:36 +08:00
{
2021-12-01 21:32:16 +08:00
return;
2021-12-01 17:24:36 +08:00
}
2021-12-01 21:32:16 +08:00
if (key == null || key == "")
{
return;
}
if (!File.Exists(path))
{
return;
}
byte[] data = File.ReadAllBytes(path);
byte[] rb = crypt(data, key);
2021-12-01 17:24:36 +08:00
2021-12-01 21:32:16 +08:00
File.WriteAllBytes(path, rb);
}
2021-12-01 17:24:36 +08:00
public static AssetBundle LoadAssetBundle(string path, string encryptKey, ulong offset)
{
AssetBundle ab;
if (AppConst.PlatformPath.Equals("Android"))
{
ab = AssetBundle.LoadFromFile(path, 0, offset);
}
else
{
ab = AssetBundle.LoadFromMemory(GameLogic.MEncyptUtil.DecyptAB(File.ReadAllBytes(path), encryptKey, offset));
}
return ab;
}
2021-12-01 17:24:36 +08:00
public static AssetBundleCreateRequest LoadAssetBundleAsync(string path, string encryptKey, ulong offset)
{
AssetBundleCreateRequest ab;
if (AppConst.PlatformPath.Equals("Android"))
{
ab = AssetBundle.LoadFromFileAsync(path, 0, offset);
}
else
{
ab = AssetBundle.LoadFromMemoryAsync(GameLogic.MEncyptUtil.DecyptAB(File.ReadAllBytes(path), encryptKey, offset));
}
return ab;
}
2021-12-01 21:32:16 +08:00
//[MenuItem("Build/TEST")]
//static void load()
//{
// byte[] a = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
// string s = "";
// for(int i = 0;i < a.Length; i++)
// {
// s += a[i];
// }
// XDebug.Log.l("a = " + s);
2021-12-01 17:24:36 +08:00
2021-12-01 21:32:16 +08:00
// byte[] b = crypt(a, "key");
// s = "";
// for (int i = 0; i < b.Length; i++)
// {
// s += b[i];
// }
// XDebug.Log.l("b = " + s);
2021-12-01 17:24:36 +08:00
2021-12-01 21:32:16 +08:00
// byte[] c = crypt(b, "key");
// s = "";
// for (int i = 0; i < c.Length; i++)
// {
// s += c[i];
// }
// XDebug.Log.l("c = " + s);
// //File.ReadAllBytes("");
//}
2021-12-01 17:24:36 +08:00
}
}