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

}

在这里插入图片描述

  • 10
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

四夕立羽

你的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值