之前在公司的时候,在没有完全结项目的时候,再给客户发执行文件的时候经常会加一些时间限制,之前用的是读取当前设备的系统时间,这样也能达到目的。但是可以用改时间来应对,下面我分享的插件,我进行了加密设置,只要时间一过,就不能运行了,改时间什么的都不会失去效用。下面是代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
using UnityEngine; using System; using System.IO; using System.Collections.Generic; using System.Text; using UnityEngine.Networking.Match; using System.Text.RegularExpressions; public class TimerCtrl : MonoBehaviour { [Serializable] public struct Timer { public int year; public int month; public int day; public Timer(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } } [HideInInspector] public Timer mTimer = new Timer(2016, 12, 31); private DateTime targetTime; private DateTime currentTime; private string path; private string filename = "DATE.txt"; private string current; void Awake() { #if !UNITY_EDITOR //每当过期后重新设置时间的时候将下面的初始密匙复制到Keys文件夹下的DATE.txt //初始的密匙:074123084123074123084123075123075123075123074123091123074123073123065123075123075123065123075123075123091123058123054123 targetTime = new DateTime(mTimer.year, mTimer.month, mTimer.day); Debug.Log("targetTime = " + targetTime); path = Application.streamingAssetsPath + "/Keys"; var file = Path.Combine(path, filename); FileInfo fileInfo = new FileInfo(file); if (fileInfo.Exists) { StreamReader sr = new StreamReader(file); var _time = sr.ReadToEnd(); sr.Close(); sr.Dispose(); if (!string.IsNullOrEmpty(_time)) { current = GetKey(_time); Debug.Log("current = " + current); } else { Application.Quit(); Debug.LogWarning("Key is null ! ! !"); } } else { //如果文件不存在直接退出 Application.Quit(); Debug.LogWarning("Path is null ! ! !"); return; } currentTime = current == DateTime.MinValue.ToString() ? DateTime.Now : DateTime.Parse(current); TimeSpan timeSpan = targetTime - currentTime; if (timeSpan.TotalSeconds < 0) { if (current == DateTime.MinValue.ToString()) { StreamWriter sw = new StreamWriter(file); var _time = AddKey(DateTime.Now.ToString()); sw.WriteLine(_time); sw.Close(); sw.Dispose(); } Application.Quit(); } #endif } /// <summary> /// 加密 /// </summary> /// <param name="str"></param> /// <returns></returns> public string AddKey(string str) { StringBuilder sb = new StringBuilder(); byte[] bytes1 = Encoding.Unicode.GetBytes(str); for (int i = 0; i < bytes1.Length; i++) { bytes1[i] = (byte)(bytes1[i] ^ 123); sb.AppendFormat("{0:D3}", bytes1[i]); } return sb.ToString(); } /// <summary> /// 解密 /// </summary> /// <param name="str"></param> /// <returns></returns> public string GetKey(string str) { MatchCollection matches = Regex.Matches(str, @"\d{3}"); byte[] bytes2 = new byte[matches.Count]; for (int i = 0; i < matches.Count; i++) bytes2[i] = (byte)(byte.Parse(matches[i].Value) ^ 123); return Encoding.Unicode.GetString(bytes2); } } |
我的方法是将时间进行加密,之后保存起来,然后和当前时间比较,这个有一个缺陷,就是如果在没有过期的时候改时间,还是会有问题,希望大家提一些想法,之后也会更新这个插件的,敬请关注。文章源自大腿Plus-https://www.zhaoshijun.com/archives/445
插件下载链接: http://pan.baidu.com/s/1jHZZ06E 密码: 6d62文章源自大腿Plus-https://www.zhaoshijun.com/archives/445 文章源自大腿Plus-https://www.zhaoshijun.com/archives/445
我的微信
微信扫一扫
shijun_z
我的QQ
QQ扫一扫
846207670
1F
这个解密data里的初始密匙解析出来是1/1/0001 12:00:00 AM,测试通不过,难道我哪里测试写错了
B1
@ 0过河小卒0 这个可能存在一些问题,我忘了初始密钥是什么了,不过思路是没什么问题,如果测试不通过,可能是那个data文件里的初始密钥和代码里里注释掉的密钥不一致导致的