01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /// <summary> /// 將一個(gè)對(duì)象序列化為字符串 /// </summary> /// <returns>The object.</returns> /// <param name="pObject">對(duì)象</param> /// <param name="pType">對(duì)象類型</param> private static string SerializeObject( object pObject) { //序列化后的字符串 string serializedString = string .Empty; //使用Json.Net進(jìn)行序列化 serializedString = JsonConvert.SerializeObject(pObject); return serializedString; } /// <summary> /// 將一個(gè)字符串反序列化為對(duì)象 /// </summary> /// <returns>The object.</returns> /// <param name="pString">字符串</param> /// <param name="pType">對(duì)象類型</param> private static object DeserializeObject( string pString,Type pType) { //反序列化后的對(duì)象 object deserializedObject = null ; //使用Json.Net進(jìn)行反序列化 deserializedObject=JsonConvert.DeserializeObject(pString,pType); return deserializedObject; } |
01 02 03 04 05 06 07 08 09 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 | /// <summary> /// Rijndael加密算法 /// </summary> /// <param name="pString">待加密的明文</param> /// <param name="pKey">密鑰,長(zhǎng)度可以為:64位(byte[8]),128位(byte[16]),192位(byte[24]),256位(byte[32])</param> /// <param name="iv">iv向量,長(zhǎng)度為128(byte[16])</param> /// <returns></returns> private static string RijndaelEncrypt( string pString, string pKey) { //密鑰 byte [] keyArray = UTF8Encoding.UTF8.GetBytes(pKey); //待加密明文數(shù)組 byte [] toEncryptArray = UTF8Encoding.UTF8.GetBytes(pString); //Rijndael解密算法 RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateEncryptor(); //返回加密后的密文 byte [] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); } /// <summary> /// ijndael解密算法 /// </summary> /// <param name="pString">待解密的密文</param> /// <param name="pKey">密鑰,長(zhǎng)度可以為:64位(byte[8]),128位(byte[16]),192位(byte[24]),256位(byte[32])</param> /// <param name="iv">iv向量,長(zhǎng)度為128(byte[16])</param> /// <returns></returns> private static String RijndaelDecrypt( string pString, string pKey) { //解密密鑰 byte [] keyArray = UTF8Encoding.UTF8.GetBytes(pKey); //待解密密文數(shù)組 byte [] toEncryptArray = Convert.FromBase64String(pString); //Rijndael解密算法 RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateDecryptor(); //返回解密后的明文 byte [] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return UTF8Encoding.UTF8.GetString(resultArray); } |
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | /** * Unity3D數(shù)據(jù)持久化輔助類 * 作者:秦元培 * 時(shí)間:2015年8月14日 **/ using UnityEngine; using System.Collections; using System; using System.IO; using System.Text; using System.Security.Cryptography; using Newtonsoft.Json; public static class IOHelper { /// <summary> /// 判斷文件是否存在 /// </summary> public static bool IsFileExists( string fileName) { return File.Exists(fileName); } /// <summary> /// 判斷文件夾是否存在 /// </summary> public static bool IsDirectoryExists( string fileName) { return Directory.Exists(fileName); } /// <summary> /// 創(chuàng)建一個(gè)文本文件 /// </summary> /// <param name="fileName">文件路徑</param> /// <param name="content">文件內(nèi)容</param> public static void CreateFile( string fileName, string content) { StreamWriter streamWriter = File.CreateText(fileName); streamWriter.Write(content); streamWriter.Close(); } /// <summary> /// 創(chuàng)建一個(gè)文件夾 /// </summary> public static void CreateDirectory( string fileName) { //文件夾存在則返回 if (IsDirectoryExists (fileName)) return ; Directory.CreateDirectory(fileName); } public static void SetData( string fileName, object pObject) { //將對(duì)象序列化為字符串 string toSave = SerializeObject(pObject); //對(duì)字符串進(jìn)行加密,32位加密密鑰 toSave = RijndaelEncrypt(toSave, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ); StreamWriter streamWriter = File.CreateText(fileName); streamWriter.Write(toSave); streamWriter.Close(); } public static object GetData( string fileName,Type pType) { StreamReader streamReader = File.OpenText(fileName); string data = streamReader.ReadToEnd(); //對(duì)數(shù)據(jù)進(jìn)行解密,32位解密密鑰 data = RijndaelDecrypt(data, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ); streamReader.Close(); return DeserializeObject(data,pType); } /// <summary> /// Rijndael加密算法 /// </summary> /// <param name="pString">待加密的明文</param> /// <param name="pKey">密鑰,長(zhǎng)度可以為:64位(byte[8]),128位(byte[16]),192位(byte[24]),256位(byte[32])</param> /// <param name="iv">iv向量,長(zhǎng)度為128(byte[16])</param> /// <returns></returns> private static string RijndaelEncrypt( string pString, string pKey) { //密鑰 byte [] keyArray = UTF8Encoding.UTF8.GetBytes(pKey); //待加密明文數(shù)組 byte [] toEncryptArray = UTF8Encoding.UTF8.GetBytes(pString); //Rijndael解密算法 RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateEncryptor(); //返回加密后的密文 byte [] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); } /// <summary> /// ijndael解密算法 /// </summary> /// <param name="pString">待解密的密文</param> /// <param name="pKey">密鑰,長(zhǎng)度可以為:64位(byte[8]),128位(byte[16]),192位(byte[24]),256位(byte[32])</param> /// <param name="iv">iv向量,長(zhǎng)度為128(byte[16])</param> /// <returns></returns> private static String RijndaelDecrypt( string pString, string pKey) { //解密密鑰 byte [] keyArray = UTF8Encoding.UTF8.GetBytes(pKey); //待解密密文數(shù)組 byte [] toEncryptArray = Convert.FromBase64String(pString); //Rijndael解密算法 RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateDecryptor(); //返回解密后的明文 byte [] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return UTF8Encoding.UTF8.GetString(resultArray); } /// <summary> /// 將一個(gè)對(duì)象序列化為字符串 /// </summary> /// <returns>The object.</returns> /// <param name="pObject">對(duì)象</param> /// <param name="pType">對(duì)象類型</param> private static string SerializeObject( object pObject) { //序列化后的字符串 string serializedString = string .Empty; //使用Json.Net進(jìn)行序列化 serializedString = JsonConvert.SerializeObject(pObject); return serializedString; } /// <summary> /// 將一個(gè)字符串反序列化為對(duì)象 /// </summary> /// <returns>The object.</returns> /// <param name="pString">字符串</param> /// <param name="pType">對(duì)象類型</param> private static object DeserializeObject( string pString,Type pType) { //反序列化后的對(duì)象 object deserializedObject = null ; //使用Json.Net進(jìn)行反序列化 deserializedObject=JsonConvert.DeserializeObject(pString,pType); return deserializedObject; } } |
01 02 03 04 05 06 07 08 09 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 | using UnityEngine; using System.Collections; using System.Collections.Generic; public class TestSave : MonoBehaviour { /// <summary> /// 定義一個(gè)測(cè)試類 /// </summary> public class TestClass { public string Name = "張三" ; public float Age = 23.0f; public int Sex = 1; public List< int > Ints = new List< int > () { 1, 2, 3 }; } void Start () { //定義存檔路徑 string dirpath = Application.persistentDataPath + "/Save" ; //創(chuàng)建存檔文件夾 IOHelper.CreateDirectory (dirpath); //定義存檔文件路徑 string filename = dirpath + "/GameData.sav" ; TestClass t = new TestClass (); //保存數(shù)據(jù) IOHelper.SetData (filename,t); //讀取數(shù)據(jù) TestClass t1 = (TestClass)IOHelper.GetData(filename, typeof (TestClass)); Debug.Log(t1.Name); Debug.Log(t1.Age); Debug.Log(t1.Ints); } } |
聯(lián)系客服