当前位置:网站首页>What are the things that should be planned from the beginning when developing a project with Unity?How to avoid a huge pit in the later stage?

What are the things that should be planned from the beginning when developing a project with Unity?How to avoid a huge pit in the later stage?

2022-08-11 07:29:00 Clank's Game Stack

Unity's games are easy to be hackedDecompile out, and then repackage and publish it, and make yourself work hardA game that was developed hard, and the transcription was naked.Many projects require resource encryption. How to do resource encryption in Unity?This article will share with you the idea of ​​encryption algorithm + resource packaging integration:

(1) How to choose the encryption algorithm for game resource encryption;

(2) Encryption and decryption of Assetsbundle resource package;

Yes!Here is a The unity learning exchange group has gathered a group of zero-based beginners who love to learn unity, and there are also some technical leaders who are engaged in unity development. You are welcome to exchange and learn.

How to choose an encryption algorithm for game resource packs

The first thing to do when encrypting the game resource package is to choose an encryption/decryption algorithm, which is not easy to be cracked by others.How do we choose?I am sorry to tell you that no safe encryption and decryption algorithm is safe.The gangster will come to refute the first time, how is this possible, how can he crack it after I encrypted it?Game resource encryption is destined that the encryption algorithm cannot be too time-consuming.Next, let's take a look at how different encryption algorithms are cracked.

(1) Use the encryption and decryption algorithm implemented by the standard library, such as using the encrypt encryption/decryption algorithm.There is a problem here, we will have a key to decrypt, and the key is usually written into the code in the game.This kind of cracking is too simple, the key is written in the code, basically string, etc., static analysisThe code finds the string corresponding to the key. With the key, you are using the standard library, and the encrypted resources will be cracked at once, and your resources will be cracked in minutes.

(2) Use the encryption and decryption algorithm implemented by yourself, for example, use binary or for encryption and decryption, for example, set a binary mask mask for encryption, let each byte XOR this mask, and get a newdata, so that the resource cannot be directly identified. When the resource is used, the encrypted data is XORed and the mask is decrypted and returned to the project for use.This kind of cracking is a bit annoying to write, you need to decompile your decryption code yourself, and at the same time find your decryption key key.But the essence is also easier to crack.

The encryption/decryption algorithm is shown as follows:

Source data 1110 0001, key is 00101000;

Encrypted data: 1110 0001 ^ 00101000 = 1100 1001

Decrypted data: 1100 1001 ^ 00101000 = 1110 0001

From the above analysis, it is almost difficult not to be cracked, depending on the cost of cracking.Is your game worth it.Generally, our goal is not to let people use the Unity decompilation tool to directly decompile our game into the Unity project project, and then recompile, package and publish.The decryption algorithm should be fast, and it is destined to not be too complicated. The key is only put on the client or the network, and it is destined to be obtained.

Assetsbundle encryption and decryption

After analyzing the encryption and decryption algorithm, let's take a look at how to encrypt our resources. Let's first introduce the first solution. When printing a resource package, encrypt a single resource and enter the encrypted data into theresource pack.When decrypting, read the resource from the ab package, and then decrypt the content in the resource package.The specific steps are as follows:

(1) Create a new class, inherit from FileStream, and override the Read/Write function.The code is as follows:

using System.IO;public class MyStream : FileStream {const byte KEY = 40; // key mask: 0010 1000public MyStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync) :base(path, mode, access, share, bufferSize, useAsync) {}public MyStream(string path, FileMode mode) : base(path, mode) {}// Overload the read interface, read and decrypt once;public override int Read(byte[] array, int offset, int count) {var index = base.Read(array, offset, count);for (int i = 0 ; i < array.Length; i++) {array[i] ^= KEY;}return index;}public override void Write(byte[] array, int offset, int count) {// Override the write interface, encrypt first and then write;for (int i = 0; i < array.Length; i++) {array[i] ^= KEY;}base.Write(array, offset, count);}}

(2) Modify the script of the ab package packaging tool, and then encrypt the generated ab package resources once. The package encryption script is as follows:

[MenuItem("Tools/BuildAB")]static void BuildAB(){…// The for loop traverses each packaged resource Ab package inside, and then calls for encryption.foreach (var name in manifest.GetAllAssetBundles()){var uniqueSalt = Encoding.UTF8.GetBytes(name);var data = File.ReadAllBytes(Path.Combine(Application.streamingAssetsPath, name));using (var myStream = new MyStream(Path.Combine(Application.streamingAssetsPath, "encypt_" + name),FileMode.Create)){myStream.Write(data, 0, data.Length); // Trigger the call of our rewritten write function in MyStream to complete data encryption}}AssetDatabase.Refresh();}

(3) When decrypting, read the ab package and use the function AssetsBundle.LoadFromStream to load it, and this will trigger the Stream to call the Read data interface, and you can enter our overloaded Read interface for decryption.The code is as follows:

var fileStream = new MyStream(Application.streamingAssetsPath + "/encypt_myab.unity3d", FileMode.Open, FileAccess.Read, FileShare.None, 1024 * 4, false)){var myLoadedAssetBundle = AssetBundle.LoadFromStream(fileStream); // Trigger the Read call in MyStream to decrypt the data.}

Today's sharing is here, you can enter the study group to communicate game development together

原网站

版权声明
本文为[Clank's Game Stack]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208110548279499.html