Browse Source

加入客户端试用期时间加密

master
Baozw 2 years ago
parent
commit
41766c0d3c
  1. 76
      Assets/MobileCommunication/Model/FBX/Materials/fffenggee.mat
  2. 9
      Assets/MobileCommunication/Model/FBX/Materials/fffenggee.mat.meta
  3. 44
      Assets/Scenes/PrepareLessons.unity
  4. 384
      Assets/Scripts/CheckDateUseful.cs
  5. 11
      Assets/Scripts/CheckDateUseful.cs.meta
  6. 9
      Assets/StreamingAssets/xml.meta
  7. 7
      Assets/StreamingAssets/xml/Validity.xml
  8. 8
      Assets/StreamingAssets/xml/Validity.xml.meta
  9. 42
      Assets/TimeLimit.prefab
  10. 7
      Assets/TimeLimit.prefab.meta
  11. 4
      ProjectSettings/EditorBuildSettings.asset

76
Assets/MobileCommunication/Model/FBX/Materials/fffenggee.mat

@ -0,0 +1,76 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: fffenggee
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 1
m_Colors:
- _Color: {r: 1, g: 0, b: 0, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}

9
Assets/MobileCommunication/Model/FBX/Materials/fffenggee.mat.meta

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 5e3c835a2c540884f991c28886ee5c41
timeCreated: 1664153274
licenseType: Pro
NativeFormatImporter:
mainObjectFileID: 2100000
userData:
assetBundleName:
assetBundleVariant:

44
Assets/Scenes/PrepareLessons.unity

@ -9117,8 +9117,8 @@ MonoBehaviour:
m_TargetGraphic: {fileID: 428}
m_HandleRect: {fileID: 284}
m_Direction: 0
m_Value: 0
m_Size: 0.9242243
m_Value: 1
m_Size: 1
m_NumberOfSteps: 0
m_OnValueChanged:
m_PersistentCalls:
@ -11204,3 +11204,43 @@ CanvasRenderer:
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1336960344}
--- !u!1 &1615115639
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 1615115641}
- component: {fileID: 1615115640}
m_Layer: 0
m_Name: TimeLimit
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1615115640
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1615115639}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ed6059b4b0846c747ad210996932c322, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!4 &1615115641
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1615115639}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 7
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

384
Assets/Scripts/CheckDateUseful.cs

@ -0,0 +1,384 @@
using UnityEngine;
using System.Collections;
using System;
using System.Xml;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
public class CheckDateUseful : MonoBehaviour {
private bool IsQuit = false;
private bool IsShowTips = false;
// Use this for initialization
private DateTime t1, t2, t11;
string original_filepath = "";
string encode_filepath = "";
string decode_filepath = "";
private int time_days = 30;//有效期30天
// Use this for initialization
void Start()
{
DontDestroyOnLoad(gameObject);
t2 = DateTime.Now;
t1 = DateTime.Now;
tipStyle = new GUIStyle();
tipStyle.fontSize = 40;
tipStyle.normal.textColor = Color.red;
original_filepath = Application.streamingAssetsPath + @"/xml/Validity.xml";
encode_filepath = Application.streamingAssetsPath + @"/xml/enValidity.xml";
decode_filepath = Application.streamingAssetsPath + @"/xml/deValidity.xml";
ControlXML();
}
string appid = "AXKC";
bool useFul = true;
DateTime RegistrationDate;
DateTime LastUseTime;
public DateTime GetRegistrationDate(string filepath)
{
if (File.Exists(filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNode root = xmlDoc.SelectSingleNode("assets");
XmlNodeList nodeList = root.ChildNodes;
foreach (XmlElement elem in nodeList)
{
if (elem.Name == "RegistrationDate")
{
return Convert.ToDateTime(elem.InnerText);
}
}
}
return DateTime.Now;
}
public DateTime GetLastUseTime(string filepath)
{
if (File.Exists(filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNode root = xmlDoc.SelectSingleNode("assets");
XmlNodeList nodeList = root.ChildNodes;
foreach (XmlElement elem in nodeList)
{
if (elem.Name == "LastUseTime")
{
return Convert.ToDateTime(elem.InnerText);
}
}
}
return DateTime.Now;
}
public void SetLastUseTime(string filepath)
{
if (File.Exists(filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNode root = xmlDoc.SelectSingleNode("assets");
XmlNodeList nodeList = root.ChildNodes;
foreach (XmlElement elem in nodeList)
{
if (elem.Name == "LastUseTime")
{
elem.InnerText = Convert.ToString(DateTime.Now);
}
}
xmlDoc.Save(filepath);
}
}
public bool GetUseful(string filepath)
{
if (File.Exists(filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNode root = xmlDoc.SelectSingleNode("assets");
XmlNodeList nodeList = root.ChildNodes;
foreach (XmlElement elem in nodeList)
{
if (elem.Name == "Useful")
{
return Convert.ToBoolean(elem.InnerText);
}
}
}
return false;
}
public void SetUsefulFalse(string filepath)
{
if (File.Exists(filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNode root = xmlDoc.SelectSingleNode("assets");
XmlNodeList nodeList = root.ChildNodes;
foreach (XmlElement elem in nodeList)
{
if (elem.Name == "Useful")
{
elem.InnerText = Convert.ToString(false);
}
}
xmlDoc.Save(filepath);
}
}
public void FirstSetXML(string filepath)
{
if (File.Exists(filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNode root = xmlDoc.SelectSingleNode("assets");
XmlNodeList nodeList = root.ChildNodes;
foreach (XmlElement elem in nodeList)
{
if (elem.Name == "Useful")
{
elem.InnerText = Convert.ToString(true);
}
if (elem.Name == "FirstExecute")
{
elem.InnerText = Convert.ToString(true);
}
if (elem.Name == "RegistrationDate")
{
elem.InnerText = Convert.ToString(DateTime.Now);
}
if (elem.Name == "OverTime")
{
}
if (elem.Name == "LastUseTime")
{
elem.InnerText = Convert.ToString(DateTime.Now);
}
}
xmlDoc.Save(filepath);
}
}
private GUIStyle tipStyle;
private int timer = 5;
string tips = "试用期已到,";
void OnGUI()
{
if (IsQuit)
{
t2 = DateTime.Now;
if (t2 - t11 > new TimeSpan(0, 0, 1))
{
if (timer > 0)
timer--;
t11 = t2;
}
//GUI.Label(new Rect(Screen.width / 2 - 150, Screen.height / 2 - 75, 300, 150), tips + timer + "秒后退出!", tipStyle);
Debug.LogWarning(tips);
if (t2 - t1 > new TimeSpan(0, 0, 5))
{
Debug.Log("程序退出!");
Application.Quit();
}
}
if (IsShowTips)
{
t2 = DateTime.Now;
if (t2 - t11 > new TimeSpan(0, 0, 1))
{
if (timer > 0)
timer--;
t11 = t2;
}
//GUI.Label(new Rect(Screen.width / 2 - 150, Screen.height / 2 - 75, 300, 150), tips + timer + "秒后关闭提示!", tipStyle);
Debug.LogWarning(tips);
if (t2 - t1 > new TimeSpan(0, 0, 3))
{
IsShowTips = false;
}
}
}
// Update is called once per frame
//xml文件加密解密
void ControlXML()
{
if (File.Exists(original_filepath))//如果找到原始文件,证明这次是第一次运行该程序,配置文件数据更新并生成加密文件
{
FirstSetXML(original_filepath);
GenerateEncodeXML1(original_filepath);
}
else
{
if (File.Exists(encode_filepath))
{
fileDcryption(encode_filepath);
if (File.Exists(decode_filepath))
{
CheckUseful();
GenerateEncodeXML1(decode_filepath);
File.Delete(decode_filepath);
}
}
else
{
tips = "未检测到配置文件,";
IsQuit = true;
IsShowTips = false;
return;
}
}
}
void CheckUseful()
{
useFul = GetUseful(decode_filepath);
if (!useFul)
{
tips = "试用期已到,";
IsQuit = true;
IsShowTips = false;
return;
}
LastUseTime = GetLastUseTime(decode_filepath);//上一次使用的时间
if (DateTime.Now < LastUseTime)
{
tips = "检测到时间不匹配,";
SetUsefulFalse(decode_filepath);
IsQuit = true;
IsShowTips = false;
return;
}
RegistrationDate = GetRegistrationDate(decode_filepath);//注册时间
TimeSpan span = DateTime.Now.Subtract(RegistrationDate);
int days = Math.Abs(span.Days);
if (days <= time_days)
{
tips = "试用期还剩" + (time_days - days).ToString() + "天,";
IsShowTips = true;
IsQuit = false;
SetLastUseTime(decode_filepath);
}
else
{
tips = "试用期结束,";
SetUsefulFalse(decode_filepath);
IsQuit = true;
IsShowTips = false;
return;
}
}
IEnumerator GenerateEncodeXML()
{
yield return new WaitForSeconds(1.0f);
fileEncryption(original_filepath, "assets");
if (File.Exists(encode_filepath))
{
File.Delete(original_filepath);
}
}
void GenerateEncodeXML1(string filepath)
{
fileEncryption(filepath, "assets");
if (File.Exists(encode_filepath))
{
File.Delete(filepath);
}
}
//加密xml文件
private void Encrypt(XmlDocument doc, string ElementName, SymmetricAlgorithm key)
{
XmlElement elementEncrypt = doc.GetElementsByTagName(ElementName)[0] as XmlElement;
EncryptedXml eXml = new EncryptedXml();
byte[] encryptElement = eXml.EncryptData(elementEncrypt, key, false);//
EncryptedData edElement = new EncryptedData();
edElement.Type = EncryptedXml.XmlEncElementUrl;
string encryptionMethod = null;
if (key is TripleDES)
{
encryptionMethod = EncryptedXml.XmlEncTripleDESUrl;
}
else if (key is DES)
{
encryptionMethod = EncryptedXml.XmlEncDESUrl;
}
if (key is Rijndael)
{
switch (key.KeySize)
{
case 128:
encryptionMethod = EncryptedXml.XmlEncAES128Url;
break;
case 192:
encryptionMethod = EncryptedXml.XmlEncAES192Url;
break;
case 256:
encryptionMethod = EncryptedXml.XmlEncAES256Url;
break;
}
}
edElement.EncryptionMethod = new EncryptionMethod(encryptionMethod);
edElement.CipherData.CipherValue = encryptElement;
EncryptedXml.ReplaceElement(elementEncrypt, edElement, false);
}
//XML文件解密
private void Decrypt(XmlDocument doc, SymmetricAlgorithm Alg)
{
XmlElement encryptedElement = doc.GetElementsByTagName("EncryptedData")[0] as XmlElement;
EncryptedData edElement = new EncryptedData();
edElement.LoadXml(encryptedElement);
EncryptedXml exml = new EncryptedXml();
byte[] rgbOutput = exml.DecryptData(edElement, Alg);
exml.ReplaceData(encryptedElement, rgbOutput);
}
//举例,对某个XML文件加密
private void fileEncryption(string filename,string ElementName)
{
RijndaelManaged key = new RijndaelManaged();
//设置密钥:key为32位=数字或字母16个=汉子8个
byte[] byteKey = Encoding.Unicode.GetBytes("1111111111111111");
key.Key = byteKey;
XmlDocument xmldoc = new XmlDocument();
xmldoc.PreserveWhitespace = true;
xmldoc.Load(filename);//想要加密的xml文件
Encrypt(xmldoc, ElementName, key);//需要加密的节点
if (key != null)
{
key.Clear();
}
xmldoc.Save(encode_filepath);//生成加密后的xml文件
}
//举例,对某个xml文件解密
private void fileDcryption(string filename)
{
RijndaelManaged key = new RijndaelManaged();
byte[] byteKey = Encoding.Unicode.GetBytes("1111111111111111");
key.Key = byteKey;
XmlDocument xmldoc = new XmlDocument();
xmldoc.PreserveWhitespace = true;
xmldoc.Load(encode_filepath);//加载要解密的xml文件
Decrypt(xmldoc, key);
if (key != null)
{
key.Clear();
}
xmldoc.Save(decode_filepath);//生成解密后的文件
}
}

11
Assets/Scripts/CheckDateUseful.cs.meta

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ed6059b4b0846c747ad210996932c322
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

9
Assets/StreamingAssets/xml.meta

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 80e7eff0d94074846b3374624b977faa
folderAsset: yes
timeCreated: 1664154874
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

7
Assets/StreamingAssets/xml/Validity.xml

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<assets>
<Useful></Useful>
<FirstExecute></FirstExecute>
<RegistrationDate></RegistrationDate>
<LastUseTime></LastUseTime>
</assets>

8
Assets/StreamingAssets/xml/Validity.xml.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 8acd2c7bfc11de7469deb8a346682cf8
timeCreated: 1664164866
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

42
Assets/TimeLimit.prefab

@ -0,0 +1,42 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &7510964028916638499
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 7510964028916638501}
- component: {fileID: 7510964028916638500}
m_Layer: 0
m_Name: TimeLimit
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
--- !u!114 &7510964028916638500
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 7510964028916638499}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ed6059b4b0846c747ad210996932c322, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!4 &7510964028916638501
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 7510964028916638499}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -0.46200034, y: 0.101851225, z: -0.10841513}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

7
Assets/TimeLimit.prefab.meta

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 857ebbe9fbeb13242934e0d2b659ba00
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

4
ProjectSettings/EditorBuildSettings.asset

@ -38,13 +38,13 @@ EditorBuildSettings:
- enabled: 0
path: Assets/Scenes/0.unity
guid: f6a70eed2bbad9740b9ad510468f5cd3
- enabled: 0
- enabled: 1
path: Assets/Scenes/11.unity
guid: 7263093372205c54fa8ba0968ac76a9e
- enabled: 0
path: Assets/Scenes/1.unity
guid: 3374f42471f44b74aa371dd9c8b2d604
- enabled: 1
- enabled: 0
path: Assets/Scenes/2.unity
guid: 7117ff1ea3af4c646a2b73c19a84af20
- enabled: 0

Loading…
Cancel
Save