Exemplo n.º 1
0
        /// <summary>
        /// Decrypt the specified string using the <see cref="System.Security.Cryptography.TripleDESCryptoServiceProvider" /> cryptographic
        /// service provider. It is expected that <see cref="Business.Interfaces.IAppSetting.EncryptionKey" /> is used for the encryption key.
        /// </summary>
        /// <param name="encryptedText">A string to be decrypted. The encrypted string should have been encrypted using the
        /// <see cref="Encrypt" /> function in this class. If the value is null or empty, the return value is equal to String.Empty.</param>
        /// <param name="encryptionKey">The encryption key.</param>
        /// <returns>Returns the original, unencrypted string contained in the <paramref name="encryptedText" /> parameter.</returns>
        /// <exception cref="System.FormatException">Thrown when the text cannot be decrypted.</exception>
        public static string Decrypt(string encryptedText, string encryptionKey)
        {
            if (String.IsNullOrEmpty(encryptedText))
            {
                return(String.Empty);
            }

            // Get the byte code of the string
            byte[] toEncryptArray = Convert.FromBase64String(encryptedText);

            using (var tdes = new System.Security.Cryptography.TripleDESCryptoServiceProvider())
            {
                // Set the secret key for the tripleDES algorithm.
                tdes.Key = System.Text.Encoding.UTF8.GetBytes(encryptionKey);

                // Mode of operation. there are other 4 modes. We choose ECB(Electronic code Book)
                tdes.Mode = System.Security.Cryptography.CipherMode.ECB;

                // Padding mode(if any extra byte added)
                tdes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

                var    cTransform  = tdes.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

                // Release resources held by TripleDes Encryptor
                tdes.Clear();

                // Return the Clear decrypted TEXT
                return(System.Text.Encoding.UTF8.GetString(resultArray));
            }
        }
Exemplo n.º 2
0
        ///// <summary>
        ///// Gets the connection string settings for the connection string associated with the gallery data.
        ///// </summary>
        ///// <returns>An instance of <see cref="System.Configuration.ConnectionStringSettings" />.</returns>
        //public static System.Configuration.ConnectionStringSettings GetConnectionStringSettings()
        //{
        //  return System.Configuration.ConfigurationManager.ConnectionStrings.Cast<System.Configuration.ConnectionStringSettings>().First(cnStringObj => cnStringObj.Name == GetConnectionStringName());
        //}

        ///// <summary>
        ///// Gets the name of the connection string for the gallery data.
        ///// </summary>
        ///// <returns>System.String.</returns>
        //private static string GetConnectionStringName()
        //{
        //  using (var repo = new GalleryRepository())
        //  {
        //    return repo.ConnectionStringName;
        //  }
        //}


        /// <summary>
        /// Encrypt the specified string using the <see cref="System.Security.Cryptography.TripleDESCryptoServiceProvider" /> cryptographic
        /// service provider. It is expected that <see cref="Business.Interfaces.IAppSetting.EncryptionKey" /> is used for the encryption key.
        /// The encrypted string can be decrypted to its original string using the <see cref="Decrypt" /> function in this class.
        /// </summary>
        /// <param name="plainText">A plain text string to be encrypted. If the value is null or empty, the return value is
        /// equal to String.Empty.</param>
        /// <param name="encryptionKey">The encryption key.</param>
        /// <returns>Returns an encrypted version of the <paramref name="plainText" /> parameter.</returns>
        public static string Encrypt(string plainText, string encryptionKey)
        {
            if (String.IsNullOrEmpty(plainText))
            {
                return(String.Empty);
            }

            // This method (and the Decrypt method) inspired from Code Project.
            // http://www.codeproject.com/useritems/Cryptography.asp
            byte[] stringToEncryptArray = System.Text.Encoding.UTF8.GetBytes(plainText);

            using (var tdes = new System.Security.Cryptography.TripleDESCryptoServiceProvider())
            {
                // Set the secret key for the tripleDES algorithm
                tdes.Key = System.Text.Encoding.UTF8.GetBytes(encryptionKey);

                // Mode of operation. there are other 4 modes. We choose ECB (Electronic code Book)
                tdes.Mode = System.Security.Cryptography.CipherMode.ECB;

                //padding mode(if any extra byte added)
                tdes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

                // Transform the specified region of bytes array to resultArray
                var    cTransform  = tdes.CreateEncryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(stringToEncryptArray, 0, stringToEncryptArray.Length);

                // Release resources held by TripleDes Encryptor
                tdes.Clear();

                // Return the encrypted data into unreadable string format
                return(Convert.ToBase64String(resultArray));
            }
        }
Exemplo n.º 3
0
        public static string Descriptografar(string Message)
        {
            byte[] Results;

            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();

            System.Security.Cryptography.MD5CryptoServiceProvider HashProvider = new System.Security.Cryptography.MD5CryptoServiceProvider();

            byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(senha));

            System.Security.Cryptography.TripleDESCryptoServiceProvider TDESAlgorithm = new System.Security.Cryptography.TripleDESCryptoServiceProvider();

            TDESAlgorithm.Key = TDESKey;

            TDESAlgorithm.Mode = System.Security.Cryptography.CipherMode.ECB;

            TDESAlgorithm.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

            byte[] DataToDecrypt = Convert.FromBase64String(Message);

            try
            {
                System.Security.Cryptography.ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();

                Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
            }
            finally
            {
                TDESAlgorithm.Clear();
                HashProvider.Clear();
            }
            return(UTF8.GetString(Results));
        }
Exemplo n.º 4
0
        /// <summary>
        /// DeCrypt a string using dual encryption method. Return a DeCrypted clear string
        /// </summary>
        /// <param name="cipherString">encrypted string</param>
        /// <param name="useHashing">Did you use hashing to encrypt this data? pass true is yes</param>
        /// <returns></returns>
        public static string Decrypt(string cipherString, bool useHashing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = Convert.FromBase64String(cipherString);

            //System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
            //Get your key from config file to open the lock!
            //string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));

            if (useHashing)
            {
                System.Security.Cryptography.MD5CryptoServiceProvider hashmd5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                hashmd5.Clear();
            }
            else
            {
                keyArray = UTF8Encoding.UTF8.GetBytes(key);
            }

            System.Security.Cryptography.TripleDESCryptoServiceProvider tdes = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
            tdes.Key     = keyArray;
            tdes.Mode    = System.Security.Cryptography.CipherMode.ECB;
            tdes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

            System.Security.Cryptography.ICryptoTransform cTransform = tdes.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            tdes.Clear();
            return(UTF8Encoding.UTF8.GetString(resultArray));
        }
        private static string EncryptDecryptText(bool isEncode, string text, string encryptionKey)
        {
            // Step 1. Hash the encryptionKey using 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 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            var tdesKey      = hashProvider.ComputeHash(Encoding.Unicode.GetBytes(encryptionKey));

            // Step 2. Create a new TripleDESCryptoServiceProvider object
            var tdesAlgorithm = new System.Security.Cryptography.TripleDESCryptoServiceProvider
            {
                Key  = tdesKey,
                Mode =
                    System.Security
                    .Cryptography
                    .CipherMode.ECB,
                Padding =
                    System.Security
                    .Cryptography
                    .PaddingMode.PKCS7
            };

            // Step 3. Setup the encoder

            // Step 4. Convert the input text to a byte[]
            byte[] dataToEncryptDecrypt = isEncode ? Encoding.Unicode.GetBytes(text) : Convert.FromBase64String(text);

            // Step 5. Attempt to encrypt/Decrypt the string
            byte[] encryptDecryptedBytes;
            try
            {
                if (isEncode)
                {
                    var encryptor = tdesAlgorithm.CreateEncryptor();
                    encryptDecryptedBytes = encryptor.TransformFinalBlock(dataToEncryptDecrypt, 0,
                                                                          dataToEncryptDecrypt.Length);
                }
                else
                {
                    var decryptor = tdesAlgorithm.CreateDecryptor();
                    encryptDecryptedBytes = decryptor.TransformFinalBlock(dataToEncryptDecrypt, 0,
                                                                          dataToEncryptDecrypt.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(isEncode ? Convert.ToBase64String(encryptDecryptedBytes) : Encoding.Unicode.GetString(encryptDecryptedBytes));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Encrypts a string using a specified security key with
        /// the option to hash.
        /// </summary>
        /// <param name="toEncrypt">String to encrypt</param>
        /// <param name="securityKey">The key to apply to the encryption</param>
        /// <param name="useHashing">Weather hashing is used</param>
        /// <returns>The encrpyted string</returns>
        private static string Encrypt(string toEncrypt, string securityKey, bool useHashing)
        {
            string retVal = string.Empty;

            try
            {
                byte[] keyArray;
                byte[] toEncryptArray = System.Text.UTF8Encoding.UTF8.GetBytes(toEncrypt);
                // Validate inputs
                ValidateInput(toEncrypt);
                ValidateInput(securityKey);
                // If hashing use get hashcode regards to your key
                if (useHashing)
                {
                    System.Security.Cryptography.MD5CryptoServiceProvider hashmd5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                    keyArray = hashmd5.ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(securityKey));
                    // Always release the resources and flush data
                    // of the Cryptographic service provide. Best Practice
                    hashmd5.Clear();
                }
                else
                {
                    keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(securityKey);
                }
                System.Security.Cryptography.TripleDESCryptoServiceProvider tdes = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
                // Set the secret key for the tripleDES algorithm
                tdes.Key = keyArray;
                // Mode of operation. there are other 4 modes.
                // We choose ECB (Electronic code Book)
                tdes.Mode = System.Security.Cryptography.CipherMode.ECB;
                // Padding mode (if any extra byte added)
                tdes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
                System.Security.Cryptography.ICryptoTransform cTransform = tdes.CreateEncryptor();
                // Transform the specified region of bytes array to resultArray
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                // Release resources held by TripleDes Encryptor
                tdes.Clear();
                // Return the encrypted data into unreadable string format
                retVal = Convert.ToBase64String(resultArray, 0, resultArray.Length);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(retVal);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Decrypts a specified key against the original security
        /// key, with the option to hash.
        /// </summary>
        /// <param name="cipherString">String to decrypt</param>
        /// <param name="securityKey">The original security key</param>
        /// <param name="useHashing">Weather hashing is enabled</param>
        /// <returns>The decrypted key</returns>
        private static string Decrypt(string cipherString, string securityKey, bool useHashing)
        {
            string retVal = string.Empty;

            try
            {
                byte[] keyArray;
                byte[] toEncryptArray = Convert.FromBase64String(cipherString);
                // Validate inputs
                ValidateInput(cipherString);
                ValidateInput(securityKey);
                if (useHashing)
                {
                    // If hashing was used get the hash code with regards to your key
                    System.Security.Cryptography.MD5CryptoServiceProvider hashmd5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                    keyArray = hashmd5.ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(securityKey));
                    // Release any resource held by the MD5CryptoServiceProvider
                    hashmd5.Clear();
                }
                else
                {
                    // If hashing was not implemented get the byte code of the key
                    keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(securityKey);
                }
                System.Security.Cryptography.TripleDESCryptoServiceProvider tdes = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
                // Set the secret key for the tripleDES algorithm
                tdes.Key = keyArray;
                // Mode of operation. there are other 4 modes.
                // We choose ECB(Electronic code Book)
                tdes.Mode = System.Security.Cryptography.CipherMode.ECB;
                // Padding mode(if any extra byte added)
                tdes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
                System.Security.Cryptography.ICryptoTransform cTransform = tdes.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                // Release resources held by TripleDes Encryptor
                tdes.Clear();
                // Return the Clear decrypted TEXT
                retVal = System.Text.UTF8Encoding.UTF8.GetString(resultArray);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(retVal);
        }
Exemplo n.º 8
0
        private static string Decrypt(string cipherString)
        {
            try
            {
                byte[] keyArray;
                byte[] toEncryptArray = Convert.FromBase64String(cipherString);

                //Di default disabilito l'hashing
                bool useHashing = false;

                //La chiave deve essere di 24 caratteri
                string key = "ValueTeamDocsPa3Services";

                if (useHashing)
                {
                    System.Security.Cryptography.MD5CryptoServiceProvider hashmd5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                    keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                    hashmd5.Clear();
                }
                else
                {
                    keyArray = UTF8Encoding.UTF8.GetBytes(key);
                }

                System.Security.Cryptography.TripleDESCryptoServiceProvider tdes = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
                tdes.Key     = keyArray;
                tdes.Mode    = System.Security.Cryptography.CipherMode.ECB;
                tdes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

                System.Security.Cryptography.ICryptoTransform cTransform = tdes.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

                tdes.Clear();
                return(UTF8Encoding.UTF8.GetString(resultArray));
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// Encrypt the specified string using the System.Security.Cryptography.TripleDESCryptoServiceProvider cryptographic
        /// service provider. The secret key used in the encryption is specified in the encryptionKey configuration setting.
        /// The encrypted string can be decrypted to its original string using the Decrypt function in this class.
        /// </summary>
        /// <param name="plainText">A plain text string to be encrypted. If the value is null or empty, the return value is
        /// equal to String.Empty.</param>
        /// <returns>Returns an encrypted version of the plainText parameter.</returns>
        public static string Encrypt(string plainText)
        {
            if (String.IsNullOrEmpty(plainText))
                return String.Empty;

            // This method (and the Decrypt method) inspired from Code Project.
            // http://www.codeproject.com/useritems/Cryptography.asp
            byte[] stringToEncryptArray = System.Text.Encoding.UTF8.GetBytes(plainText);

            using (System.Security.Cryptography.TripleDESCryptoServiceProvider tdes = new System.Security.Cryptography.TripleDESCryptoServiceProvider())
            {
                // Set the secret key for the tripleDES algorithm
                byte[] keyArray = EncryptionKey;
                tdes.Key = keyArray;

                // Mode of operation. there are other 4 modes. We choose ECB (Electronic code Book)
                tdes.Mode = System.Security.Cryptography.CipherMode.ECB;

                //padding mode(if any extra byte added)
                tdes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

                // Transform the specified region of bytes array to resultArray
                System.Security.Cryptography.ICryptoTransform cTransform = tdes.CreateEncryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(stringToEncryptArray, 0, stringToEncryptArray.Length);

                // Release resources held by TripleDes Encryptor
                tdes.Clear();

                // Return the encrypted data into unreadable string format
                return Convert.ToBase64String(resultArray);
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Decrypt the specified string using the System.Security.Cryptography.TripleDESCryptoServiceProvider cryptographic
        /// service provider. The secret key used in the decryption is specified in the encryptionKey configuration setting.
        /// </summary>
        /// <param name="encryptedText">A string to be decrypted. The encrypted string should have been encrypted using the
        /// Encrypt function in this class. If the value is null or empty, the return value is equal to String.Empty.</param>
        /// <returns>
        /// Returns the original, unencrypted string contained in the encryptedText parameter.
        /// </returns>
        /// <exception cref="System.FormatException">Thrown when the text cannot be decrypted.</exception>
        public static string Decrypt(string encryptedText)
        {
            if (String.IsNullOrEmpty(encryptedText))
                return String.Empty;

            // Get the byte code of the string
            byte[] toEncryptArray = Convert.FromBase64String(encryptedText);

            using (System.Security.Cryptography.TripleDESCryptoServiceProvider tdes = new System.Security.Cryptography.TripleDESCryptoServiceProvider())
            {
                // Set the secret key for the tripleDES algorithm.
                tdes.Key = EncryptionKey;

                // Mode of operation. there are other 4 modes. We choose ECB(Electronic code Book)
                tdes.Mode = System.Security.Cryptography.CipherMode.ECB;

                // Padding mode(if any extra byte added)
                tdes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

                System.Security.Cryptography.ICryptoTransform cTransform = tdes.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

                // Release resources held by TripleDes Encryptor
                tdes.Clear();

                // Return the Clear decrypted TEXT
                return System.Text.Encoding.UTF8.GetString(resultArray);
            }
        }