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_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

}

unity3d:激活码系统(根据PC机器码,给对应激活码完成软件注册)_第1张图片

你可能感兴趣的:(Unity3D实用技术笔记,Unity3d技术笔记,Unity3d游戏源码)