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

95 lines
2.2 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 == "")
{
rb = crypt(data, key);
}
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;
}
[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);
byte[] b = crypt(a, "key");
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);
//File.ReadAllBytes("");
}
}
}