You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
281 lines
8.5 KiB
281 lines
8.5 KiB
4 years ago
|
using UnityEngine;
|
||
|
using System;
|
||
|
using System.Xml;
|
||
|
using System.IO;
|
||
|
using System.Text;
|
||
|
|
||
|
using System.Security.Cryptography;
|
||
|
using System.Security.Cryptography.Xml;
|
||
|
using System.Collections;
|
||
|
|
||
|
using SmartX1Demo;
|
||
|
|
||
|
public class SmartController : MonoBehaviour
|
||
|
{
|
||
|
int[] keyHandles = new int[8];
|
||
|
int[] keyNumber = new int[8];
|
||
|
private int Rtn = 0;
|
||
|
SmartApp smart = new SmartApp();
|
||
|
private bool IsQuit = false;
|
||
|
|
||
|
private DateTime t1, t2, t11;
|
||
|
void Start()
|
||
|
{
|
||
|
t2 = DateTime.Now;
|
||
|
t1 = DateTime.Now;
|
||
|
DontDestroyOnLoad(GameObject.Find("SmartX1"));
|
||
|
|
||
|
tipStyle = new GUIStyle();
|
||
|
tipStyle.fontSize = 40;
|
||
|
tipStyle.normal.textColor = Color.red;
|
||
|
|
||
|
original_filepath = Application.streamingAssetsPath + @"/xml/SmartXProject.xml";
|
||
|
encode_filepath = Application.streamingAssetsPath + @"/xml/enSmartXProject.xml";
|
||
|
decode_filepath = Application.streamingAssetsPath + @"/xml/deSmartXProject.xml";
|
||
|
original_filepath2 = Application.streamingAssetsPath + @"/xml/SmartXProject2.xml";
|
||
|
encode_filepath2 = Application.streamingAssetsPath + @"/xml/enSmartXProject2.xml";
|
||
|
decode_filepath2 = Application.streamingAssetsPath + @"/xml/deSmartXProject2.xml";
|
||
|
|
||
|
appid = ControlXML(original_filepath, encode_filepath, decode_filepath);
|
||
|
appid2 = ControlXML(original_filepath2, encode_filepath2, decode_filepath2);
|
||
|
//appid = GetAppID(original_filepath);
|
||
|
|
||
|
//检测加密狗是否存在
|
||
|
FindSmartX1();
|
||
|
}
|
||
|
string appid = "AXKC";
|
||
|
string appid2 = "YuAnguanli";
|
||
|
public string GetAppID(string filepath)
|
||
|
{
|
||
|
//filepath = Application.dataPath + @"/xml/SmartXProject.xml";
|
||
|
if (File.Exists(filepath))
|
||
|
{
|
||
|
XmlDocument xmlDoc = new XmlDocument();
|
||
|
xmlDoc.Load(filepath);
|
||
|
XmlNode root = xmlDoc.SelectSingleNode("SmartX1");
|
||
|
XmlNodeList nodeList = root.ChildNodes;
|
||
|
foreach (XmlElement elem in nodeList)
|
||
|
{
|
||
|
if (elem.Name == "BaseInfo")
|
||
|
{
|
||
|
foreach (XmlElement zq in elem.ChildNodes)
|
||
|
{
|
||
|
if (zq.Name == "AppID")
|
||
|
{
|
||
|
return zq.InnerText;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
return "";
|
||
|
}
|
||
|
void FindSmartX1()
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
Rtn = smart.SmartX1Find(appid, keyHandles, keyNumber);
|
||
|
if (Rtn == 0)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
Rtn = smart.SmartX1Find(appid2, keyHandles, keyNumber);
|
||
|
if (Rtn == 0)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
IsQuit = true;
|
||
|
t1 = DateTime.Now;
|
||
|
t2 = DateTime.Now;
|
||
|
t11 = DateTime.Now;
|
||
|
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
Debug.Log("异常" + ex.Message);
|
||
|
Application.Quit();
|
||
|
}
|
||
|
}
|
||
|
void CheckExist()
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
Rtn = smart.SmartX1CheckExist(keyHandles[0]);
|
||
|
if (Rtn != 0)
|
||
|
{
|
||
|
IsQuit = true;
|
||
|
}
|
||
|
|
||
|
if (IsQuit)
|
||
|
{
|
||
|
t1 = DateTime.Now;
|
||
|
t2 = DateTime.Now;
|
||
|
t11 = DateTime.Now;
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
Debug.Log("异常" + ex.Message);
|
||
|
Application.Quit();
|
||
|
}
|
||
|
}
|
||
|
private GUIStyle tipStyle;
|
||
|
private int timer = 5;
|
||
|
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), "密钥验证失败,程序将在" + timer + "秒后退出!", tipStyle);
|
||
|
|
||
|
if (t2 - t1 > new TimeSpan(0, 0, 5))
|
||
|
{
|
||
|
Debug.Log("程序退出!");
|
||
|
Application.Quit();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
// Update is called once per frame
|
||
|
void Update()
|
||
|
{
|
||
|
if (IsQuit)
|
||
|
return;
|
||
|
t2 = DateTime.Now;
|
||
|
if (t2 - t1 > new TimeSpan(0, 0, 0, 10, 0))
|
||
|
{
|
||
|
t1 = t2;
|
||
|
CheckExist();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
//xml文件加密解密
|
||
|
|
||
|
string ControlXML(string originalFile, string encodeFile, string decodeFile)
|
||
|
{
|
||
|
var id = "";
|
||
|
if (File.Exists(originalFile))//证明这次是第一次运行该程序,获取当前配置文件
|
||
|
{
|
||
|
id = GetAppID(originalFile);
|
||
|
fileEncryption(originalFile, encodeFile);
|
||
|
if (File.Exists(encodeFile))
|
||
|
{
|
||
|
File.Delete(originalFile);
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (File.Exists(encodeFile))
|
||
|
{
|
||
|
fileDcryption(encodeFile, decodeFile);
|
||
|
if (File.Exists(decodeFile))
|
||
|
{
|
||
|
id = GetAppID(decodeFile);
|
||
|
File.Delete(decodeFile);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
//Debug.Log("--------->" + id);
|
||
|
return id;
|
||
|
}
|
||
|
|
||
|
//加密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);
|
||
|
}
|
||
|
|
||
|
string original_filepath = "";
|
||
|
string encode_filepath = "";
|
||
|
string decode_filepath = "";
|
||
|
|
||
|
string original_filepath2 = "";
|
||
|
string encode_filepath2 = "";
|
||
|
string decode_filepath2 = "";
|
||
|
//举例,对某个XML文件加密
|
||
|
private void fileEncryption(string originalFile, string encodeFile)
|
||
|
{
|
||
|
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(originalFile);//想要加密的xml文件
|
||
|
Encrypt(xmldoc, "SmartX1", key);//需要加密的节点
|
||
|
if (key != null)
|
||
|
{
|
||
|
key.Clear();
|
||
|
}
|
||
|
xmldoc.Save(encodeFile);//生成加密后的xml文件
|
||
|
}
|
||
|
//举例,对某个xml文件解密
|
||
|
private void fileDcryption(string encodeFile, string decodeFile)
|
||
|
{
|
||
|
RijndaelManaged key = new RijndaelManaged();
|
||
|
byte[] byteKey = Encoding.Unicode.GetBytes("1111111111111111");
|
||
|
key.Key = byteKey;
|
||
|
XmlDocument xmldoc = new XmlDocument();
|
||
|
xmldoc.PreserveWhitespace = true;
|
||
|
xmldoc.Load(encodeFile);//加载要解密的xml文件
|
||
|
Decrypt(xmldoc, key);
|
||
|
if (key != null)
|
||
|
{
|
||
|
key.Clear();
|
||
|
}
|
||
|
xmldoc.Save(decodeFile);//生成解密后的文件
|
||
|
}
|
||
|
}
|