Unity打包,加密限制软件使用时间

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

public class Learn : MonoBehaviour
{
    private static readonly byte[] Key = Encoding.UTF8.GetBytes("abcdefghijklmnop"); // 密钥,16个字节
    private static readonly byte[] IV = Encoding.UTF8.GetBytes("1234567890abcdef"); // 初始化向量,16个字节
    string path;
    //当前年、月、日、时、分、秒
    float curYear, curMonth, curDay, curHours, curMin, curSec;
    //截至XX年,XX月,XX日,XX时,XX分,XX秒
    float year,month,day,hours,min,sec;
    float startTime;
    float internalTime;
    string[] readContext=new string[2];
    private void Awake()
    {
        path = Application.dataPath + "/Privacy_Timer.txt";
        CreateTxtFile(path);
    }
    private void Start()
    {
        startTime = Time.unscaledTime;
        WriteTxtFile(path);
        //Debug.Log("Original Message: " + timer);
        //string encryptedMessage = Encrypt(timer);
        //Debug.Log("Encrypted Message: " + encryptedMessage);
        //string decryptedMessage = Decrypt(encryptedMessage);
        //Debug.Log("Decrypted Message: " + decryptedMessage);
    }
    private void Update()
    {
        internalTime = Time.unscaledTime - startTime;//获取程序运行的时间
        SetTimeValue(path, internalTime);
        CompareWithTime();
    }
    void CompareWithTime()
    {
        if (curYear > year)
        {
#if UNITY_EDITOR
            UnityEditor.EditorApplication.isPlaying = false;
#endif
#if UNITY_STANDALONE_WIN
            Application.Quit();
#endif
        }
        else if (curYear < year)
        {
            return;
        }
        else if (curYear == year)
        {
            if (curMonth > month)
            {
#if UNITY_EDITOR
                UnityEditor.EditorApplication.isPlaying = false;
#endif
#if UNITY_STANDALONE_WIN
                Application.Quit();
#endif
            }
            else if (curMonth < month)
            {
                return;
            }
            else if (curMonth == month)
            {
                if (curDay > day)
                {
#if UNITY_EDITOR
                    UnityEditor.EditorApplication.isPlaying = false;
#endif
#if UNITY_STANDALONE_WIN
                    Application.Quit();
#endif
                }
                else if (curDay < day)
                {
                    return;
                }
                else if (curDay == day)
                {
                    if (curHours > hours)
                    {
#if UNITY_EDITOR
                        UnityEditor.EditorApplication.isPlaying = false;
#endif
#if UNITY_STANDALONE_WIN
                        Application.Quit();
#endif
                    }
                    else if (curHours < hours)
                    {
                        return;
                    }
                    else if (curHours == hours)
                    {
                        if (curMin > min)
                        {
#if UNITY_EDITOR
                            UnityEditor.EditorApplication.isPlaying = false;
#endif
#if UNITY_STANDALONE_WIN
                            Application.Quit();
#endif
                        }
                        else if (curMin < min)
                        {
                            return;
                        }
                        else if (curMin == min)
                        {
                            if (curSec > sec)
                            {
#if UNITY_EDITOR
                                UnityEditor.EditorApplication.isPlaying = false;
#endif
#if UNITY_STANDALONE_WIN
                                Application.Quit();
#endif
                            }
                            else if (curSec <= sec)
                            {
                                return;
                            }
                        }
                    }
                }
            }
        }
    }
    //设置时间参数
    void SetTimeValue(string path,float timer)
    {
        //当前时间
        curYear = GetCurrentTime(path, timer, 0).Year;
        curMonth= GetCurrentTime(path, timer, 0).Month;
        curDay = GetCurrentTime(path, timer, 0).Day;
        curHours = GetCurrentTime(path, timer, 0).Hour;
        curMin = GetCurrentTime(path, timer, 0).Minute;
        curSec = GetCurrentTime(path, timer, 0).Second;
        //截至时间
        year = GetCurrentTime(path, 0, 1).Year;
        month = GetCurrentTime(path, 0, 1).Month;
        day = GetCurrentTime(path, 0, 1).Day;
        hours = GetCurrentTime(path, 0, 1).Hour;
        min = GetCurrentTime(path, 0, 1).Minute;
        sec = GetCurrentTime(path, 0, 1).Second;
    }
    void CreateTxtFile(string path)
    {
        if(!File.Exists(path))
        {
            File.Create(path);
        }
    }
    DateTime ChangeStringToDateTime(string timer)
    {
        string format = "yyyy/M/d HH:mm:ss";
        DateTime result = DateTime.ParseExact(timer, format, CultureInfo.InvariantCulture);
        return result;
    }
    DateTime GetCurrentTime(string path,float timer,int index)
    {
        if(string.IsNullOrEmpty(readContext[0]))
        {
            readContext = File.ReadAllLines(path);
            readContext[0] = Decrypt(readContext[0]);
            readContext[1] = Decrypt(readContext[1]);
        }
        if (!string.IsNullOrEmpty(readContext[0]))
        {
            if(index==0)
            {
                return ChangeStringToDateTime(readContext[0]).AddSeconds(timer);
            }
            else
            {
                return ChangeStringToDateTime(readContext[1]);
            }
        }
        return System.DateTime.Now;
    }
    //向文件中写入数据
    void WriteTxtFile(string path)
    {
        if(File.Exists(path))
        {
            string[] contents = File.ReadAllLines(path,Encoding.UTF8);
            if (contents.Length != 0)
            {
                contents[0] = Decrypt(contents[0]);
                contents[1] = Decrypt(contents[1]);
                DateTime start = ChangeStringToDateTime(contents[0]).AddSeconds(internalTime);
                contents[0] = start.ToString();
                contents[0] = Encrypt(contents[0]);
                contents[1] = Encrypt(contents[1]);
                File.WriteAllLines(path,contents,Encoding.UTF8);
            }
            else
            {
                contents = new string[2];
                DateTime startDate = DateTime.Now; // 当前时间
                DateTime endDate = startDate.AddDays(3); // 将startDate增加3天
                contents[0] = startDate.ToString();
                contents[1] = endDate.ToString();
                contents[0] = Encrypt(contents[0]);
                contents[1] = Encrypt(contents[1]);
                File.WriteAllLines(path, contents, Encoding.UTF8);
            }
        }
    }
    //加密
    public string Encrypt(string plainText)
    {
        byte[] encryptedBytes;
        using (Aes aesAlg = Aes.Create())
        {
            aesAlg.Key = Key;
            aesAlg.IV = IV;
            ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

            using (var msEncrypt = new System.IO.MemoryStream())
            {
                using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (var swEncrypt = new System.IO.StreamWriter(csEncrypt))
                    {
                        swEncrypt.Write(plainText);
                    }
                    encryptedBytes = msEncrypt.ToArray();
                }
            }
        }
        return Convert.ToBase64String(encryptedBytes);
    }
    //解密
    public string Decrypt(string encryptedText)
    {
        byte[] cipherTextBytes = Convert.FromBase64String(encryptedText);
        string plainText = null;
        using (Aes aesAlg = Aes.Create())
        {
            aesAlg.Key = Key;
            aesAlg.IV = IV;
            ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

            using (var msDecrypt = new System.IO.MemoryStream(cipherTextBytes))
            {
                using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (var srDecrypt = new System.IO.StreamReader(csDecrypt))
                    {
                        plainText = srDecrypt.ReadToEnd();
                    }
                }
            }
        }
        return plainText;
    }
    private void OnApplicationQuit()
    {
        WriteTxtFile(path);
    }
}

参考:

(3条消息) Unity 设置软件使用期限,加密软件_unity 给程序添加时间锁_山中雨来了的博客-CSDN博客

你可能感兴趣的:(Unity,unity,游戏引擎)