using System; using System.IO.Compression; using System.IO; using System.Security.Cryptography; using System.Text; public class Crypto : SingletonMonoBehaviour { private const string str = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; //¾ÐÃà public string Compress(string text) { byte[] uniBytes = Encoding.Unicode.GetBytes(text); byte[] compressedByte; using (MemoryStream ms = new MemoryStream()) { using (DeflateStream ds = new DeflateStream(ms, CompressionMode.Compress)) { ds.Write(uniBytes, 0, uniBytes.Length); } compressedByte = ms.ToArray(); } return Convert.ToBase64String(compressedByte); } //¾ÐÃàÇØÁ¦ public string Decompress(string t) { MemoryStream resultStream = new MemoryStream(); byte[] buffer = Convert.FromBase64String(t); using (MemoryStream ms = new MemoryStream(buffer)) { using (DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress)) { ds.CopyTo(resultStream); ds.Close(); } } byte[] decompressedByte = resultStream.ToArray(); resultStream.Dispose(); return Encoding.Unicode.GetString(decompressedByte); } //¾Ïȣȭ public string Encrypto(string encrypt) { //#if UNITY_EDITOR // return encrypt; //#endif //Åë½Å ¾Ïȣȭ #region To Base64 byte[] b = Encoding.UTF8.GetBytes(encrypt); encrypt = Convert.ToBase64String(b); #endregion #region Áß°£¿¡ °ª ³Ö±â //for (int i = 0; i < encrypt.Length; i++) //{ // if ((i % 2).Equals(0)) // { // encrypt = encrypt.Insert(i, str[UnityEngine.Random.Range(0, str.Length)].ToString()); // } //} encrypt = encrypt.Insert(0, str[UnityEngine.Random.Range(0, str.Length)].ToString()); #endregion #region ¼ø¼­¹Ù²Ù±â int l1 = (int)(encrypt.Length * 0.3f); int l2 = l1 * 2; int l3 = l1 * 3; string s1 = encrypt.Substring(0, l1); string s2 = encrypt.Substring(l1, l1); string s3 = encrypt.Substring(l2, l1); string s4 = encrypt.Substring(l3, encrypt.Length - l3); encrypt = s4 + s2 + s3 + s1; #endregion encrypt = Encrypt(encrypt); return encrypt; } //º¹È£È­ public string Decrypto(string cipherText) { //#if UNITY_EDITOR // return cipherText; //#endif string decrypt = cipherText; decrypt = Decrypt(decrypt); #region ¼ø¼­¹Ù²Ù±â int length = decrypt.Length; int l1 = (int)(length * 0.3f); int remainder = length - l1 * 3; string s4 = decrypt.Substring(0, remainder); string tmpds = decrypt.Substring(remainder); string s3 = tmpds.Substring(0, l1); string s2 = tmpds.Substring(l1, l1); string s1 = tmpds.Substring(l1 * 2, l1); decrypt = s1 + s3 + s2 + s4; #endregion #region Áß°£¿¡ °ª ³Ö±â //StringBuilder sb = new StringBuilder(); //for (int i = 1; i < length; i += 2) //{ // sb.Append(decrypt[i]); //} //decrypt = sb.ToString(); //Debug.Log(decrypt); decrypt = decrypt.Substring(1); #endregion #region To Base64 byte[] b = Convert.FromBase64String(decrypt); decrypt = Encoding.UTF8.GetString(b); #endregion return decrypt; } private const string vfanjlk32rknjvfsduik = "sldkfghqpwo!)($%+sr=g234"; private const string vbawrnois = "f9v#d9OV*1Unf*%v"; private const string initVector = "dqu83e2g32;v"; private const string passPhrase = "vwui3f876^289)^%"; private const string v = "v3289y3f18793fewq2ljkhn"; private const string p = "er12h93f2njk!*&#@"; private const string iv12g34r = "cn932fr$809j@3r2lnk"; private const string tehtrb231 = "3ft2%uijb3r128^sadljkfsuoi$8y2"; private const string i567ijuyt = "vqnjk1389y!@#*&(T1d"; private const string vetrg54 = "abv3123122tsdg3q2r12@#!@%fhe"; // This constant is used to determine the keysize of the encryption algorithm. private const int keysize = 256; //¾Ïȣȭ public string Encrypt(string plainText) { byte[] initVectorBytes = Encoding.UTF8.GetBytes(vbawrnois); byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText); PasswordDeriveBytes password = new PasswordDeriveBytes(vfanjlk32rknjvfsduik, null); byte[] keyBytes = password.GetBytes(keysize / 8); RijndaelManaged symmetricKey = new RijndaelManaged(); symmetricKey.Mode = CipherMode.CBC; ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes); MemoryStream memoryStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write); cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); cryptoStream.FlushFinalBlock(); byte[] cipherTextBytes = memoryStream.ToArray(); memoryStream.Close(); cryptoStream.Close(); return Convert.ToBase64String(cipherTextBytes); } //º¹È£È­ public string Decrypt(string cipherText) { try { byte[] initVectorBytes = Encoding.ASCII.GetBytes(vbawrnois); byte[] cipherTextBytes = Convert.FromBase64String(cipherText); PasswordDeriveBytes password = new PasswordDeriveBytes(vfanjlk32rknjvfsduik, null); byte[] keyBytes = password.GetBytes(keysize / 8); RijndaelManaged symmetricKey = new RijndaelManaged(); symmetricKey.Mode = CipherMode.CBC; ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes); MemoryStream memoryStream = new MemoryStream(cipherTextBytes); CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read); byte[] plainTextBytes = new byte[cipherTextBytes.Length]; int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length); memoryStream.Close(); cryptoStream.Close(); return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount); } catch { return string.Empty; } } #region HTML ¹®ÀÚ ¿£Æ¼Æ¼ public static string RegularExpressionEncryption(string text) { return text.Replace("\\\"", "\"").Replace("<", "<").Replace(">", ">").Replace("\"", """).Replace("\'", "'").Replace("¢¥", "´").Replace("[", "[").Replace("\\", "&#backslash;").Replace("]", "]").Replace("{", "{").Replace("}", "}").Replace("\n", "&#linebreak;"); } public static string RegularExpressionDecryption(string text) { return text.Replace("<", "<").Replace(">", ">").Replace(""", "\"").Replace("'", "\'").Replace("´", "¢¥").Replace("[", "[").Replace("&#backslash;", "\\").Replace("]", "]").Replace("{", "{").Replace("}", "}").Replace("&#linebreak;", "\n"); } #endregion #region µ¥ÀÌÅÍ ¾Ïȣȭ public string DecryptSchema(string textToDecrypt) { RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7; rijndaelCipher.KeySize = 128; rijndaelCipher.BlockSize = 128; byte[] encryptedData = Convert.FromBase64String(textToDecrypt); byte[] pwdBytes = Encoding.UTF8.GetBytes(vfanjlk32rknjvfsduik); byte[] keyBytes = new byte[16]; int len = pwdBytes.Length; if (len > keyBytes.Length) { len = keyBytes.Length; } Array.Copy(pwdBytes, keyBytes, len); rijndaelCipher.Key = keyBytes; rijndaelCipher.IV = keyBytes; byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length); return Encoding.UTF8.GetString(plainText); } public string EncryptSchema(string textToEncrypt) { RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7; rijndaelCipher.KeySize = 128; rijndaelCipher.BlockSize = 128; byte[] pwdBytes = Encoding.UTF8.GetBytes(vfanjlk32rknjvfsduik); byte[] keyBytes = new byte[16]; int len = pwdBytes.Length; if (len > keyBytes.Length) { len = keyBytes.Length; } Array.Copy(pwdBytes, keyBytes, len); rijndaelCipher.Key = keyBytes; rijndaelCipher.IV = keyBytes; ICryptoTransform transform = rijndaelCipher.CreateEncryptor(); byte[] plainText = Encoding.UTF8.GetBytes(textToEncrypt); return Convert.ToBase64String(transform.TransformFinalBlock(plainText, 0, plainText.Length)); } #endregion #region SHA256 SHA256 sha = new SHA256Managed(); public string SHA256Hash(string data) { //#if UNITY_EDITOR // return data; //#endif byte[] hash = sha.ComputeHash(Encoding.ASCII.GetBytes(data)); StringBuilder stringBuilder = new StringBuilder(); foreach (byte b in hash) { stringBuilder.AppendFormat("{0:x2}", b); } return stringBuilder.ToString(); } #endregion }