암호화 복호화 저장
DataEncryptDecrypt Class
https://gist.github.com/DongguemYoo/2546a2067dc065297765c5b7d55b05cf
using UnityEngine; using System; using System.IO; using System.Text; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Serialization; public class Data { public Inner iNner; public Inner ind; } public class Inner { public int A; public int B; } public class Test : MonoBehaviour { // Use this for initialization void Start () { Data testData = new Data(); Inner inner = new Inner(); inner.A = 10; inner.B = 22; testData.iNner = inner; //오브젝트 암호화 저장 //obj_encrypt_Save(testData, Application.dataPath + "/En/En_tst22.txt"); //암호화 저장된 데이터 오브젝트에 Deserialize!! //decrypt_load(testData, Application.dataPath + "/En/En_tst22.txt"); //복호화가 잘 되었는지 파일로 내려서 확인 //private_Check(testData); } //오브젝트를 암호화 하여 text로 저장 public void obj_encrypt_Save(object obj,string filePath) { string enc = DataEncryptDecrypt.encrystringData(SerializeToXml(obj)); File.WriteAllText(filePath,enc, Encoding.Default); } //text를 불러와 복호화 후 오브젝트에 Deserailize public void decrypt_load<T>(T obj,string En_filePath) { string read = File.ReadAllText(En_filePath); string de_data = DataEncryptDecrypt.Decrypt(read); obj = Serializer.DeserializeObject<T>(de_data); } //복호화가 잘 되었는지 확인용 private void private_Check<T>(T obj) { Stream stream = new FileStream(Application.dataPath + "/복호화확인.xml", FileMode.CreateNew); XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); xmlSerializer.Serialize(stream, obj); } public string SerializeToXml(object input) { XmlSerializer ser = new XmlSerializer(input.GetType()); string result = string.Empty; using (MemoryStream memStm = new MemoryStream()) { ser.Serialize(memStm, input); memStm.Position = 0; result = new StreamReader(memStm).ReadToEnd(); } return result; } } public static class Serializer { public static string SerializeObject(object obj) { MemoryStream memoryStream = new MemoryStream(); XmlSerializer xs = new XmlSerializer(obj.GetType()); XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); xs.Serialize(xmlTextWriter, obj); memoryStream = (MemoryStream)xmlTextWriter.BaseStream; return UTF8ByteArrayToString(memoryStream.ToArray()); } public static T DeserializeObject<T>(string objString) { object obj = null; XmlSerializer xs = new XmlSerializer(typeof(T)); using (MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(objString))) { XmlTextReader xtr = new XmlTextReader(memoryStream); obj = xs.Deserialize(xtr); } return (T)obj; } private static string UTF8ByteArrayToString(byte[] characters) { UTF8Encoding encoding = new UTF8Encoding(); return encoding.GetString(characters); } private static byte[] StringToUTF8ByteArray(string xmlString) { UTF8Encoding encoding = new UTF8Encoding(); return encoding.GetBytes(xmlString); } }
댓글 없음:
댓글 쓰기