using UnityEngine; using System; using System.Text; using System.IO; public class AsteriskCode : MonoBehaviour { public static bool IsRedirect = false; #region Encrypt & Decrypt //# using ICSharpCode.SharpZipLib.GZip //private const string STR_EMPTY = "EM"; //private static readonly byte[] BYTE_EMPTY = Encoding.UTF8.GetBytes("EM"); //private const string KEY_128 = "dftaLxGMrgDBzRoS"; //private const string IV_128 = "RFGmvTn6UKRWdlok"; //// 암호화. //private static string EncryptAES128(string plain) //{ // try // { // // 바이트로 변환. // byte[] plainBytes = Encoding.UTF8.GetBytes(plain); // // 레인달 알고리즘. // RijndaelManaged rm = new RijndaelManaged(); // // 복호화할 방식과 패딩 방법 일치시킴(AES/CBC/PKCS7). // rm.Mode = CipherMode.CBC; // rm.Padding = PaddingMode.PKCS7; // rm.KeySize = 128; // //rm.BlockSize = 128; // MemoryStream memoryStream = new MemoryStream(); // ICryptoTransform encryptor = rm.CreateEncryptor(Encoding.UTF8.GetBytes(KEY_128), Encoding.UTF8.GetBytes(IV_128)); // // Key, IV 값으로 메모리스트림을 이용해 크립토스트림 생성. // CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write); // // 크립토스트림에 바이트 배열을 쓰고 플러시. // cryptoStream.Write(plainBytes, 0, plainBytes.Length); // cryptoStream.FlushFinalBlock(); // // 메모리스트림에 담겨있는 암호화된 바이트배열을 담음. // byte[] encryptBytes = memoryStream.ToArray(); // // base64로 변환. // string encryptString = Convert.ToBase64String(encryptBytes); // cryptoStream.Close(); // memoryStream.Close(); // return encryptString; // } // catch (Exception e) // { // Debug.LogWarning("EncryptAES128: " + e.StackTrace); // return null; // } //} //// 암호화. //private static byte[] EncryptAES128Byte(string plain) //{ // try // { // // 바이트로 변환. // byte[] plainBytes = Encoding.UTF8.GetBytes(plain); // // 레인달 알고리즘. // RijndaelManaged rm = new RijndaelManaged(); // // 복호화할 방식과 패딩 방법 일치시킴(AES/CBC/PKCS7). // rm.Mode = CipherMode.CBC; // rm.Padding = PaddingMode.PKCS7; // rm.KeySize = 128; // //rm.BlockSize = 128; // MemoryStream memoryStream = new MemoryStream(); // ICryptoTransform encryptor = rm.CreateEncryptor(Encoding.UTF8.GetBytes(KEY_128), Encoding.UTF8.GetBytes(IV_128)); // // Key, IV 값으로 메모리스트림을 이용해 크립토스트림 생성. // CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write); // // 크립토스트림에 바이트 배열을 쓰고 플러시. // cryptoStream.Write(plainBytes, 0, plainBytes.Length); // cryptoStream.FlushFinalBlock(); // // 메모리스트림에 담겨있는 암호화된 바이트배열을 담음. // byte[] encryptBytes = memoryStream.ToArray(); // cryptoStream.Close(); // memoryStream.Close(); // return encryptBytes; // } // catch (Exception e) // { // Debug.LogWarning("EncryptAES128: " + e.StackTrace); // return null; // } //} //// 복호화. //private static string DecryptAES128(string encrypt) //{ // try // { // // base64를 바이트로 변환. // byte[] enctyptBytes = Convert.FromBase64String(encrypt); // // 레인달 알고리즘. // RijndaelManaged rm = new RijndaelManaged(); // // 암호화한 방식과 패딩 방법 일치시킴(AES/CBC/PKCS7). // rm.Mode = CipherMode.CBC; // rm.Padding = PaddingMode.PKCS7; // rm.KeySize = 128; // //rm.BlockSize = 128; // MemoryStream memoryStream = new MemoryStream(enctyptBytes); // ICryptoTransform decryptor = rm.CreateDecryptor(Encoding.UTF8.GetBytes(KEY_128), Encoding.UTF8.GetBytes(IV_128)); // // Key, IV 값으로 메모리스트림을 이용해 크립토스트림 생성. // CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read); // // 복호화된 데이터를 담을 바이트 배열 선언. // byte[] plainBytes = new byte[enctyptBytes.Length]; // int plainCount = cryptoStream.Read(plainBytes, 0, plainBytes.Length); // // 복호화된 바이트 배열을 string으로 변환. // string plainString = Encoding.UTF8.GetString(plainBytes, 0, plainCount); // cryptoStream.Close(); // memoryStream.Close(); // return plainString; // } // catch (Exception e) // { // Debug.LogWarning("DecryptAES128: " + e.StackTrace); // return null; // } //} //// 복호화. //private static byte[] DecryptAES128Byte(string encrypt) //{ // try // { // // base64를 바이트로 변환. // byte[] enctyptBytes = Convert.FromBase64String(encrypt); // // 레인달 알고리즘. // RijndaelManaged rm = new RijndaelManaged(); // // 암호화한 방식과 패딩 방법 일치시킴(AES/CBC/PKCS7). // rm.Mode = CipherMode.CBC; // rm.Padding = PaddingMode.PKCS7; // rm.KeySize = 128; // //rm.BlockSize = 128; // MemoryStream memoryStream = new MemoryStream(enctyptBytes); // ICryptoTransform decryptor = rm.CreateDecryptor(Encoding.UTF8.GetBytes(KEY_128), Encoding.UTF8.GetBytes(IV_128)); // // Key, IV 값으로 메모리스트림을 이용해 크립토스트림 생성. // CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read); // // 복호화된 데이터를 담을 바이트 배열 선언. // byte[] plainBytes = new byte[enctyptBytes.Length]; // int plainCount = cryptoStream.Read(plainBytes, 0, plainBytes.Length); // cryptoStream.Close(); // memoryStream.Close(); // return plainBytes; // } // catch (Exception e) // { // Debug.LogWarning("DecryptAES128: " + e.StackTrace); // return null; // } //} //// 서버 전송용 변환. //public static byte[] EncryptJson(string jsontext) //{ // try // { // if (string.IsNullOrEmpty(jsontext)) // return BYTE_EMPTY; // string strbody = EncryptAES128(jsontext); // if (string.IsNullOrEmpty(strbody)) // return BYTE_EMPTY; // // 암호화한 문자열을 body에 넣어 json 변환. // strbody = JsonUtility.ToJson(new jBody(strbody)); // if (string.IsNullOrEmpty(strbody)) // return BYTE_EMPTY; // return Encoding.UTF8.GetBytes(strbody); // } // catch (Exception e) // { // Debug.LogWarning("EncryptJ: " + e.StackTrace); // return BYTE_EMPTY; // } //} //// 서버 응답 변환. //public static string DecryptJson(string jsontext) //{ // try // { // if (string.IsNullOrEmpty(jsontext)) // return STR_EMPTY; // // 복호화할 문자열을 body형식 json에서 변환. // string strbody = JsonUtility.FromJson(jsontext).body; // if (string.IsNullOrEmpty(strbody)) // return STR_EMPTY; // strbody = DecryptAES128(strbody); // if (string.IsNullOrEmpty(strbody)) // return STR_EMPTY; // return strbody; // } // catch (Exception e) // { // Debug.LogWarning("DecryptJ: " + e.StackTrace); // return STR_EMPTY; // } //} //// 서버 전송용 변환. //public static byte[] CompEncryptJson(object obj) //{ // string jsontext = JsonUtility.ToJson(obj); // return BYTE_EMPTY; //} #endregion Encrypt & Decrypt #region Compress & Decompress //# using ICSharpCode.SharpZipLib.GZip //public static string Decompress(byte[] contents) //{ // using (var memoryStream = new MemoryStream()) // { // Decompress(memoryStream, contents); // var bytes = memoryStream.ToArray(); // return Encoding.UTF8.GetString(bytes); // } //} //private static void Decompress(Stream stream, byte[] compressedData) //{ // var buffer = new byte[4096]; // using (var memoryStream = new MemoryStream(compressedData)) // using (var gzipOutputStream = new GZipInputStream(memoryStream)) // { // for (int r = -1; r != 0; r = gzipOutputStream.Read(buffer, 0, buffer.Length)) // { // if (r > 0) // { // stream.Write(buffer, 0, r); // } // } // } //} #endregion public static object DecodeJson(string contents, Type type) { return JsonUtility.FromJson(contents, type); //#if VER_DEV // return JsonUtility.FromJson(contents, type); //#else // if (IsRedirect) // return JsonUtility.FromJson(contents, type); // try // { // if (string.IsNullOrEmpty(contents)) // return null; // // 복호화할 문자열을 body형식 json에서 변환. // string strbody = JsonUtility.FromJson(contents).body; // if (string.IsNullOrEmpty(strbody)) // return null; // byte[] bytes = DecryptAES128Byte(strbody); // if (bytes == null) // return null; // contents = Decompress(bytes); // } // catch (Exception e) // { // Debug.LogWarning("DecryptJ: " + e.StackTrace); // return null; // } // return JsonUtility.FromJson(contents, type); //#endif } public static T DecodeJson(string contents) { return JsonUtility.FromJson(contents); //#if VER_DEV // return JsonUtility.FromJson(contents); //#else // if (IsRedirect) // return JsonUtility.FromJson(contents); // try // { // if (string.IsNullOrEmpty(contents)) // return default(T); // value 타입일 경우 0, reference 타입일 경우 null 리턴. // // 복호화할 문자열을 body형식 json에서 변환. // string strbody = JsonUtility.FromJson(contents).body; // if (string.IsNullOrEmpty(strbody)) // return default(T); // byte[] bytes = DecryptAES128Byte(strbody); // if (bytes == null) // return default(T); // contents = Decompress(bytes); // } // catch (Exception e) // { // Debug.LogWarning("DecryptJ: " + e.StackTrace); // return default(T); // } // return JsonUtility.FromJson(contents); //#endif } public static byte[] EncodeJson(object obj) { string jsontext = JsonUtility.ToJson(obj); return Encoding.UTF8.GetBytes(jsontext); //#if VER_DEV // return Encoding.UTF8.GetBytes(jsontext); //#else // if (IsRedirect) // return Encoding.UTF8.GetBytes(jsontext); // return EncryptJson(jsontext); //#endif } public static byte[] EncodeString(string jsontext) { return Encoding.UTF8.GetBytes(jsontext); //#if VER_DEV // return Encoding.UTF8.GetBytes(jsontext); //#else // if (IsRedirect) // return Encoding.UTF8.GetBytes(jsontext); // return EncryptJson(jsontext); //#endif } }