unity3d:激活碼系統(根據PC機器碼,給對應激活碼完成軟件註冊)

1.機器碼爲明文,採用DES加密
2.判斷輸入激活碼與機器碼的密文一致,激活成功


using UnityEngine;
using UnityEngine.UI;

using UnityEngine.SceneManagement;
using System.IO;

namespace KeyScene
{
    public class KeyMgr : MonoBehaviour
    {
        public string m_titleName;
        public string m_projectName;
        string m_sMachineCode;
        public string m_nextScene;
        public Text m_machineCode;
        public InputField m_inputActice;
        public Text m_title;
        public UnityEngine.UI.Button m_btnCopy;
        public UnityEngine.UI.Button m_btnActive;

        public Text m_hint;

        string m_miMa;
        string m_path;
        string m_fileName = "mi.json";
        string m_allPath;

        public GameObject m_panel;
        // Use this for initialization
        void Start()
        {
            m_sMachineCode = m_projectName + SystemInfo.deviceUniqueIdentifier;
            m_miMa = DesTool.EncryptString(m_sMachineCode, "subor123");
            Debug.Log(m_miMa);
            m_path = UnityEngine.Application.streamingAssetsPath;
            m_allPath = m_path + "/" + m_fileName;
            if (File.Exists(m_allPath) == true)
            {
                string sContent = GetJsonString(m_allPath);
                if (sContent == m_miMa)
                {
                    SceneManager.LoadScene(m_nextScene);
                }
                else {
                    m_panel.SetActive(true);
                }
            }
            else {
                m_panel.SetActive(true);
            }
            

            
            m_machineCode.text = m_sMachineCode;
            m_title.text = m_titleName;

            m_btnActive.onClick.AddListener(OnBtnActive);
            m_btnCopy.onClick.AddListener(OnBtnCopy);

            
            
        }


        void OnBtnActive()
        {
            string active = m_inputActice.text;
            if (active == m_miMa)
            {
                
                m_hint.text = "激活成功";
                m_hint.color = Color.green;
                m_hint.gameObject.SetActive(true);
                m_panel.SetActive(false);
                //保存文本
                SaveJsonString(active, m_allPath);
                SceneManager.LoadScene(m_nextScene);
            }
            else {
                m_hint.text = "激活失敗";
                m_hint.color = Color.red;
                m_hint.gameObject.SetActive(true);
            }
        }

        void OnBtnCopy()
        {
            System.Windows.Forms.Clipboard.SetDataObject(m_sMachineCode);
        
        }



        static public void SaveJsonString(string JsonString, string path)    //保存Json格式字符串
        {
            //寫入Json數據
            if (File.Exists(path) == true)
            {
                File.Delete(path);
            }

            string onlyPath = GetOnlyPath(path);
            if (!Directory.Exists(onlyPath))
            {
                Directory.CreateDirectory(onlyPath);
            }

            FileInfo file = new FileInfo(path);
            StreamWriter writer = file.CreateText();
            writer.Write(JsonString);
            writer.Close();
            writer.Dispose();
        }

        static public string GetJsonString(string path)     //從文件裏面讀取json數據
        {//讀取Json數據
            StreamReader reader = new StreamReader(path);
            string jsonData = reader.ReadToEnd();
            reader.Close();
            reader.Dispose();
            return jsonData;
        }

        public static string GetOnlyPath(string path)
        {
            string[] bufPath = path.Split('/');
            string name = bufPath[bufPath.Length - 1];
            string onlyPath = path.Replace(name, "");
            //string abPath = info.m_prefabName.Replace("/" + abName, "");
            //string[] bufAbName = abName.Split('.');
            return onlyPath;
        }
    }
}

des加密

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

public class DesTool  {

    #region 密鑰

    //private static string key = "abcd1234";                                   //密鑰(長度必須8位以上)   

    #endregion



    #region DES加密

    public static string EncryptString(string pToEncrypt,string key)
    {

        DESCryptoServiceProvider des = new DESCryptoServiceProvider();

        byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);





        des.Key = UTF8Encoding.UTF8.GetBytes(key);

        des.IV = UTF8Encoding.UTF8.GetBytes(key);

        MemoryStream ms = new MemoryStream();

        CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);



        cs.Write(inputByteArray, 0, inputByteArray.Length);

        cs.FlushFinalBlock();



        StringBuilder ret = new StringBuilder();

        foreach (byte b in ms.ToArray())
        {

            ret.AppendFormat("{0:X2}", b);

        }

        ret.ToString();

        return ret.ToString();

    }

    #endregion



    #region DES解密

    public static string DecryptString(string pToDecrypt,string key)
    {

        DESCryptoServiceProvider des = new DESCryptoServiceProvider();



        byte[] inputByteArray = new byte[pToDecrypt.Length / 2];

        for (int x = 0; x < pToDecrypt.Length / 2; x++)
        {

            int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));

            inputByteArray[x] = (byte)i;

        }



        des.Key = UTF8Encoding.UTF8.GetBytes(key);

        des.IV = UTF8Encoding.UTF8.GetBytes(key);

        MemoryStream ms = new MemoryStream();

        CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);

        cs.Write(inputByteArray, 0, inputByteArray.Length);

        cs.FlushFinalBlock();



        StringBuilder ret = new StringBuilder();



        return Encoding.UTF8.GetString(ms.ToArray());

    }

    #endregion
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章