r3nder
asked on
encrypt the app.config connectionstring
I am trying to encrypt the connection string in a win form and I found this code on Expert Exchange. I encrypted the connection string but how do I put the encrypted string in the config file?
code to get encrypted string
Encryption/decryption code
code to get encrypted string
private void Form1_Load(object sender, EventArgs e)
{
string conString = ConfigurationManager.ConnectionStrings["MyConString"].ToString();
textBox1.Text = vault.Encrypt(conString);
}
Encrypted string7sIgbZ0r6wGpIahLvIZrJmCjYAJNJGMno464rU6TMaimMYfOKAxZD2NyPYgT76/J09BUqjeT9/1X9ZfvbaY7rKYCBz2H+xqE2PWStpQJ3Da9nENakNNqx683yj5EEp514CKaVG7EUw4MiUG9ASNKLjUcnVyw340Vpu0PSNjMMs/WoZS/zQvy8CFXZtpvOS+C
Encryption/decryption code
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace EncryptTest
{
class vault
{
/************************************************************************************************************************************/
#region Public Methods
public static string Encrypt(string plainText)
{
RijndaelManaged key = GetKey();
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
using (ICryptoTransform encryptor = key.CreateEncryptor())
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainText.Length);
cryptoStream.FlushFinalBlock();
return Convert.ToBase64String(memoryStream.ToArray());
}
}
}
}
public static string Decrypt(string encryptedString)
{
RijndaelManaged key = GetKey();
byte[] encryptedData = Convert.FromBase64String(encryptedString);
using (ICryptoTransform decryptor = key.CreateDecryptor(key.Key, key.IV))
{
using (MemoryStream memoryStream = new MemoryStream(encryptedData))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
byte[] plainText = new byte[encryptedData.Length];
int decryptedCount = cryptoStream.Read(plainText, 0, plainText.Length);
return Encoding.UTF8.GetString(plainText, 0, decryptedCount);
}
}
}
}
#endregion
/************************************************************************************************************************************/
#region Private Methods
//Create encryption key
private static RijndaelManaged GetKey()
{
RijndaelManaged key = new RijndaelManaged();
byte[] passwordBytes = Encoding.UTF8.GetBytes(Properties.Resources.Password);
byte[] saltBytes = Encoding.UTF8.GetBytes(Properties.Resources.Salt);
PasswordDeriveBytes p = new PasswordDeriveBytes(passwordBytes, saltBytes);
key.IV = p.GetBytes(key.BlockSize / 8);
key.Key = p.GetBytes(key.KeySize / 8);
return key;
}
#endregion
/************************************************************************************************************************************/
}
}
I have a sample at http://emoreau.com/Entries/Articles/2009/05/Using-the-ConnectionStrings-section-of-the-configuration-file.aspx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you - explains alot