/// <summary> /// Encrypts a strings passed in using Electronic Code Book Cipher /// </summary> /// <param customerName="stringToEncrypt">String to encrypt</param> /// <returns>A new string that must have the same productPassword passed in to unlock.</returns> public static string EncryptString(string stringToEncrypt) { // Initial Value string encryptedString = null; // locals TripleDESCryptoServiceProvider des; MD5CryptoServiceProvider hashmd5; byte[] pwdhash; byte[] buff; // authorization code needed to decrypt productPassword. string authorizationCode = "worldclass"; try { // Verify String Does Exist and is not null if (!String.IsNullOrEmpty(stringToEncrypt)) { // encrypted system productPassword here string systemPassword = "******"; // now decrypt productPassword string password = CryptographyHelper.DecryptString(systemPassword, authorizationCode); // create MD5 Service hashmd5 = new MD5CryptoServiceProvider(); // compute productPassword has pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(password)); // dispose of hashmd5 hashmd5 = null; // implement DES3 encryption des = new TripleDESCryptoServiceProvider(); // the key is the secret productPassword hash. des.Key = pwdhash; // Electronic Code Book Cipher (CBC, CFB) des.Mode = CipherMode.ECB; // Set Buffer To stringToEncrypt buff = ASCIIEncoding.ASCII.GetBytes(stringToEncrypt); // Get Encrypted String encryptedString = Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(buff, 0, buff.Length)); } } catch { } // Return Encrypted String return(encryptedString); }
/// <summary> /// Decrypts a string passed in. /// </summary> /// <param customerName="stringToDecrypt">String that needs to be deciphered.</param> /// <returns></returns> public static string DecryptString(string stringToDecrypt) { // initial value string decryptedString = null; // locals TripleDESCryptoServiceProvider des; MD5CryptoServiceProvider hashmd5; byte[] pwdhash; byte[] buff; // authorization code needed to decrypt productPassword. string authorizationCode = "worldclass"; try { // encrypted system productPassword here string systemPassword = "******"; // now decrypt productPassword string password = CryptographyHelper.DecryptString(systemPassword, authorizationCode); // Create MD5 CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider(); // computer productPassword hash pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(password)); // dispose of object hashmd5 = null; //implement DES3 encryption des = new TripleDESCryptoServiceProvider(); //the key is the secret productPassword hash. des.Key = pwdhash; // Electronic Code Book Cipher (CBC, CFB) des.Mode = CipherMode.ECB; // Decrypt String buff = Convert.FromBase64String(stringToDecrypt); //decrypt DES 3 encrypted byte buffer and return ASCII string decryptedString = ASCIIEncoding.ASCII.GetString(des.CreateDecryptor().TransformFinalBlock(buff, 0, buff.Length)); } catch { } // Return Value return(decryptedString); }
/// <summary> /// This method reads and decrypts the values /// in the App.config or Web.config appSettings section. /// </summary> /// <param name="settingName"></param> /// <returns></returns> public static string ReadAppSetting(string settingName, bool dataIsEncrypted = false, string encryptionKey = "") { // initial value string settingValue = null; // local string temp = ""; try { // if the setting exists if (ConfigurationManager.AppSettings[settingName] != null) { // read the setting value from the App.Config or Web.Config temp = ConfigurationManager.AppSettings[settingName].ToString(); } // if the temp string exists if (TextHelper.Exists(temp)) { // if the data is encrypted if (dataIsEncrypted) { // we must decrypt the data if (TextHelper.Exists(encryptionKey)) { // use the default encryption key settingValue = CryptographyHelper.DecryptString(temp, encryptionKey); } else { // use the default encryption key settingValue = CryptographyHelper.DecryptString(temp); } } else { // return the setting value as is settingValue = temp; } } } catch (Exception error) { // for debugging only string err = error.ToString(); } // return value return(settingValue); }