Unity one machine one code encryption

一、Visual studio端(Visual studio 2019)

(1) Create a form application project

Search for Windows Forms application, select the item as shown below, next step

å¨è¿éæå¥å¾çæè¿°

 

If the Windows Forms application cannot be found, you need to install the .NET desktop development item in the Visual Studio Installer

å¨è¿éæå¥å¾çæè¿°
In order to facilitate testing, all students try to unify the project name (MachineCodeEncryption)

 å¨è¿éæå¥å¾çæè¿°

 å¨è¿éæå¥å¾çæè¿°

 


(2) Open the toolbox.
After the project is built, the default window is as shown above. You can click (View/Toolbox) or use the shortcut key Ctrl+Alt+X to open the toolbox.

å¨è¿éæå¥å¾çæè¿°
(3) Edit window title name
. The window name here can be edited arbitrarily. There are two ways to modify it. One is to modify it directly inside the script, and the other is to modify it directly in the editor window. I personally recommend using the second one. Editing and modification are relatively simple. , directly select the window/control with the left mouse button, and modify it directly in the solution explorer.

 å¨è¿éæå¥å¾çæè¿°

 å¨è¿éæå¥å¾çæè¿°

 

(4) Build a Form window.
Use the common controls in the toolbox to build a relatively simple Form window, as shown below.

å¨è¿éæå¥å¾çæè¿°

 

In order to facilitate testing, the name is first consistent with the name of all controls in the test case to facilitate the correspondence with the code. Modification method: click the left mouse button on the control and modify it in the panel in the lower right corner. The rules are from top to bottom and from left to right. The control names are as follows :

label_MachineID、textBox_MachineID
label_AuthorizationCode、textBox_AuthorizationCode
button_Calculate

å¨è¿éæå¥å¾çæè¿°
(5) MD5 encryption function writing (32-bit string)
Students who want to know about 16-bit or 64-bit can go to - MD5 Encryption - to view.

 å¨è¿éæå¥å¾çæè¿°

 å¨è¿éæå¥å¾çæè¿°

 

 

Add MD5Cryption class and function writing:

using System.Security.Cryptography;
using System.Text;

namespace MachineCodeEncryption
{
    public class MD5Cryption
    {
        /// <summary>
        /// MD5 32位加密
        /// </summary>
        /// <param name="_encryptContent">需要加密的内容</param>
        /// <returns></returns>
        public static string EncryptMD5_32(string _encryptContent)
        {
            string content_Normal = _encryptContent;
            string content_Encrypt = "";
            MD5 md5 = MD5.Create();

            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(content_Normal));

            for (int i = 0; i < s.Length; i++)
            {
                content_Encrypt = content_Encrypt + s[i].ToString("X2");
            }
            return content_Encrypt;
        }
    }
}


(6) Encryption main logic writing
Double-click the calculation button

å¨è¿éæå¥å¾çæè¿°

 

The complete code is as shown below

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MachineCodeEncryption
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button_Calculate_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox_MachineID.Text))
            {
                string authorizationCode = MD5Cryption.EncryptMD5_32(textBox_MachineID.Text);
                textBox_AuthorizationCode.Text = authorizationCode;
            }
        }
    }
}



(7) Running effect
Final result: No matter what data is input, the encrypted 32-bit string information is finally returned.

å¨è¿éæå¥å¾çæè¿°

 

(8) Windows applications and class libraries each output a copy of
the Windows application output to facilitate starting the executable program and performing calculations.
The class library output is for the convenience of importing into Unity for calling, and there is no need to write functions repeatedly.

Right-click the project and enter properties

å¨è¿éæå¥å¾çæè¿°

 

Perform windows application and class library generation operations separately

å¨è¿éæå¥å¾çæè¿°å¨è¿éæå¥å¾çæè¿°
The generation path is as follows:

 å¨è¿éæå¥å¾çæè¿°

 

 

For the convenience of testing, the two generated results are copied to the desktop respectively.

å¨è¿éæå¥å¾çæè¿°

 

2. Unity side (Unity 2019.4.8)
(1) Create Unity project

å¨è¿éæå¥å¾çæè¿°
(2) Create the folder required for the project and import the class library file

 å¨è¿éæå¥å¾çæè¿°

 


(3) Create and build the
initial scene and perform verification operations

å¨è¿éæå¥å¾çæè¿°

 

game scene

å¨è¿éæå¥å¾çæè¿°
(3) Write logic code,
create VerifyTest script, improve logic and mount components

 

using UnityEngine;
using UnityEngine.UI;
using MachineCodeEncryption;
using UnityEngine.SceneManagement;

public class VerifyTest : MonoBehaviour
{
    public Text text_MachineID;
    public InputField inputField_AuthorizationCode;
    public Button button_EnterGame;

    private void Start()
    {
        text_MachineID.text = GetMachineID();

        button_EnterGame.onClick.AddListener(Event_EnterGame);
    }

    /// <summary>
    /// 获取设备识别码
    /// </summary>
    /// <returns></returns>
    public string GetMachineID() { return SystemInfo.deviceUniqueIdentifier; }

    /// <summary>
    /// 进入游戏
    /// </summary>
    public void Event_EnterGame()
    {
        if (inputField_AuthorizationCode.text == MD5Cryption.EncryptMD5_32(text_MachineID.text))
        {
            SceneManager.LoadScene("Game");
        }
        else
        {
            Debug.Log("用户授权码错误");
        }
    }
}

å¨è¿éæå¥å¾çæè¿°

Of course, the device identification identifier here is just a code name. I prefer to call it verification rules. Interested students can define other rules by themselves. The SystemInfo.deviceUniqueIdentifier used here comes from the article Reading PC Hardware Information in Unity. .

The general logic is: when the user opens the project on the computer, the system automatically obtains the identification number and displays it, then opens the form application to calculate the authorization code corresponding to the identification number, and matches it on the Unity side to realize the corresponding operation designation for a single computer. project.

3. Display effect

å¨è¿éæå¥å¾çæè¿°

 

This chapter ends here. It just provides students with an idea and approach. Of course, other rules can be used to limit it. In addition, it can also be written into a local TxT file after verification, so that a computer only needs to verify once. You can directly enter the functions and so on.
——————————————
Copyright Statement: This article is an original article by CSDN blogger “StubbrnStar” and follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement when reprinting .
Original link: https://blog.csdn.net/weixin_38484443/article/details/118873611

Guess you like

Origin blog.csdn.net/qq_18427785/article/details/119755313
one
ONE