Unity中简单加密实现软件限制使用次数和时间的小功能

Unity中实现简单限制软件打开的小功能

最近公司需求是需要在电脑上根据MAC地址软件 才能被打开,陆陆续续的又提了时间限制打开次数限制。

  通过打包之后第一次运行获取MAC地址(用于临时展示,大家也可以将信息加密一下再保存)保存在文本中,每次打开软件会判断当前是MAC地址是否一样,不一样则软件退出。

功能脚本:

using System;
using System.IO;
using System.Net.NetworkInformation;
using System.Text;
using UnityEngine;

public class LoginConfig : MonoBehaviour

{
    [Range(2021, 2023)]
    public int year = 2021;

    [Range(1, 12)]
    public int month = 1;

    [Range(1, 31)]
    public int day = 1;

    [Header("可以打开的次数")]
    public int EndCount = 3;

    [Header("是否开启MAC地址判定")]
    public bool isMainID = true;

    [Header("是否开启时间判定")]
    public bool isEndTime = true;

    [Header("是否开启次数判定")]
    public bool isCount = true;

    private string path = Application.streamingAssetsPath + "/Encryption/Complex/intelligence/config.txt";

    private void Start()

    {
        getJsonData();

        if (isMainID)

        {
            if (!GetmainID(GetMacAddress()))

            {
                Application.Quit();
            }
        }

        if (isEndTime)

        {
            if (!ComPareGetEndTime())

            {
                Application.Quit();
            }
        }

        if (isCount)

        {
            if (!CompareCount())

            {
                Application.Quit();
            }
        }
    }

    /// <summary>

    /// 获取配置

    /// </summary>

    private string[] getJsonData()

    {
        string[] strs = File.ReadAllText(path).Split('|');

        if (strs.Length != 2 || strs[0] == null || strs[1] == null)

        {
            //初始化

            string data = GetMacAddress() + "|0";

            File.WriteAllBytes(path, Encoding.UTF8.GetBytes(data));
        }

        return strs;
    }

    #region 获取MAC地址

    private bool GetmainID(string macid)
    {
        if (Application.platform!= RuntimePlatform.WindowsEditor)
        {
            string newMAC = getJsonData()[0];

            if (newMAC.Equals(GetMacAddress()) && GetMacAddress() != null)

            {
                return true;
            }
            else

            {
                return false;
            }
        }
        return true;
       
    }

    /// <summary>

    /// MAC地址

    /// </summary>

    /// <returns></returns>

    private static string GetMacAddress()

    {
        string physicalAddress = "";

        NetworkInterface[] nice = NetworkInterface.GetAllNetworkInterfaces();

        foreach (NetworkInterface adaper in nice)

        {
            Debug.Log(adaper.Description);

            if (adaper.Description == "en0")

            {
                physicalAddress = adaper.GetPhysicalAddress().ToString();

                break;
            }
            else

            {
                physicalAddress = adaper.GetPhysicalAddress().ToString();

                if (physicalAddress != "")

                {
                    break;
                };
            }
        }

        return physicalAddress;
    }

    #endregion 获取MAC地址

    #region 时间比较

    public bool ComPareGetEndTime()

    {
        string EndTime = year + "-" + month + "-" + day;

        //当前的时间

        DateTime nowTimeDate = DateTime.Today;

        DateTime endDataTime = Convert.ToDateTime(EndTime);

        try

        {
            if (endDataTime.CompareTo(nowTimeDate) < 0)

            {
                return false;
            }
            else

            {
                return true;
            }
        }
        catch (Exception)

        {
            throw;
        }
    }

    #endregion 时间比较

    #region 次数比较

    private bool CompareCount()

    {
        if (Application.platform!= RuntimePlatform.WindowsEditor)
        {
            int index = int.Parse(getJsonData()[1]);

            int value = index + 1;

            string newData = getJsonData()[0] + "|" + value.ToString();

            File.WriteAllBytes(path, Encoding.UTF8.GetBytes(newData));

            if (EndCount > index)

            {
                return true;
            }
            else

            {
                return false;
            }
        }
        return true;
    }

    #endregion 次数比较
}

将需要勾选即可。
在这里插入图片描述

  • 4
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一个简单Unity C# 脚本,使用 HttpListener 实现加密签名: ```csharp using System; using System.Net; using System.Text; using System.Security.Cryptography; using UnityEngine; public class HttpListenerExample : MonoBehaviour { private HttpListener listener; // 在启动时运行 void Start() { // 创建 HttpListener 对象 listener = new HttpListener(); // 添加要监听的 URI listener.Prefixes.Add("http://localhost:8080/"); // 启动监听器 listener.Start(); // 开始异步处理传入请求 listener.BeginGetContext(ProcessRequest, null); } // 处理传入请求 void ProcessRequest(IAsyncResult result) { // 获取请求上下文 HttpListenerContext context = listener.EndGetContext(result); // 获取请求对象 HttpListenerRequest request = context.Request; // 获取请求数据 byte[] requestData = Encoding.UTF8.GetBytes(request.QueryString.ToString()); // 获取签名 string signature = GetSignature(requestData, "your_secret_key_here"); // 返回签名 byte[] responseData = Encoding.UTF8.GetBytes(signature); context.Response.ContentType = "text/plain"; context.Response.ContentLength64 = responseData.Length; context.Response.OutputStream.Write(responseData, 0, responseData.Length); // 关闭响应 context.Response.Close(); // 继续异步处理传入请求 listener.BeginGetContext(ProcessRequest, null); } // 获取签名 string GetSignature(byte[] data, string secretKey) { // 使用 HMAC-SHA256 算法计算签名 using (HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(secretKey))) { byte[] hash = hmac.ComputeHash(data); return BitConverter.ToString(hash).Replace("-", "").ToLower(); } } // 在停止时运行 void OnDestroy() { // 停止监听器 listener.Stop(); listener.Close(); } } ``` 请注意,这个示例仅仅演示了如何使用 HttpListener 实现加密签名。在实际应用,您需要根据您的具体需求进行修改和扩展。同时,建议对密钥进行加密保护,并使用 HTTPS 等安全协议来保证传输的安全性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值