Your application probably needs to communicate with a database of some kind. Naturally, that database isn’t open to the world – it needs to be protected and secured. The typical solution to this is to create a username and password combination (ideally, specific to each application or user that requires access) and configure the application with these credentials. In many cases, they’re simply stored in configuration, such as the section of web.config for an ASP.NET application. By default, such settings are stored in plaintext, and are checked into source control, which can have disastrous consequences (note: if you use GitHub and accidentally expose a secret publicly, you need to change it. Just deleting it isn’t enough). There are many different kinds of secrets an application might require, from database connection strings to API keys.
Including connection strings in the code is not a very good practice as your code can be de-compiled and it will be more prone to hijacking the website as well as database server.
To protect this, a good practice would be to encrypt the connection string and decrypt it while accessing the connection string in the code.
Use below code to encrypt and decrypt connection strings using key and hash.
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace EncodingDecodingMain
- {
- public static class EncDec
- {
- public static string Encrypt(string toEncrypt, string SecurityKey ,bool useHashing)
- {
- byte[] keyArray;
- byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
-
- string key = SecurityKey;
-
-
- if (useHashing)
- {
- MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
- keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
-
-
-
- hashmd5.Clear();
- }
- else
- keyArray = UTF8Encoding.UTF8.GetBytes(key);
-
- TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
-
- tdes.Key = keyArray;
-
-
- tdes.Mode = CipherMode.ECB;
-
-
- tdes.Padding = PaddingMode.PKCS7;
-
- ICryptoTransform cTransform = tdes.CreateEncryptor();
-
- byte[] resultArray =
- cTransform.TransformFinalBlock(toEncryptArray, 0,
- toEncryptArray.Length);
-
- tdes.Clear();
-
- return Convert.ToBase64String(resultArray, 0, resultArray.Length);
- }
-
- public static string Decrypt(string cipherString, string SecurityKey, bool useHashing)
- {
- byte[] keyArray;
-
-
- byte[] toEncryptArray = Convert.FromBase64String(cipherString);
-
- string key = SecurityKey;
-
- if (useHashing)
- {
-
- MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
- keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
-
-
- hashmd5.Clear();
- }
- else
- {
-
- keyArray = UTF8Encoding.UTF8.GetBytes(key);
- }
-
- TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
-
- tdes.Key = keyArray;
-
-
-
- tdes.Mode = CipherMode.ECB;
-
- tdes.Padding = PaddingMode.PKCS7;
-
- ICryptoTransform cTransform = tdes.CreateDecryptor();
- byte[] resultArray = cTransform.TransformFinalBlock(
- toEncryptArray, 0, toEncryptArray.Length);
-
- tdes.Clear();
-
- return UTF8Encoding.UTF8.GetString(resultArray);
- }
-
- }
- }
Make a separate tool that will encode and decode a string using above code, encode your connection strings and place in the web.config file the encrypted connection strings. This way even if someone views your code, he would not be able to easily reach out to your data source without the secret key which only you have access to.