using System.Security.Cryptography; using System.Text; namespace EgwCoreLib.Utils { /// /// utils x cifrature e Crypto /// public class SteamCrypto { #region Public Methods /// /// decifra un messaggio con una password /// /// /// /// public static string DecryptString(string Message, string Passphrase) { string answ = Message; byte[] Results = new byte[8]; UTF8Encoding UTF8 = new UTF8Encoding(); // Step 1. We hash the passphrase using MD5 We use the MD5 hash generator as the result // is a 128 bit byte array which is a valid length for the TripleDES encoder we use below var HashProvider = MD5.Create(); //MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider(); byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase)); // Step 2. Create a new TripleDESCryptoServiceProvider object var TDESAlgorithm = TripleDES.Create(); //TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider(); // Step 3. Setup the decoder TDESAlgorithm.Key = TDESKey; TDESAlgorithm.Mode = CipherMode.ECB; TDESAlgorithm.Padding = PaddingMode.PKCS7; // Step 4. Convert the input string to a byte[] byte[] DataToDecrypt = new byte[8]; try { DataToDecrypt = Convert.FromBase64String(Message); } catch { } if (DataToDecrypt != null) { // Step 5. Attempt to decrypt the string try { ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor(); Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length); } finally { // Clear the TripleDes and Hashprovider services of any sensitive information TDESAlgorithm.Clear(); HashProvider.Clear(); } // Step 6. Return the decrypted string in UTF8 format answ = UTF8.GetString(Results); } return answ; } /// /// cifra un messaggio con una password /// /// /// /// public static string EncryptString(string Message, string Passphrase) { byte[] Results; UTF8Encoding UTF8 = new UTF8Encoding(); // Step 1. We hash the passphrase using MD5 We use the MD5 hash generator as the result // is a 128 bit byte array which is a valid length for the TripleDES encoder we use below var HashProvider = MD5.Create(); //MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider(); byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase)); // Step 2. Create a new TripleDESCryptoServiceProvider object var TDESAlgorithm = TripleDES.Create(); //TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider(); // Step 3. Setup the encoder TDESAlgorithm.Key = TDESKey; TDESAlgorithm.Mode = CipherMode.ECB; TDESAlgorithm.Padding = PaddingMode.PKCS7; // Step 4. Convert the input string to a byte[] byte[] DataToEncrypt = UTF8.GetBytes(Message); // Step 5. Attempt to encrypt the string try { ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor(); Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length); } finally { // Clear the TripleDes and Hashprovider services of any sensitive information TDESAlgorithm.Clear(); HashProvider.Clear(); } // Step 6. Return the encrypted string as a base64 encoded string return Convert.ToBase64String(Results); } /// /// genera hash di una stringa in MD5 (es x hash gravatar) /// /// /// public static string getHashStringMD5(string Message) { string hash = ""; using (MD5 md5Hash = MD5.Create()) { hash = GetMd5Hash(md5Hash, Message); } return hash; } /// /// Crea un hash MD5 /// /// /// /// public static string GetMd5Hash(MD5 md5Hash, string input) { // Convert the input string to a byte array and compute the hash. byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input)); // Create a new Stringbuilder to collect the bytes and create a string. StringBuilder sBuilder = new StringBuilder(); // Loop through each byte of the hashed data and format each one as a hexadecimal string. for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } // Return the hexadecimal string. return sBuilder.ToString(); } /// /// Generates a random string with a given size /// /// /// /// public static string RandomString(int size, bool lowerCase = false) { var builder = new StringBuilder(size); // Unicode/ASCII Letters are divided into two blocks (Letters 65–90 / 97–122): The first // group containing the uppercase letters and the second group containing the lowercase. // char is a single Unicode character char offset = lowerCase ? 'a' : 'A'; const int lettersOffset = 26; // A...Z or a..z: length=26 for (var i = 0; i < size; i++) { var @char = (char)_random.Next(offset, offset + lettersOffset); builder.Append(@char); } return lowerCase ? builder.ToString().ToLower() : builder.ToString(); } /// /// Verify a hash against a string. /// /// /// /// /// public static bool VerifyMd5Hash(MD5 md5Hash, string input, string hash) { // Hash the input. string hashOfInput = GetMd5Hash(md5Hash, input); // Create a StringComparer an compare the hashes. StringComparer comparer = StringComparer.OrdinalIgnoreCase; if (0 == comparer.Compare(hashOfInput, hash)) { return true; } else { return false; } } #endregion Public Methods #region Private Fields private static readonly Random _random = new Random(); #endregion Private Fields } }