<appSettings>
<add key="ConnectionString" value="server=localhost;database=test;pwd=sa;uid=sa;" />
</appSettings>
现在,使用一个简单的加密,其实是base64编码而非加密。
private void Button1_Click(object sender, System.EventArgs e)
{
byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes(this.TextBox1.Text);
string str = Convert.ToBase64String(data);
this.TextBox2.Text = str;
<add key="ConnectionString" value="server=localhost;database=test;pwd=sa;uid=sa;" />
</appSettings>
现在,使用一个简单的加密,其实是base64编码而非加密。
private void Button1_Click(object sender, System.EventArgs e)
{
byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes(this.TextBox1.Text);
string str = Convert.ToBase64String(data);
this.TextBox2.Text = str;
}
得到:c2VydmVyPWxvY2FsaG9zdDtkYXRhYmFzZT10ZXN0O3B3ZD1zYTt1aWQ9c2E7
之后,我们用这个字符串替换未编码的字符串。如下所示:
<appSettings>
<add key="ConnectionString" value="c2VydmVyPWxvY2FsaG9zdDtkYXRhYmFzZT10ZXN0O3B3ZD1zYTt1aWQ9c2E7" />
</appSettings>
我们的程序需要理解这个字符串的意义,我们在数据访问层里面添加如下的工具方法:
private string GetConnectionString()
{
string strconn = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
byte[] data = Convert.FromBase64String(strconn);
string strRealConn = System.Text.ASCIIEncoding.ASCII.GetString(data);
return strRealConn;
}