using System; using System.Text; using System.Security.Cryptography; using System.IO; using System.IO.Compression; namespace framework_library { public class encryption { private TripleDESCryptoServiceProvider TripleDes = new TripleDESCryptoServiceProvider(); /// /// constructor /// /// public encryption(string key) { // Initialize the crypto provider. TripleDes.Key = TruncateHash(key, (int)Math.Truncate(Convert.ToDouble(TripleDes.KeySize / 8))); TripleDes.IV = TruncateHash("", (int)Math.Truncate(Convert.ToDouble(TripleDes.BlockSize / 8))); } /// /// string to return an encrypted value /// /// /// public string encryptValue(string textValue) { string result = String.Empty; //encrypt result = EncryptData(textValue); return result; } /// /// string to decrypt data /// /// /// public string decryptData(string encryptedValue) { string result = String.Empty; //decrypt result = DecryptData(encryptedValue); return result; } public string DecodeUrlString(string url) { string newUrl; while ((newUrl = Uri.UnescapeDataString(url)) != url) url = newUrl; return newUrl; } #region private methods /// /// encrypt plan text data to 64 Base String /// /// /// private string EncryptData(string plaintext) { // Convert the plaintext string to a byte array. byte[] plaintextBytes = Zip(plaintext); // Create the stream. System.IO.MemoryStream ms = new System.IO.MemoryStream(); // Create the encoder to write to the stream. CryptoStream encStream = new CryptoStream(ms, TripleDes.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write); // Use the crypto stream to write the byte array to the stream. encStream.Write(plaintextBytes, 0, plaintextBytes.Length); encStream.FlushFinalBlock(); // Convert the encrypted stream to a printable string. return Convert.ToBase64String(ms.ToArray()); } /// /// Decrypt Data /// /// /// private string DecryptData(string encryptedtext) { // Convert the encrypted text string to a byte array. byte[] encryptedBytes = Convert.FromBase64String(encryptedtext); // Create the stream. System.IO.MemoryStream ms = new System.IO.MemoryStream(); // Create the decoder to write to the stream. CryptoStream decStream = new CryptoStream(ms, TripleDes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write); // Use the crypto stream to write the byte array to the stream. decStream.Write(encryptedBytes, 0, encryptedBytes.Length); decStream.FlushFinalBlock(); // Convert the plaintext stream to a string. return Unzip(ms.ToArray()); } /// /// truncate and hash the key /// /// /// /// private Byte[] TruncateHash(string key, int length) { SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider(); // Hash the key. byte[] keyBytes = System.Text.Encoding.Unicode.GetBytes(key); byte[] hash = sha1.ComputeHash(keyBytes); // Truncate or pad the hash. Array.Resize(ref hash, length); return hash; } private static void CopyTo(Stream src, Stream dest) { byte[] bytes = new byte[4096]; int cnt; while ((cnt = src.Read(bytes, 0, bytes.Length)) != 0) { dest.Write(bytes, 0, cnt); } } private static byte[] Zip(string str) { var bytes = Encoding.UTF8.GetBytes(str); using (var msi = new MemoryStream(bytes)) using (var mso = new MemoryStream()) { using (var gs = new GZipStream(mso, CompressionMode.Compress)) { //msi.CopyTo(gs); CopyTo(msi, gs); gs.Dispose(); } return mso.ToArray(); } } private static string Unzip(byte[] bytes) { using (var msi = new MemoryStream(bytes)) using (var mso = new MemoryStream()) { using (var gs = new GZipStream(msi, CompressionMode.Decompress)) { //gs.CopyTo(mso); CopyTo(gs, mso); gs.Dispose(); } return Encoding.UTF8.GetString(mso.ToArray()); } } #endregion } }