AssetBuntle加密解密

本文介绍了一个Unity项目的加密流程,包括使用特定的异或因子对文件进行解密和加密的方法,以及如何通过读取本地文件并应用这些加密技术来保护资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参照悠游课堂边涯

1、解密


using UnityEngine;
using System.Collections;

public sealed class SecurityUtil
{
    #region xorScale 异或因子
    /// <summary>
    /// 异或因子
    /// </summary>
    private static readonly byte[] xorScale = new byte[] { 45, 66, 38, 55, 23, 254, 9, 165, 90, 19, 41, 45, 201, 58, 55, 37, 254, 185, 165, 169, 19, 171 };//.data文件的xor加解密因子
    #endregion

    private SecurityUtil()
    {

    }

    /// <summary>
    /// 对数组进行异或
    /// </summary>
    /// <param name="buffer"></param>
    /// <returns></returns>
    public static byte[] Xor(byte[] buffer)
    {
        //------------------
        //第3步:xor解密
        //------------------
        int iScaleLen = xorScale.Length;
        for (int i = 0; i < buffer.Length; i++)
        {
            buffer[i] = (byte)(buffer[i] ^ xorScale[i % iScaleLen]);
        }
        return buffer;
    }
}

 

2、文件加载


using UnityEngine;
using System.Collections;
using System.IO;

/// <summary>
/// 本地文件管理
/// </summary>
public class LocalFileMgr : Singleton<LocalFileMgr>
{
    public readonly string LocalFilePath = Application.persistentDataPath + "/";

    /// <summary>
    /// 读取本地文件到byte数组
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public byte[] GetBuffer(string path)
    {
        byte[] buffer = null;

        using (FileStream fs = new FileStream(path, FileMode.Open))
        {
            buffer = new byte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
        }
        return buffer;
    }
}

 

3、加密

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;

namespace ABFW
{
    public class AssetBundleWindow 
    {
        private void AssetBundleEncrypt(string path)
        {
            string[] files = Directory.GetFiles(path);
          
            foreach (string file in files)
            {
                  FileInfo fileInfo = new FileInfo(file);
                if(!fileInfo.Extension.Equals(".ab",System.StringComparison.CurrentCultureIgnoreCase)&&
                    !fileInfo.Extension.Equals(".unity3d",StringComparison.CurrentCultureIgnoreCase)
                    )
                {
                    continue;
                }
                byte[] buffer = null;
                using (FileStream fs = new FileStream(file, FileMode.Open))
                {
                    buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                }
                buffer = SecurityUtil.Xor(buffer);
                using (FileStream fs = new FileStream(file, FileMode.Create))
                {
                    fs.Write(buffer, 0, buffer.Length);
                    fs.Flush();
                }
            }
            string[] dirs = Directory.GetDirectories(path);
            foreach (string dir in dirs)
            {
                AssetBundleEncrypt(dir);
            }

        }


    
    }//class end
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值