Here's a not-so-secure reference implementation of a simple connection string encryption/decryption mechanism.
First of all, encode your connection string by using the Base64 encoding scheme.
Unencoded Connection String:
server=localhost\SQLEXPRESS2012;database=testdb;uid=testuser;pwd=supersecret
Base64 Encoded Connection String:
c2VydmVyPWxvY2FsaG9zdFxTUUxFWFBSRVNTMjAxMjtkYXRhYmFzZT10ZXN0ZGI7dWlkPXRlc3R1c2VyO3B3ZD1zdXBlcnNlY3JldA==
After this, the corresponding line in your App.config file should look like this.
<add name="TestDb" connectionString="c2VydmVyPWxvY2FsaG9zdFxTUUxFWFBSRVNTMjAxMjtkYXRhYmFzZT10ZXN0ZGI7dWlkPXRlc3R1c2VyO3B3ZD1zdXBlcnNlY3JldA==" providerName="System.Data.SqlClient" />
Finally, modify your DbContext to be able to create database connections by using the encoded connection string.
using System;
using System.Configuration;
using System.Data.Common;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Text;
namespace TestApp
{
public class TestDb : DbContext
{
public TestDb() : base(CreateConnection("TestDb"), true)
{
}
static DbConnection CreateConnection(string dbName)
{
string encodedCs = ConfigurationManager.ConnectionStrings[dbName].ConnectionString;
string decodedCs = Encoding.UTF8.GetString(Convert.FromBase64String(encodedCs));
return new SqlConnection(decodedCs);
}
}
}
As you've noticed, this implementation uses the Base64 encoding scheme which can easily be reversed by the end users if they know what they are doing.