Bundle encryption prevents AssetStudio from acquiring assets

Students who have done Unity bundle resources know that AssetStudio can easily crack the bundle and obtain the original resources in the project. In order to prevent their project resources from being easily obtained by others, AssetStudio has studied the following feasible bundle encryption scheme on the Internet. The following two methods are summarized:

1. Use the byte code of the bit XOR operation bundle to encrypt and decrypt. Why can the bit XOR operation realize encryption and decryption in general, because 1 ^ 0 = 1; 0 ^ 0 = 0; Therefore, x ^ 0 = x can maintain the data. The XOR operation in bit operation meets the exchange law, so B ^ A ^ A = B ^ 0 = B. therefore, after the bundle resources are typed, read the byte data of the bundle, XOR your key, and generate the encrypted file, When loading the bundle, first read the byte data of the encrypted file, and then XOR your key to get the original bundle byte data, and then use AssetBundle Loadfrommemory (byte [] binary) loads the corresponding bundle. This scheme can realize the encryption and decryption of bundles, but the performance of online projects should be considered. This scheme can not meet the requirements in terms of memory performance, so the actual online projects are generally Do not use this scheme.

2. Read the byte data of the bundle, and then re create the corresponding file. Write a section of encrypted byte data in the header, and then add the byte data of the bundle. Use AssetBundle when loading the bundle The loadfromfile (string path, uint CRC, ulong offset) interface transfers in the length of the byte data you encrypt as offset, or you can load the corresponding bundle, The program In terms of performance, there is basically no other consumption, which is also the final scheme I use. The corresponding encryption code is posted below. Those who are interested can refer to it.

[MenuItem("Tools/Encode Bundles", false, 901)]
public static void EncodeBundles()
{
    string bundleDir = Application.dataPath + "/StreamingAssets/xxxx";  //The path of your bundle folder
    DirectoryInfo dirInfo = new DirectoryInfo(bundleDir);
    string currentDir = dirInfo.Name;
    string encodeDir = Path.Combine(dirInfo.Parent.FullName, currentDir + "_encode");
    if (Directory.Exists(encodeDir))
    {
        Directory.Delete(encodeDir, true);
    }
    Directory.CreateDirectory(encodeDir);

    FileInfo[] abs = dirInfo.GetFiles("*", SearchOption.AllDirectories);
    for (int i = 0; i < abs.Length; i++)
    {
        FileInfo bundleFile = abs[i];
        if (bundleFile.Extension.Equals(".meta") || bundleFile.Extension.Equals(".manifest"))
        {
            continue;
        }

        string newDir = bundleFile.DirectoryName.Replace("\\", "/").Replace(bundleDir, encodeDir);
        if (!Directory.Exists(newDir))
        {
            Directory.CreateDirectory(newDir);
        }

        byte[] salt = "Key string".StringToBytes();  //Different salt can be generated in other ways. Here is just a simple example
        byte[] src = File.ReadAllBytes(Path.Combine(bundleFile.DirectoryName, bundleFile.Name));

        byte[] buffer = new byte[src.Length + salt.Length];
        Array.Copy(salt, buffer, salt.Length);
        Array.Copy(src, 0, buffer, salt.Length, src.Length);

        string newFile = Path.Combine(newDir, bundleFile.Name);
        FileStream fs = new FileStream(newFile, FileMode.CreateNew);
        fs.Write(buffer, 0, buffer.Length);
        fs.Close();
    }

    AssetDatabase.Refresh();
}

Modify bundleDir to the path where your project's bundle is located. After processing, the corresponding bundle with the same name will be generated in the same level directory_ The encode folder is used to store encrypted bundles. Salt can be replaced by another generation method, which is just a simple process here. Finally, call AssetBundle Loadfromfile (path, 0, salt length) to load the corresponding bundle resource.

Note the following:

1. Considering the unpacking and hot update, don't type the original bundle directory under the StreamingAssets directory, but put the encrypted folder under the StreamingAssets directory for packaging. In this way, even if others extract your package and get the bundle file, they can't easily get the original resources with AssetStudio.

2. If you use the MD5 code record and comparison of the bundle to realize hot update resources, you should use the MD5 code of the encrypted bundle file instead of the original bundle. After each hot change increment package, you will encrypt it to ensure that your key salt remains unchanged.

3. It's impossible to add salt to the end instead of the head. AssetStudio can still parse the original resources, so salt can only place the head.

 

 

Keywords: Unity Encryption Unity3d assetbundle

Added by doctor_james on Mon, 31 Jan 2022 05:10:36 +0200