using System;using System.Text;using System.Security.Cryptography;using System.IO;public class DataEncryptDecrypt{public static string encryData(string filepath){byte[] keyArray = UTF8Encoding.UTF8.GetBytes("AASWAwQEFgcIBAoDDA0ODw==");byte[] toEmcryptArray = File.ReadAllBytes(filepath);RijndaelManaged rDel = new RijndaelManaged();rDel.Key = keyArray;rDel.Mode = CipherMode.ECB;ICryptoTransform cTransform = rDel.CreateEncryptor();byte[] resultArray = cTransform.TransformFinalBlock(toEmcryptArray, 0, toEmcryptArray.Length);return Convert.ToBase64String(resultArray, 0, resultArray.Length);}public static string Decrypt(string toDecrypt){byte[] keyArray = UTF8Encoding.UTF8.GetBytes("AASWAwQEFgcIBAoDDA0ODw==");byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);RijndaelManaged rDel = new RijndaelManaged();rDel.Key = keyArray;rDel.Mode = CipherMode.ECB;rDel.Padding = PaddingMode.None;ICryptoTransform cTransform = rDel.CreateDecryptor();byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);return UTF8Encoding.UTF8.GetString(resultArray);}}
----------------------
//기본 사용법 JSON이용public static void obj_encrypt_Save(object obj, string filePath, string fileName){string json = ObjectToJson(obj);string enc = DataEncryptDecrypt.encrystringData(json);CreateJsonFile(filePath, fileName, enc);}//text를 불러와 복호화 후 오브젝트에 Deserailizepublic static void decrypt_load<T>(T obj, string En_filePath, string fileName){FileStream filestream = new FileStream(string.Format("{0}/{1}.json", En_filePath, fileName), FileMode.Open);byte[] data = new byte[filestream.Length];filestream.Read(data, 0, data.Length);filestream.Close();string jsonData = Encoding.UTF8.GetString(data);string de_data = DataEncryptDecrypt.Decrypt(jsonData);JsonUtility.FromJsonOverwrite(de_data, obj);}
댓글 없음:
댓글 쓰기