I used connection string in app.config file in c#. I want to change connection string dynamically. I used a form to change the connection string. i want when i change server configuration and click to save it the connection string encrypted. i use a text box to encrypted password field when text changed.i use following code to encrypted connection string. it save the connection string encrypted but my software not connect the database for the encryption. It shows no valid password. How to Solve this problem?
private void btnSave_Click(object sender, EventArgs e)
{
DBPath = cbodbsource.Text + txtdbname.Text;
//Set connection string
string connectionString = string.Format("Provider={0};Data Source={1};Jet OLEDB:Database Password={2};", cboProvider.Text, DBPath, txtdbpassword.Text);
try
{
OledbHelper helper = new OledbHelper(connectionString);
if (helper.IsConnection)
{
AppSetting setting = new AppSetting();
setting.SaveConnectionString("con", connectionString);
MessageBox.Show("Your connection string has been successfully saved.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
string Encrypt(string source, string key)
{
using (TripleDESCryptoServiceProvider tripleDESCryptoService = new TripleDESCryptoServiceProvider())
{
using (MD5CryptoServiceProvider hashMD5Provider = new MD5CryptoServiceProvider())
{
byte[] byteHash = hashMD5Provider.ComputeHash(Encoding.UTF8.GetBytes(key));
tripleDESCryptoService.Key = byteHash;
tripleDESCryptoService.Mode = CipherMode.ECB;
byte[] data = Encoding.UTF8.GetBytes(source);
return Convert.ToBase64String(tripleDESCryptoService.CreateEncryptor().TransformFinalBlock(data, 0, data.Length));
}
}
}
I have used text_Changed Event Code. Here is it
private void txtdbpassword_TextChanged(object sender, EventArgs e)
{
txtEncrypt.Text = this.Encrypt(txtdbpassword.Text, "myconstring");
}