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

228 lines
7.6 KiB
C#

using UnityEngine;
using System.Collections.Generic;
using System.Reflection;
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)
{
byte[] cl = System.Text.Encoding.Default.GetBytes(key);
byte[] r = new byte[data.Length];
for (long i = 0; i < data.Length; i++)
{
if (i % 4 == 0)
{
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包
public static byte[] DecyptAB(byte[] data, string key, ulong offset)
{
byte[] rb ;
//System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
//sw.Start();
if (key == null || key == "")
{
rb = data;
}
else {
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;
}
// 文件加密
public static void EncyptAB(string path, string key)
{
// 闪屏先不加密了
if (Path.GetFileName(path).Contains("splashpanel"))
{
return;
}
if (key == null || key == "")
{
return;
}
if (!File.Exists(path))
{
return;
}
byte[] data = File.ReadAllBytes(path);
byte[] rb = encryptHead(data, key);
File.WriteAllBytes(path, rb);
}
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(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;
}
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(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 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 = encryptHead(a, "ae125efkk4_54eeff444ferfkny6oxi8");
s = "";
for (int i = 0; i < b.Length; i++)
{
s += b[i];
}
XDebug.Log.l("b = " + 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("");
}
}
}