【XPIOS资源加密】优化加密方式,还是使用头部加密,其他方式都会导致游戏卡顿

dev_chengFeng
JLIOSM1 2021-12-07 16:17:29 +08:00 committed by gaoxin
parent 86f1620e7d
commit acbd814a89
1 changed files with 131 additions and 45 deletions

View File

@ -5,35 +5,78 @@ using LuaInterface;
using System;
using System.IO;
using UnityEditor;
using System.Security.Cryptography;
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++)
byte[] cl = System.Text.Encoding.Default.GetBytes(key);
byte[] r = new byte[data.Length];
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)
if (i % 4 == 0)
{
r[x++] = rb[i];
r[i] = BitConverter.GetBytes(data[i] ^ cl[i % cl.Length])[0];
}
else
{
r[i] = data[i];
}
}
return r;
}
// AES加密
static byte[] encryptAES(byte[] data, string key)
{
RijndaelManaged rij = new RijndaelManaged();
rij.Key = System.Text.Encoding.Default.GetBytes(key);
rij.Mode = CipherMode.ECB;
rij.Padding = PaddingMode.PKCS7;
ICryptoTransform ctr = rij.CreateEncryptor();
return ctr.TransformFinalBlock(data, 0, data.Length);
}
static byte[] decryptAES(byte[] data, string key)
{
RijndaelManaged rij = new RijndaelManaged();
rij.Key = System.Text.Encoding.Default.GetBytes(key);
rij.Mode = CipherMode.ECB;
rij.Padding = PaddingMode.PKCS7;
ICryptoTransform ctr = rij.CreateDecryptor();
return ctr.TransformFinalBlock(data, 0, data.Length);
}
// 头部混淆
static byte[] encryptHead(byte[] data, string key)
{
byte[] cl = System.Text.Encoding.Default.GetBytes(key);
byte[] r = new byte[data.Length + cl.Length];
long x = 0;
for (long i = 0; i < cl.Length; i++)
{
r[x++] = cl[i];
}
for (long i = 0; i < data.Length; i++)
{
r[x++] = data[i];
}
return r;
}
// 头部混淆
static byte[] decryptHead(byte[] data, string key)
{
byte[] cl = System.Text.Encoding.Default.GetBytes(key);
byte[] r = new byte[data.Length - cl.Length];
long x = 0;
for (long i = cl.Length; i < data.Length; i++)
{
r[x++] = data[i];
}
return r;
}
// 加载AB包
@ -41,18 +84,23 @@ namespace GameLogic {
{
byte[] rb ;
//System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
//sw.Start();
if (key == null || key == "")
{
rb = data;
}
else {
rb = crypt(data, key);
rb = decryptHead(data, key);
}
//Debug.Log("_"+sw.Elapsed.TotalMilliseconds);
byte[] r = new byte[rb.Length - (int)offset];
for (int i = (int)offset; i < rb.Length; i++)
{
r[i - (int)offset] = rb[i];
}
//Debug.Log("_"+sw.Elapsed.TotalMilliseconds);
//sw.Stop();
return r;
}
// 文件加密
@ -72,7 +120,7 @@ namespace GameLogic {
return;
}
byte[] data = File.ReadAllBytes(path);
byte[] rb = crypt(data, key);
byte[] rb = encryptHead(data, key);
File.WriteAllBytes(path, rb);
}
@ -86,7 +134,10 @@ namespace GameLogic {
}
else
{
ab = AssetBundle.LoadFromMemory(GameLogic.MEncyptUtil.DecyptAB(File.ReadAllBytes(path), encryptKey, offset));
//ab = AssetBundle.LoadFromMemory(DecyptAB(File.ReadAllBytes(path), encryptKey, offset));
if (encryptKey == null) encryptKey = "";
byte[] cl = System.Text.Encoding.Default.GetBytes(encryptKey);
ab = AssetBundle.LoadFromFile(path, 0, (ulong)(cl.Length + (int)offset));
}
return ab;
}
@ -100,43 +151,78 @@ namespace GameLogic {
}
else
{
ab = AssetBundle.LoadFromMemoryAsync(GameLogic.MEncyptUtil.DecyptAB(File.ReadAllBytes(path), encryptKey, offset));
//ab = AssetBundle.LoadFromMemoryAsync(DecyptAB(File.ReadAllBytes(path), encryptKey, offset));
byte[] cl = System.Text.Encoding.Default.GetBytes(encryptKey);
ab = AssetBundle.LoadFromFileAsync(path, 0, (ulong)(cl.Length + (int)offset));
}
return ab;
}
//[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);
static void Test()
{
AssetBundle.UnloadAllAssetBundles(true);
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
byte[] b = File.ReadAllBytes("/Users/ljsd/Documents/JieLing/Packager/IOS/TCX_CN_XIPU/Data/Raw/IOS/lz4_atlas_dyact_activity4.unity3d");
Debug.Log(sw.Elapsed.TotalMilliseconds);
byte[] d = DecyptAB(b, "ae125efkk4_54eeff444ferfkny6oxi8", 128);
Debug.Log(sw.Elapsed.TotalMilliseconds);
//AssetBundle.LoadFromMemory(d);
//Debug.Log(sw.Elapsed.TotalMilliseconds);
sw.Stop();
// //AssetBundle.UnloadAllAssetBundles(true);
// //sw.Start();
// //AssetBundle.LoadFromFile("/Users/ljsd/Documents/JieLing/UnityProject/JL_china_ios/BuildABs/IOS/lz4_atlas_dyact_activity4.unity3d", 0, 128);
// //Debug.Log(sw.Elapsed.TotalMilliseconds);
// //sw.Stop();
// //AssetBundle.UnloadAllAssetBundles(true);
// //sw.Start();
// //byte[] bb = File.ReadAllBytes("/Users/ljsd/Documents/JieLing/UnityProject/JL_china_ios/BuildABs/IOS/lz4_atlas_dyact_activity4.unity3d");
// //Debug.Log(sw.Elapsed.TotalMilliseconds);
// //byte[] dd = DecyptAB(bb, "", 128);
// //Debug.Log(sw.Elapsed.TotalMilliseconds);
// //AssetBundle.LoadFromMemory(dd);
// //Debug.Log(sw.Elapsed.TotalMilliseconds);
// //sw.Stop();
}
//[MenuItem("Build/TEST2")]
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);
// byte[] b = crypt(a, "key");
// s = "";
// for (int i = 0; i < b.Length; i++)
// {
// s += b[i];
// }
// XDebug.Log.l("b = " + s);
byte[] b = encryptHead(a, "ae125efkk4_54eeff444ferfkny6oxi8");
s = "";
for (int i = 0; i < b.Length; i++)
{
s += b[i];
}
XDebug.Log.l("b = " + s);
// byte[] c = crypt(b, "key");
// s = "";
// for (int i = 0; i < c.Length; i++)
// {
// s += c[i];
// }
// XDebug.Log.l("c = " + s);
byte[] c = decryptHead(b, "ae125efkk4_54eeff444ferfkny6oxi8");
s = "";
for (int i = 0; i < c.Length; i++)
{
s += c[i];
}
XDebug.Log.l("c = " + s);
// //File.ReadAllBytes("");
//}
//File.ReadAllBytes("");
}
}
}