Sunday, March 20, 2005

Encrypting and Decryption the connection string

Most of use saves the connection string in the Web.Config file in plain format which is not secure enough.

Here is a little piece of code that you can use to encrypt and decrypt the connection string.

Encryption of the connection string:

private string EncryptConnectionString(string connectionString)
{
Byte[] b = System.Text.ASCIIEncoding.ASCII.GetBytes(connectionString);
string encryptedConnectionString = Convert.ToBase64String(b);
return encryptedConnectionString;
}

Once the connection string is encrypted you can put it in the web.config file manually.


Descryption of the connection string:

private string DecryptConnectionString()
{
Byte[] b = Convert.FromBase64String(ConfigurationSettings.AppSettings["ConnectionString"]);
string decryptedConnectionString = System.Text.ASCIIEncoding.ASCII.GetString(b);
return decryptedConnectionString;
}



No comments: