AB包简单加密,offset加密

offset加密


0.简单建个场景:

Scene:1个GameObject,1个UI

Assets:1个图片,1个Prefab,1个材质

image-20220309113851834


1.普通打包【未加密】

image-20220309113830606

  • AssetStudio验证

在这里插入图片描述

  • 游戏加载验证

    image-20220319191233245

2.AB包前加入乱码

​ 2.1 获取原AB包的HashCode

​ 2.2 提取HashCode作为参数计算偏移量offset:

for (int i = 0,iMax = hashchar.Length; i < 3; ++i)
{
	offset += (int)hashchar[i];
}

​ 2.3 从原数据中选取offset长度的数据作为假数据头RandomHead(这样不同的子包,每次加密,他们前offset位的数据都是随机的)

private static byte[] GetRandomHead(byte[] sourceData,int headLength)
    {
        byte[] head = new byte[headLength];
        int startIdx = 0;
        int randomParam = UnityEngine.Random.Range(0, sourceData.Length - headLength);
        for (int i = randomParam, iMax = randomParam + headLength; i < iMax; ++i,++startIdx)
        {
            head[startIdx] = sourceData[i];
        }
        return head;
    }

​ 2.4 把 RandomHead 写到原数据前,实现简单的offset加密

private static void SetOffset(ref byte[] buffer,byte[] head,byte[] sourceData)
    {
        for (int i = 0,iMax = sourceData.Length + head.Length; i < iMax; ++i)
        {
            if (i<head.Length)
            {
                buffer[i] = head[i];
            }
            else
            {
                buffer[i] = sourceData[i - head.Length];
            }
        }
    }

在这里插入图片描述

  • AssetStudio验证

    [黑Saber姐姐不见了]

image-20220319153046399

  • 游戏加载验证

    private Sprite LoadImg()
        {
            Sprite icon = null;
            string outputPath = Application.dataPath + "/BuildAssetBundle/Window/";
            AssetBundle.UnloadAllAssetBundles(true);
            AssetBundle ab = AssetBundle.LoadFromFile(outputPath + bundleName);
            icon = ab.LoadAsset<Sprite>(spriteName);
            return icon;
        }
    

    [黑Saber姐姐不见了] +1

    image-20220319191449324

3.解密

根据hash算出每个子包的offset,然后略过他们

private static void DelateOffset(ref byte[] buffer,int offset,byte[] sourceData)
    {
        for (int i = 0,iMax = sourceData.Length; i < iMax; ++i)
        {
            if (i < offset)
            {
                continue;
            }
            
            buffer[i - offset] = sourceData[i];
        }
    }
  • AssetStudio验证

在这里插入图片描述

  • 游戏加载验证

    private Sprite LoadImg_decryption()
        {
            Sprite icon = null;
            string outputPath = Application.dataPath + "/BuildAssetBundle/Window/";
            AssetBundle.UnloadAllAssetBundles(true);
            AssetBundle ab = AssetBundle.LoadFromFile(outputPath + bundleName, 0, (ulong)GetOffset());
            icon = ab.LoadAsset<Sprite>(spriteName);
            return icon;
        }
    

image-20220319192209114

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShallsenSalt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值