Esempio n. 1
0
        /// <summary>
        /// Encrypts a strings passed in using Electronic Code Book Cipher
        /// </summary>
        /// <param name="stringToEncrypt">String to encrypt</param>
        /// <param name="password">password needed to unlock encrypted string</param>
        /// <returns>A new string that must have the same password 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 password.
            string authorizationCode = "worldclass";

            try
            {
                // Verify String Does Exist and is not null
                if (!String.IsNullOrEmpty(stringToEncrypt))
                {
                    // encrypted system password here
                    string systemPassword = "******";

                    // now decrypt password
                    string password = CryptographyManager.DecryptString(systemPassword, authorizationCode);

                    // create MD5 Service
                    hashmd5 = new MD5CryptoServiceProvider();

                    // compute password has
                    pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(password));

                    // dispose of hashmd5
                    hashmd5 = null;

                    // implement DES3 encryption
                    des = new TripleDESCryptoServiceProvider();

                    // the key is the secret password 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);
        }
Esempio n. 2
0
        /// <summary>
        /// Decrypts a string passed in.
        /// </summary>
        /// <param name="stringToDecrypt">String that needs to be deciphered.</param>
        /// <param name="Password">Code to unlock this password.</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 password.
            string authorizationCode = "worldclass";

            try
            {
                // encrypted system password here
                string systemPassword = "******";

                // now decrypt password
                string password = CryptographyManager.DecryptString(systemPassword, authorizationCode);

                // Create MD5 CryptoServiceProvider
                hashmd5 = new MD5CryptoServiceProvider();

                // computer password hash
                pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(password));

                // dispose of object
                hashmd5 = null;

                //implement DES3 encryption
                des = new TripleDESCryptoServiceProvider();

                //the key is the secret password 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 = CryptographyManager.DecryptString(temp, encryptionKey);
                        }
                        else
                        {
                            // use the default encryption key
                            settingValue = CryptographyManager.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);
        }