Unity程序加密以及激活码生成程序

Unity程序加密以及激活码生成程序


生成激活码

using UnityEngine;
using UnityEngine.UI;

public class InfoDNS : MonoBehaviour
{
    string str;
    public Text tx;

    public InputField shibiema;
    public InputField jihuoma;
    public DataLimi DL = DataLimi.qt;
    public Dropdown DP;

    void Start()
    {
        if (DP.value == 0)
        {
            DL = DataLimi.qt;
        }
        else if (DP.value == 1)
        {
            DL = DataLimi.ss;
        }
        else if (DP.value == 2)
        {
            DL = DataLimi.bn;
        }
        else if (DP.value == 3)
        {
            DL = DataLimi.yn;
        }
        else if (DP.value == 4)
        {
            DL = DataLimi.nn;
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (DP.value == 0)
        {
            DL = DataLimi.qt;
        }
        else if (DP.value == 1)
        {
            DL = DataLimi.ss;
        }
        else if (DP.value == 2)
        {
            DL = DataLimi.bn;
        }
        else if (DP.value == 3)
        {
            DL = DataLimi.yn;
        }
        else if (DP.value == 4)
        {
            DL = DataLimi.nn;
        }
    }

    public void GetDeviceID()
    {
        shibiema.text = SystemInfo.deviceUniqueIdentifier;
    }
	//点击生成激活码
    public void Jihuo()
    {
        str = shibiema.text;
        GetJi(str, DL);
    }
    //

    void GetJi(string Str, DataLimi dl)
    {
        if (str.Length < 40 || string.IsNullOrEmpty(str))
        {
            tx.text = "无效识别码,重新输入";
            shibiema.text = "";
            return;
        }
        int value0 = Random.Range(0, 40);
        if (value0 > 9)
        {
            string s = value0.ToString();
            s = s[1].ToString();
            value0 = int.Parse(s);
        }
        int value1 = Random.Range(0, 40);
        if (value1 > 9)
        {
            string ss = value1.ToString();
            ss = ss[1].ToString();
            value1 = int.Parse(ss);
        }
        int value8 = Random.Range(0, 40);
        if (value8 > 9)
        {
            string sss = value8.ToString();
            sss = sss[1].ToString();
            value8 = int.Parse(sss);
        }
        int value9 = Random.Range(0, 40);
        if (value9 > 9)
        {
            string ssss = value9.ToString();
            ssss = ssss[1].ToString();
            value9 = int.Parse(ssss);
        }
        string str2 = str[value0].ToString();
        string str3 = str[value8].ToString();
        string str5 = str[value1].ToString();
        string str7 = str[value9].ToString();
        string Get = dl.ToString();
        string str4 = Get[0].ToString();
        string str6 = Get[1].ToString();
        string total = value0.ToString() + value1.ToString() + str2 + str3 + str4 + str5 + str6 + str7 + value8 + value9;
        jihuoma.text = total;
    }
}

使用激活码

using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public enum DataLimi
{
    qt,
    ss,
    bn,
    yn,
    nn
}
public class DayTest : MonoBehaviour
{
    //bool ison = false;
    string str;
    string ALL;
    public DataInfo DIF;
    public DataLimi DL = DataLimi.qt;
    string shuruma;
    public InputField Jihuoma;
    public InputField Shibiema;
    public Text DebugTex;
    public Button ButtonExit;
    string Name;
    [Header("激活后执行的事件")]
    public UnityEvent JihuoEvent;
    [Header("系统未激活执行的事件")]
    public UnityEvent NojihuoEvent;

    void Start()
    {
        if (ButtonExit != null)
        {
            ButtonExit.onClick.AddListener(ExitButton);
        }
        str = SystemInfo.deviceUniqueIdentifier;
        if (Shibiema != null)
        {
            Shibiema.text = str;
        }
        string path = Application.dataPath + "/StreamingAssets/Config/" + "Lock.json";
        DIF = JsonWR.Read<DataInfo>(path);
        DIF.Lock[0].jihuoma = DIF.Lock[0].jihuoma;
        DIF.Lock[0].nianxian = DIF.Lock[0].nianxian;
        DIF.Lock[0].shijiancuo = DIF.Lock[0].shijiancuo;
        DIF.Lock[0].shifouguoqi = DIF.Lock[0].shifouguoqi;
        //if (DIF.Lock[0].shibiema.Length == 40)
        //{
        //    if (DIF.Lock[0].shibiema != str)
        //    {
        //        return;
        //    }
        //}
        bool testIson = TestJihuoMa(DIF.Lock[0].jihuoma, str, DIF.Lock[0].nianxian, DIF);
        Debug.Log(testIson);
        if (!testIson)
        {
            NojihuoEvent.Invoke();
        }
        //if (DIF.Lock[0].jihuoma.Length == 10)
        //{
        //    shuruma = DIF.Lock[0].jihuoma;
        //}
    }
    public void ExitButton()
    {
        SqlADL.SqlManager.DBAddLog("未激活退出", "Exit without Activation");
        Application.Quit();
    }
    //点击使用激活码激活
    public void JIHUOBtn()
    {
        shuruma = Jihuoma.text;
        if (string.IsNullOrEmpty(Jihuoma.text))
        {
            Debug.Log("请输入激活码");
            DebugTex.text = "请输入激活码!!!";
            return;
        }
        if (!string.IsNullOrEmpty(shuruma))
        {
            if (shuruma.Length < 10)
            {
                Jihuoma.text = "";
                DebugTex.text = "无效激活码!!!";
                return;
            }
            if (shuruma == DIF.Lock[0].jihuoma)
            {
                Debug.Log("上一个激活码已经过期,请重新取激活码");
                DebugTex.text = "上一个激活码已经过期,请重新取激活码!!!";
                return;
            }
            DIF.Lock[0].shibiema = str;
            DIF.Lock[0].jihuoma = Jihuoma.text;
            int yearNow = DateTime.Now.Year;
            int monthNow = DateTime.Now.Month;
            int DayNow = DateTime.Now.Day;
            string s = monthNow.ToString();
            string s1 = DayNow.ToString();
            if (monthNow < 10)
            {
                s = "0" + s;
            }
            if (DayNow < 10)
            {
                s1 = "0" + s1;
            }
            DIF.Lock[0].shijiancuo = yearNow.ToString() + s + s1;
            string s2 = Jihuoma.text[4].ToString() + Jihuoma.text[6].ToString();
            if (Jihuoma.text[4].ToString() != "q" && Jihuoma.text[4].ToString() != "s" && Jihuoma.text[4].ToString() != "b" && Jihuoma.text[4].ToString() != "y" && Jihuoma.text[4].ToString() != "n")
            {
                Debug.Log("激活码非法");
                DebugTex.text = "激活码非法,请重新输入!!!";
                Jihuoma.text = "";
                return;
            }
            if (Jihuoma.text[6].ToString() != "t" && Jihuoma.text[6].ToString() != "s" && Jihuoma.text[6].ToString() != "n")
            {
                Debug.Log("激活码非法");
                DebugTex.text = "激活码非法,请重新输入!!!";
                Jihuoma.text = "";
                return;
            }
            DL = (DataLimi)System.Enum.Parse(typeof(DataLimi), s2);

            switch (DL)
            {
                case DataLimi.qt:
                    if (DayNow > 23)
                    {
                        monthNow += 1;
                        DayNow = 7 - (30 - DayNow);
                    }
                    else
                    {
                        DayNow += 7;
                    }

                    break;
                case DataLimi.ss:
                    monthNow += 1;
                    break;
                case DataLimi.bn:
                    if (monthNow > 6)
                    {
                        yearNow += 1;
                        monthNow -= 6;
                    }
                    else
                    {
                        monthNow += 6;
                    }
                    break;
                case DataLimi.yn:
                    yearNow += 1;
                    break;
                case DataLimi.nn:
                    yearNow += 100;
                    break;
                default:
                    break;
            }

            string s3 = monthNow.ToString();
            string s4 = DayNow.ToString();
            if (monthNow < 10)
            {
                s3 = "0" + s3;
            }
            if (DayNow < 10)
            {
                s4 = "0" + s4;
            }
            int value0;
            int value1;
            int value8;
            int value9;
            if (!int.TryParse(shuruma[0].ToString(), out value0) || !int.TryParse(shuruma[1].ToString(), out value1) || !int.TryParse(shuruma[8].ToString(), out value8) || !int.TryParse(shuruma[9].ToString(), out value9))
            {
                Jihuoma.text = "";
                Debug.Log("无效激活码");
                DebugTex.text = "无效激活码请重新输入";
                return;
            }
            if (shuruma[2].ToString() != DIF.Lock[0].shibiema[value0].ToString() || shuruma[3].ToString() != DIF.Lock[0].shibiema[value8].ToString() || shuruma[5].ToString() != DIF.Lock[0].shibiema[value1].ToString() || shuruma[7].ToString() != DIF.Lock[0].shibiema[value9].ToString())
            {
                Jihuoma.text = "";
                Debug.Log("激活码错误");
                DebugTex.text = "激活码错误,请重新输入";
                return;
            }
            DIF.Lock[0].nianxian = yearNow.ToString() + s3 + s4;
            DIF.Lock[0].shifouguoqi = true;
            PlayerPrefs.SetString(Name, "shi");
            string path = Application.dataPath + "/StreamingAssets/Config/" + "Lock.json";
            JsonWR.Write<DataInfo>(DIF, path);

            //激活码写入VR程序
            string VRpath = Application.streamingAssetsPath + "/SZGYY_VR";
            if (Directory.Exists(VRpath))
            {
                string pathVR = Application.streamingAssetsPath + "/SZGYY_VR/SZGYY_VR_Data/StreamingAssets/Config/" + "Lock.json";
                JsonWR.Write<DataInfo>(DIF, pathVR);
            }

            Debug.Log("激活成功");
            JihuoEvent.Invoke();
            //SceneManager.LoadScene(1);
        }
        else
        {
            Debug.Log("请输入激活码");
            DebugTex.text = "请输入激活码!!!";
        }
    }

    /// <summary>
    /// str是已存在的激活码,str1是识别码,进行比对
    /// </summary>
    /// <param name="str"></param>
    /// <param name="str1"></param>
    /// <returns></returns>
    bool TestJihuoMa(string str, string str1, string nianxian, DataInfo dl)
    {
        dl.Lock[0].shibiema = str1;
        if (str.Length < 10)
        {
            Debug.Log("系统未曾激活");
            DebugTex.text = "系统未曾激活!!!";
            return false;
        }
        int value0 = int.Parse(str[0].ToString());
        int value1 = int.Parse(str[1].ToString());
        int value8 = int.Parse(str[8].ToString());
        int value9 = int.Parse(str[9].ToString());
        if (str[2].ToString() != str1[value0].ToString() || str[3].ToString() != str1[value8].ToString() || str[5].ToString() != str1[value1].ToString() || str[7].ToString() != str1[value9].ToString())
        {
            Debug.Log("激活码错误");
            return false;
        }

        //int year = int.Parse(nianxian.Substring(0, 4));
        //int month = int.Parse(nianxian.Substring(4, 2));
        //int day = int.Parse(nianxian.Substring(6, 2));
        //int yearNow = DateTime.Now.Year;
        //int monthNow = DateTime.Now.Month;
        //int DayNow = DateTime.Now.Day;
        //if (yearNow > year || monthNow > month || DayNow > day)
        //{
        //    PlayerPrefs.SetString(Name, "fou");
        //    Debug.Log("存在激活码,但是激活码已经过期!");
        //    DebugTex.text = "存在激活码,但是激活码已经过期!!!";
        //    dl.Lock[0].shifouguoqi = false;
        //    string path = Application.dataPath + "/StreamingAssets/Config/" + "Lock.json";
        //    JsonWR.Write<DataInfo>(dl, path);
        //    return false;  //如果是过期就应该重新激活
        //}

        DateTime getTime = DateTime.ParseExact(nianxian, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
        DateTime today = DateTime.Now;
        TimeSpan diff = getTime - today;
        //print("剩余天数:" + diff.TotalDays);
        if (diff.TotalDays <= 0)
        {
            PlayerPrefs.SetString(Name, "fou");
            Debug.Log("存在激活码,但是激活码已经过期!");
            DebugTex.text = "存在激活码,但是激活码已经过期!!!";
            dl.Lock[0].shifouguoqi = false;
            string path = Application.dataPath + "/StreamingAssets/Config/" + "Lock.json";
            JsonWR.Write<DataInfo>(dl, path);
            return false;  //如果是过期就应该重新激活
        }

        if (PlayerPrefs.GetString(Name) == "shi")
        {
            //SceneManager.LoadScene(1);
            JihuoEvent.Invoke();
            DebugTex.text = "已激活,到期时间:" + nianxian;
            return true;
        }
        else
        {
            DebugTex.text = "系统未曾激活!!!";
            return false;
        }

    }

    public class DataInfo
    {
        public List<LockItem> Lock;
    }

    public class LockItem
    {
        public string shibiema;
        public string jihuoma;
        public string shijiancuo;
        public string nianxian;
        public bool shifouguoqi;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值