Exemplo 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);
        }
Exemplo 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);
        }