Unity AssetBundle encryption

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/wangjiangrong/article/details/89671861

Foreword

Yesterday the interview, the interviewer asked some relevant knowledge about the package AB, AB asked about encryption issues, as had previously not understood, but the feeling is a very important aspect. So today search of the investigation related knowledge, record it (if the wrong place, welcome bigwigs help correct)

We all know that our ab packages are placed in the body of the package readable file folder, the player can be easily taken out of our ab package, if not, then do the encryption process, in accordance with normal read operation ab package, any It can be loaded ab out the contents of our package, and then use, which is far from secure.

Here we use a simple encryption algorithm, our ab packet is encrypted.

Thinking

After getting ab package, we traverse StreamingAssets catalog, get all the ab package, and then one by one into the pack ab byte [], then every bit of the secret key and set a good key performs an exclusive OR operation byte [] in (encryption). Finally, the new byte [] re-written to the file, that encryption is complete.

When ab read, but also () methods into methods used AssetBundle.LoadFromMemory () from the original AssetBundle.LoadFromFile, incoming byte [] parameter is ab same package to be read into byte [], then every key and secret key performing an exclusive oR operation (decryption).

NOTE: A XOR Key = B, B XOR Key = A.

Code

This code is followed in the package before the package of ab plus project, first of all let's define methods and good encryption and decryption keys.

using System.IO;

namespace Utility {

    public class FileUtility {

        ......

        const byte m_key = 157;
        /// <summary>
        /// 加密/解密
        /// </summary>
        /// <param name="targetData">文件流</param>
        public static void Encypt(ref byte[] targetData) {
            //加密,与key异或,解密的时候同样如此
            int dataLength = targetData.Length;
            for(int i = 0; i < dataLength; ++i) {
                targetData[i] = (byte)(targetData[i] ^ m_key);
            }
        }
    }
}

Then we complete package when reading ab bag and encrypted. (Note demo in the ab package regenerate all the next, and then put all the files under StreamAssets encrypted, the actual situation of our differences only for packet encryption. At the same time to avoid repeating encryption)

namespace AssetBundle {
    public static class BuildAssetBundle {

        ......

        public static IEnumerator EncyptAssetBundle() {
            yield return "开始加密...";
            //遍历streamingAssets目录下所有的ab包,逐个进行加密
            foreach(var f in new DirectoryInfo(AssetBundleUtility.streamingAssetsPath).GetFiles("*.u", SearchOption.AllDirectories)) {
                Byte[] temp = File.ReadAllBytes(f.FullName);
                yield return "加密文件:"+ f.FullName;
                FileUtility.Encypt(ref temp);
                File.WriteAllBytes(f.FullName, temp);
            }
            yield return "加密完成...";
        }

        ......

    }
}

Then at the end of the packing process, call encryption method

namespace AssetBundle {
    public class BuildAssetBundleWindow : EditorWindow {

        ......

        void Build() {

            ......

            #region 流程

            // 打包
            if (BuildAssetBundleSetting.instance.isBuild) {
                count++;

                IEnumerator buildEtor = BuildAssetBundle.Build();
                while (buildEtor.MoveNext()) {
                    yield return buildEtor.Current;
                }
            }

            //加密
            IEnumerator encyptEtor = BuildAssetBundle.EncyptAssetBundle();
            while(encyptEtor.MoveNext()) {
                yield return encyptEtor.Current;
            }

            #endregion

            BuildAssetBundleSetting.instance.Save();

            ......
        }

        ......
    }
}

This time, the encryption pack has been completed, we hit a ab package, get the following results

Ab read time, or when using AssetBundle.LoadFromFile () method will be given as follows. So that other users can not freely read our resources ab package of.

The correct reading method as follows, using LoadFromMemory () Method:

ab = new AssetBundleItem(path, fileName, isHasDependence);
byte[] stream = File.ReadAllBytes(ab.pathName);
//解密
FileUtility.Encypt(ref stream);
ab.assetBundle = AssetBundle.LoadFromMemory(stream);
cacheAssetBundleItemDic.Add(path, ab);

 

Guess you like

Origin blog.csdn.net/wangjiangrong/article/details/89671861