[Unity3D Daily] Sharing of encryption and activation ideas for Unity3D projects

I. Introduction

Today I share several simple ideas for project encryption and activation, but they are not the source code of the project, but only a few encryption ideas that are useful to people who do not know how to crack. Of course, everything can be decrypted by encryption, it is only a matter of time

Second, the text

Idea 1: Save account password in the project

This can be used to save the account password, or the registry, or local persistent data. The idea is to set the account password first, and then others need the account password to activate when using this software, for encryption

Process:
1 We first make a UI
Insert picture description here
2 code

using UnityEngine;
using UnityEngine.UI;

public class Encript2 : MonoBehaviour
{
    //设置一个初始的账号和密码 
    public string UserName = "";
    public string PassWord = "";
    //UI
    public Text m_UserNameText;
    public Text m_PassWordText;
    public Text m_HelpText;
    public Button m_BtnAticvation;
    public Button m_BtnExit;
    
    void Start()
    {
        m_BtnAticvation.onClick.AddListener(BtnEvent_Aticvation);
        m_BtnExit.onClick.AddListener(BtnEvent_Exit);
        //判断是否已经激活
        Decide_Acticvation();
    }

    public void Decide_Acticvation()
    {
        if (UserName == "admin" && PassWord == "123456")
        {
            gameObject.SetActive(false);
        }
    }

    public void BtnEvent_Aticvation()
    {
        UserName = m_UserNameText.text;
        PassWord = m_PassWordText.text;
        if (m_UserNameText.text == "admin" && m_PassWordText.text == "123456")
        {
            //激活成功
            gameObject.SetActive(false);
        }
        else
        {
            //未激活成功
            m_HelpText.text = "账号密码不正确";
        }
    }

    public void BtnEvent_Exit()
    {
        Application.Quit();
    }
}

3 Binding parameters The
Insert picture description here
overall idea is very simple, set a local constant, and then enter the software needs to be activated first, activation is successful

Advantages: simple and rough
disadvantages: easy to be cracked

Idea two: Write saved files with encrypted data locally

This is actually very simple. First, write a verification program on the client to verify the unique identifier of the current computer device plus a custom field. Afterwards
, if the encrypted file data is compared, whether it is the same, enter the program if it is the same, and close it if it is different. Program
and then write an activation program on the computer

Process:
1. First make an active interface
Insert picture description here
Insert picture description here
. It's ugly. The most important thing is the function! ! !
2. Code

using Microsoft.Win32;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;

public class Encript3 : MonoBehaviour
{
    public GameObject m_PanelEncrypted;

    void Start()
    {
        string s1 = SystemInfo.deviceUniqueIdentifier;
        string s2 = GetMD5String(s1);
        //验证本地的加密字符串是否正确
        Verification_MD5(s2);
    }

    //验证本地的md5正确与否
    public void Verification_MD5(string ValidateMD5)
    {
        RegistryKey RootKey, RegKey;
        RootKey = Registry.CurrentUser.OpenSubKey("UserSetting", true);
        //打开子项:HKEY_CURRENT_USER\UserSetting\Encrypted
        if ((RegKey = RootKey.OpenSubKey("Encrypted", true)) == null)//如果文件不存在,就显示要激活的界面
        {
            m_PanelEncrypted.SetActive(true);
        }
        //异常捕捉,如果出现程序异常,比如闪退
        try
        {
            object encrypted = RegKey.GetValue("EncryptedString");        //读取键值
            if (encrypted.ToString() == ValidateMD5)
            {
                m_PanelEncrypted.SetActive(false);
            }
            else
            {
                m_PanelEncrypted.SetActive(true);
            }
        }
        catch
        {
            print("异常状况");
        }
    }

    /// <summary>
    /// 通过字符串获取MD5值,返回32位字符串。
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string GetMD5String(string text)
    {
        MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
        byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(text));
        StringBuilder sBuilder = new StringBuilder();
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }
        return sBuilder.ToString().ToLower();
    }
}

OK, friends can try it. . Yes, there is an activation program. .
Code:

/// <summary>
    /// 通过字符串获取MD5值,返回32位字符串。
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string GetMD5String(string text)
    {
        MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
        byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(text));
        StringBuilder sBuilder = new StringBuilder();
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }
        return sBuilder.ToString().ToLower();
    }

    //激活程序
    public void Activation()
    {
        string md5 = GetMD5String(SystemInfo.deviceUniqueIdentifier);
        // 创建键值对
        RegistryKey RootKey, RegKeyRoot,RegKey;
        //项名为:HKEY_CURRENT_USER
        RootKey = Registry.CurrentUser;
        if ((RegKeyRoot = RootKey.OpenSubKey("UserSetting",true))== null)
        {
            RootKey.CreateSubKey("UserSetting");
            RegKeyRoot = RootKey.OpenSubKey("UserSetting", true);
        }
        //打开子项:HKEY_CURRENT_USER\Software\TestToControlUseTime
        if ((RegKey = RegKeyRoot.OpenSubKey("Encrypted", true)) == null)
        {
            RegKeyRoot.CreateSubKey("Encrypted");               //不存在,则创建子项
            RegKey = RootKey.OpenSubKey("Encrypted", true);  //打开键值
            RegKey.SetValue("EncryptedString", (object)md5);         //创建键值
            return;
        }
        else
        {
            RegKey.SetValue("EncryptedString", (object)md5);
            return;
        }
    }

Summary: You can write your own encryption program, or add some other parameters to increase the difficulty of cracking

Advantages: difficult to crack, higher security
Disadvantages: still not secure enough, if you crack the source code, you know how to encrypt, you can decrypt in reverse

Idea 3: Encrypt on the server side and return the encrypted string

This idea is to put the encryption program on the server, even if other people have cracked the code, and do not know the encryption idea, they cannot decrypt in the reverse direction.

Process:
1. The interface design
Insert picture description here
Insert picture description here
is ugly, the most important thing is the function! ! !

2. Code

using Microsoft.Win32;
using System.Collections;
using UnityEngine;

public class Encript4 : MonoBehaviour
{
    public GameObject m_PanelEncrypted;
    string md5 = "";

    void Start()
    {
        StartCoroutine(GetData());
        //验证本地的加密字符串是否正确
        Verification_MD5(md5);
    }

    //验证本地的md5正确与否
    public void Verification_MD5(string ValidateMD5)
    {
        RegistryKey RootKey, RegKey;
        RootKey = Registry.CurrentUser.OpenSubKey("UserSetting", true);
        //打开子项:HKEY_CURRENT_USER\UserSetting\Encrypted
        if ((RegKey = RootKey.OpenSubKey("Encrypted", true)) == null)//如果文件不存在,就显示要激活的界面
        {
            m_PanelEncrypted.SetActive(true);
        }
        //异常捕捉,如果出现程序异常,比如闪退
        try
        {
            object encrypted = RegKey.GetValue("EncryptedString");        //读取键值
            if (encrypted.ToString() == ValidateMD5)
            {
                m_PanelEncrypted.SetActive(false);
            }
            else
            {
                m_PanelEncrypted.SetActive(true);
            }
        }
        catch
        {
            print("异常状况");
        }
    }

    public IEnumerator GetData()
    {
        WWW www = new WWW("http://127.0.0.1/Test.php?md5="+SystemInfo.deviceUniqueIdentifier);
        yield return www;
        if (www.error != null)
        {
            yield return null;
        }
        else
        {
            md5 = www.text;
        }
    }
}

OK, come here today

Published 226 original articles · praised 509 · 530,000 views

Guess you like

Origin blog.csdn.net/q764424567/article/details/101061353