using System; using System.IO.Compression; using System.IO; using System.Security.Cryptography; using System.Text; public class Crypto { 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 text) { MemoryStream resultStream = new MemoryStream(); byte[] buffer = Convert.FromBase64String(text); 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) { //Åë½Å ¾Ïȣȭ #region To Base64 byte[] b = Encoding.UTF8.GetBytes(encrypt); encrypt = Convert.ToBase64String(b); #endregion #region Áß°£¿¡ °ª ³Ö±â Random rand = new Random(); encrypt = encrypt.Insert(0, str[rand.Next(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) { 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 Áß°£¿¡ °ª ³Ö±â decrypt = decrypt.Substring(1); #endregion #region To Base64 byte[] b = Convert.FromBase64String(decrypt); decrypt = Encoding.UTF8.GetString(b); #endregion return decrypt; } private const string strPassword = "sldkfghqpwo!)($%+sr=g234"; private const string vi = "f9v#d9OV*1Unf*%v"; // 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(vi); byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText); PasswordDeriveBytes password = new PasswordDeriveBytes(strPassword, 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(vi); byte[] cipherTextBytes = Convert.FromBase64String(cipherText); PasswordDeriveBytes password = new PasswordDeriveBytes(strPassword, 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(vi); 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(vi); 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) { 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 }