Browse Source

换成双密钥加密

develop
曹衍涛 4 years ago
parent
commit
c6003aad91
  1. BIN
      Assets/Scene/Dummy.unity
  2. 153
      Assets/Scripts/SmartX1/SmartController.cs
  3. 2
      Assets/StreamingAssets/xml/enSmartXProject.xml
  4. 2
      Assets/StreamingAssets/xml/enSmartXProject2.xml
  5. 7
      Assets/StreamingAssets/xml/enSmartXProject2.xml.meta
  6. BIN
      ProjectSettings/ProjectSettings.asset

BIN
Assets/Scene/Dummy.unity

Binary file not shown.

153
Assets/Scripts/SmartX1/SmartController.cs

@ -3,26 +3,28 @@ 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
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;
string appid = "AXKC";
// Use this for initialization
void Start () {
void Start()
{
t2 = DateTime.Now;
t1 = DateTime.Now;
DontDestroyOnLoad(GameObject.Find("SmartX1"));
tipStyle = new GUIStyle();
tipStyle.fontSize = 40;
tipStyle.normal.textColor = Color.red;
@ -30,15 +32,22 @@ public class SmartController : MonoBehaviour
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";
ControlXML();
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();
@ -62,60 +71,56 @@ public class SmartController : MonoBehaviour
}
return "";
}
bool FindSmartX1()
void FindSmartX1()
{
try
{
Rtn = smart.SmartX1Find(appid, keyHandles, keyNumber);
if (Rtn != 0)
{
IsQuit = true;
t1 = DateTime.Now;
t2 = DateTime.Now;
t11 = DateTime.Now;
return false;
if (Rtn == 0)
{
return;
}
Rtn = smart.SmartX1Find(appid2, keyHandles, keyNumber);
if (Rtn == 0)
{
return;
}
Debug.Log("SmartX1Find Successfully");
return true;
IsQuit = true;
t1 = DateTime.Now;
t2 = DateTime.Now;
t11 = DateTime.Now;
}
catch (Exception ex)
{
Debug.Log(keyHandles[0]);
Debug.Log("异常" + ex.Message);
GameObject.Find("Application").GetComponent<ApplicationClose>().isSmartX1Exit = true;
Application.Quit();
return false;
}
}
bool CheckExist()
void CheckExist()
{
try
{
Rtn = smart.SmartX1CheckExist(keyHandles[0]);
if (Rtn != 0)
{
//Debug.Log("SmartX1 not exist!" + Rtn.ToString());
IsQuit = true;
}
if (IsQuit)
{
t1 = DateTime.Now;
t2 = DateTime.Now;
t11 = DateTime.Now;
return false;
}
//Debug.Log("SmartX1Exist!");
return true;
}
catch (Exception ex)
{
//Debug.Log(keyHandles[0]);
//Debug.Log("异常" + ex.Message);
GameObject.Find("Application").GetComponent<ApplicationClose>().isSmartX1Exit = true;
Debug.Log("异常" + ex.Message);
Application.Quit();
return false;
}
}
private GUIStyle tipStyle;
private int timer = 5;
void OnGUI()
@ -123,25 +128,24 @@ public class SmartController : MonoBehaviour
if (IsQuit)
{
t2 = DateTime.Now;
if(t2-t11>new TimeSpan(0,0,1))
if (t2 - t11 > new TimeSpan(0, 0, 1))
{
if(timer>0)
if (timer > 0)
timer--;
t11 = t2;
}
GUI.Label(new Rect(Screen.width / 2 - 150, Screen.height / 2 - 75, 300, 150), "密钥验证失败,程序将在"+timer+"秒后退出!", tipStyle);
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("程序退出!");
GameObject.Find("Application").GetComponent<ApplicationClose>().isSmartX1Exit = true;
Debug.Log("程序退出!");
Application.Quit();
}
}
}
// Update is called once per frame
void Update () {
// Update is called once per frame
void Update()
{
if (IsQuit)
return;
t2 = DateTime.Now;
@ -150,48 +154,37 @@ public class SmartController : MonoBehaviour
t1 = t2;
CheckExist();
}
}
}
//xml文件加密解密
void ControlXML()
string ControlXML(string originalFile, string encodeFile, string decodeFile)
{
if (File.Exists(original_filepath))//证明这次是第一次运行该程序,获取当前配置文件
var id = "";
if (File.Exists(originalFile))//证明这次是第一次运行该程序,获取当前配置文件
{
appid = GetAppID(original_filepath);
//StartCoroutine(GenerateEncodeXML());
GenerateEncodeXML1();
id = GetAppID(originalFile);
fileEncryption(originalFile, encodeFile);
if (File.Exists(encodeFile))
{
File.Delete(originalFile);
}
}
else
{
if (File.Exists(encode_filepath))
if (File.Exists(encodeFile))
{
fileDcryption(encode_filepath);
if (File.Exists(decode_filepath))
fileDcryption(encodeFile, decodeFile);
if (File.Exists(decodeFile))
{
appid = GetAppID(decode_filepath);
File.Delete(decode_filepath);
id = GetAppID(decodeFile);
File.Delete(decodeFile);
}
}
}
}
IEnumerator GenerateEncodeXML()
{
yield return new WaitForSeconds(1.0f);
fileEncryption(original_filepath);
if (File.Exists(encode_filepath))
{
File.Delete(original_filepath);
}
}
void GenerateEncodeXML1()
{
fileEncryption(original_filepath);
if (File.Exists(encode_filepath))
{
File.Delete(original_filepath);
}
//Debug.Log("--------->" + id);
return id;
}
//加密xml文件
@ -248,8 +241,11 @@ public class SmartController : MonoBehaviour
string encode_filepath = "";
string decode_filepath = "";
string original_filepath2 = "";
string encode_filepath2 = "";
string decode_filepath2 = "";
//举例,对某个XML文件加密
private void fileEncryption(string filename)
private void fileEncryption(string originalFile, string encodeFile)
{
RijndaelManaged key = new RijndaelManaged();
//设置密钥:key为32位=数字或字母16个=汉子8个
@ -257,29 +253,28 @@ public class SmartController : MonoBehaviour
key.Key = byteKey;
XmlDocument xmldoc = new XmlDocument();
xmldoc.PreserveWhitespace = true;
xmldoc.Load(original_filepath);//想要加密的xml文件
xmldoc.Load(originalFile);//想要加密的xml文件
Encrypt(xmldoc, "SmartX1", key);//需要加密的节点
if (key != null)
{
key.Clear();
}
xmldoc.Save(encode_filepath);//生成加密后的xml文件
xmldoc.Save(encodeFile);//生成加密后的xml文件
}
//举例,对某个xml文件解密
private void fileDcryption(string filename)
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(encode_filepath);//加载要解密的xml文件
xmldoc.Load(encodeFile);//加载要解密的xml文件
Decrypt(xmldoc, key);
if (key != null)
{
key.Clear();
}
xmldoc.Save(decode_filepath);//生成解密后的文件
xmldoc.Save(decodeFile);//生成解密后的文件
}
}

2
Assets/StreamingAssets/xml/enSmartXProject.xml

File diff suppressed because one or more lines are too long

2
Assets/StreamingAssets/xml/enSmartXProject2.xml

File diff suppressed because one or more lines are too long

7
Assets/StreamingAssets/xml/enSmartXProject2.xml.meta

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 29b09fd96247e6f4cb05f21822872a3d
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

BIN
ProjectSettings/ProjectSettings.asset

Binary file not shown.
Loading…
Cancel
Save