Use dongle to encrypt Unity project plug-in source code

Use dongle to encrypt Unity project plug-in source code

	最近在unity3D中开发了一款插件,需要卖给客户,但是公司需要隐藏插件的源码,而且保证客户只有指定的电脑才能使用该插件开发!
	针对这个问题,分为两个步骤:1.隐藏插件源码,将插件源码编译成dll;2.使用加密锁加密源码。
	1.将Unity3d代码编译成dll
	在VS中创建一个C#类库工程,添加引用,引用Unity3d的类库UnityEditor.dll 和 UnityEngine.dll,本机它们的地址在D:\Program Files (x86)\Unity\Editor\Data\Managed。

insert image description hereCreate a C# class library project

insert image description here
insert image description hereReference the dll of Unity3d

Create a new class, copy and paste the C# script of unity3d into this class, note that this class has a namespace, and calling the function of this dll in unity3d needs to refer to the namespace.
insert image description here

insert image description hereInherit MonoBehaviour in the Class1 class, print it in the Update function, test whether the class can be hung on the GameObject of unity, and then write a function to test whether it can be called normally by the C# script of unity.

insert image description hereChange the solution configuration to Release, then compile, and the dll will be generated
insert image description here

insert image description hereCreate a new Plugins folder in the unity project, and paste DllTest into this folder.
insert image description here
Open the unity project, drag a GameObject into the scene, and mount the script Class1 on it.
insert image description here

Create a new script TestDll in the unity project, reference the namespace DllTest, call the function of Class1, mount the script on the GameObject, and run it.

insert image description hereIt can be judged by printing that we have successfully mounted the Class1 of the Dll on the GameObject in the unity scene, and can successfully call the public functions of Class1.

2. Use the encryption lock to encrypt the source code.
At the beginning, I directly used the Gemalto super dog, hoping to directly encrypt the Dll (this method can be used to load UE4). It took 2 days from the installation of their software to the successful encryption, and finally in unity After testing, I found that even if the super dog is plugged in, it still can’t be used normally. After asking the manufacturer, they said that they don’t support the dll in the unity editor. I don’t know the reason. In order to solve the problem, I searched for other brands on JD. dll in the editor, but the Yutian encryption lock manufacturer provided me with a solution, which is to encrypt the source code, which solved my problem. In order to express my gratitude, I will help promote it in the blog.
Yutian encryption lock Jingdong link: Yutian encryption lock Jingdong link
Encryption lock model: D8-16K
Manufacturer technician QQ: 2180985017

insert image description hereUse the tool YTTool provided by Yutian manufacturer to generate a key and the corresponding SoftKey.cs code. The key can be set to the dongle hardware. The specific method will be provided by the manufacturer. Here is a detailed description of how to use SoftKey.cs to encrypt the source code.

Add a class named SoftLock to the class library project in step 1, copy and paste the code in SoftKey, and create a new singleton class named SoftKeyManager to manage the dongle function.

using System;
using UnityEngine;
using System.Runtime.InteropServices;


namespace WindowsApplication1
{
    
    
    public class SoftLockManager
    {
    
    
        private static SoftLockManager instance = null;
        private SoftLockManager()
        {
    
    
            ytsoftkey = new SoftKey();
            //CheckSoftLock(true);
        }

        public static SoftLockManager Instance
        {
    
    
            get
            {
    
    
                if (instance == null)
                {
    
    
                    instance = new SoftLockManager();
                }
                return instance;
            }
        }


        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);

        public const int WM_SYSCOMMAND = 0x0112;


        public const int SC_CLOSE = 0xF060;

        private bool isCheckedSoftLock = true;
        public bool GetIsCheckedSoftLock
        {
    
    
            get {
    
     return isCheckedSoftLock; }
        }

        SoftKey ytsoftkey;

        public bool CheckSoftLock(bool isShowMessage)
        {
    
    

            if (Application.platform == RuntimePlatform.WindowsPlayer)
            {
    
    
                isCheckedSoftLock = true;
            }
            else
            {
    
    
                if (ytsoftkey.CheckKeyByFindort_2() == 0)
                {
    
    
                    isCheckedSoftLock = true;
                }
                else
                {
    
    
                    if (isShowMessage)
                    {
    
    
                        MessageBox(new IntPtr(0), "检测到未插上加密锁!", "错误", 0);
                    }
                    isCheckedSoftLock = false;
                }
            }

            return isCheckedSoftLock;
        }
    }
}

This class provides an interface to detect whether there is a dongle. If there is, it will return true. If not, a prompt box will pop up and return false. The company needs to enable the dongle function in the unity editor. If it is not enabled after packaging, add it here A judgment of Application.platform == RuntimePlatform.WindowsPlayer is made, the test is not false when running in the editor, and it is not true when running in windows after packaging.
ytsoftkey.CheckKeyByFindort_2() == 0 is provided by the operator to detect whether the dongle is inserted, if it is added, it will return true, and if it is not inserted, it will return false.

At this time, you can call the function CheckSoftLock (true) of the singleton class in the core code of the plug-in to detect the encryption lock. If it returns true, the code will run, and if it returns false, it will not run. Compile and generate a new dll, and use the dll according to step 1. Can.

Guess you like

Origin blog.csdn.net/xialuhui/article/details/114306431