Labels

Thursday, June 25, 2015

Decrypting the Encrypted Connection Strings

var key = Dts.Variables["SymmetricKey"].Value.ToString();
                var keyIV = Dts.Variables["SymmetricKeyIV"].Value.ToString();              

                // DB Decryption
                var encryptedDBConString = Dts.Variables["EncryptedDBConString"].Value.ToString();
                var decryptedDBConString = (new SecurityUtility(key, keyIV)).Decrypt(encryptedDBConString);
                Dts.Variables["DecryptedDBConString"].Value = decryptedDBConString;
                using (var Conn = new OleDbConnection(decryptedDBConString))
                {
                    Conn.Open();
                }

    /// <summary>
    /// This class provides helper methods for security E.G. encryption/decryption.
    /// </summary>
    internal class SecurityUtility
    {
        #region - Properties and Fields -
        /// <summary>
        /// Gets the Byte Array for the Key
        /// </summary>
        private byte[] KeyArray
        {
            get
            {
                return Convert.FromBase64String(_symmetricKey);
            }
        }

        /// <summary>
        /// Gets the Byte Array for the Key IV
        /// </summary>
        private byte[] KeyIVArray
        {
            get
            {
                return Convert.FromBase64String(_symmetricKeyIV);
            }
        }

        private string _symmetricKey;

        private string _symmetricKeyIV;
        #endregion

        #region - Methods -
        internal SecurityUtility(string symmetricKey, string symmetricKeyIV)
        {
            _symmetricKey = symmetricKey;
            _symmetricKeyIV = symmetricKeyIV;
        }

        /// <summary>
        /// Encrypts the specified plain data.
        /// </summary>
        /// <param name="plainData">The plain data.</param>
        /// <returns>The encrypted data.</returns>
        internal string Encrypt(string plainData)
        {
            // Convert the passed string to a byte array.
            byte[] toEncrypt = new ASCIIEncoding().GetBytes(plainData);
            byte[] inArray = Encrypt(toEncrypt);
            return inArray == null ? string.Empty : Convert.ToBase64String(inArray);
        }

        /// <summary>
        /// Decrypts the specified encrypted data.
        /// </summary>
        /// <param name="encryptedData">The encrypted data.</param>
        /// <returns>The decrypted data.</returns>
        internal string Decrypt(string encryptedData)
        {
            byte[] toDecrypt = Convert.FromBase64String(encryptedData);
            byte[] bytes = Decrypt(toDecrypt);
            return bytes == null ? string.Empty : Encoding.ASCII.GetString(bytes);
        }

        /// <summary>
        /// Encrypts the specified data.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns>The encrypted data.</returns>
        private byte[] Encrypt(byte[] data)
        {
            using (MemoryStream ms = new MemoryStream(data.Length))
            {
                using (var tripleDES = new TripleDESCryptoServiceProvider())
                {
                    using (CryptoStream cs = new CryptoStream(ms, tripleDES.CreateEncryptor(KeyArray, KeyIVArray), CryptoStreamMode.Write))
                    {
                        cs.Write(data, 0, data.Length);
                        cs.FlushFinalBlock();
                        return ms.ToArray();
                    }
                }
            }
        }

        /// <summary>
        /// Decrypts the specified data.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns>The decrypted data.</returns>
        private byte[] Decrypt(byte[] data)
        {
            using (MemoryStream ms = new MemoryStream(data.Length))
            {
                using (var tripleDES = new TripleDESCryptoServiceProvider())
                {
                    using (CryptoStream cs = new CryptoStream(ms, tripleDES.CreateDecryptor(KeyArray, KeyIVArray), CryptoStreamMode.Read))
                    {
                        ms.Write(data, 0, data.Length);
                        ms.Position = 0L;
                        string s = new StreamReader(cs).ReadToEnd();
                        return Encoding.ASCII.GetBytes(s);
                    }
                }
            }
        }
        #endregion
    }

No comments:

Post a Comment