C# .NET - Encryption of Configuration File.

Asked By sunil juluru
03-Feb-12 04:04 AM
How to encrypt and decrypt the Configuration files in windows Application.
Please send me the code.
  kalpana aparnathi replied to sunil juluru
03-Feb-12 04:15 AM
hi,

Try this code:

static void ToggleConfigEncryption(string exeConfigName)
{
  // Takes the executable file name without the
  // .config extension.
  try
  {
    // Open the configuration file and retrieve
    // the connectionStrings section.
    Configuration config = ConfigurationManager.
      OpenExeConfiguration(exeConfigName);
 
    ConnectionStringsSection section =
      config.GetSection("connectionStrings")
      as ConnectionStringsSection;
 
    if (section.SectionInformation.IsProtected)
    {
      // Remove encryption.
      section.SectionInformation.UnprotectSection();
    }
    else
    {
      // Encrypt the section.
      section.SectionInformation.ProtectSection(
        "DataProtectionConfigurationProvider");
    }
    // Save the current configuration.
    config.Save();
 
    Console.WriteLine("Protected={0}",
      section.SectionInformation.IsProtected);
  }
  catch (Exception ex)
  {
    Console.WriteLine(ex.Message);
  }
}

more detail:http://msdn.microsoft.com/en-us/library/ff647398.aspx
  dipa ahuja replied to sunil juluru
03-Feb-12 05:05 AM
static byte[] bytes = ASCIIEncoding.ASCII.GetBytes("ZeroCool");
   
void passString()
{
  string Encryptedcode = Encrypt(TextBox1.Text.ToString());  
 
}
  
/* Encrypt */
 
public static string Encrypt(string originalString)
{
  if (String.IsNullOrEmpty(originalString))
  {
    throw new ArgumentNullException
      ("The string which needs to be encrypted can not be null.");
  }
  DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
  MemoryStream memoryStream = new MemoryStream();
  CryptoStream cryptoStream = new CryptoStream(memoryStream,
    cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
  StreamWriter writer = new StreamWriter(cryptoStream);
  writer.Write(originalString);
  writer.Flush();
  cryptoStream.FlushFinalBlock();
  writer.Flush();
  return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
}
 
/* Decrypt */
public static string Decrypt(string cryptedString)
{
  if (String.IsNullOrEmpty(cryptedString))
  {
    throw new ArgumentNullException
      ("The string which needs to be decrypted can not be null.");
  }
  DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
  MemoryStream memoryStream = new MemoryStream
    (Convert.FromBase64String(cryptedString));
  CryptoStream cryptoStream = new CryptoStream(memoryStream,
    cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
  StreamReader reader = new StreamReader(cryptoStream);
  return reader.ReadToEnd();
}
  
 
Create New Account
help
Function b64encode(ByVal StrEncode As String ) Dim encodedString As String encodedString = (Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(StrEncode))) Return (encodedString) End Function Public Shared Function b64decode(ByVal StrDecode As String ) Dim decodedString As String decodedString = System.Text.ASCIIEncoding.ASCII.GetString(Convert.FromBase64String(StrDecode)) Return decodedString End Function End Class< / code > 3. try this Byte Try key = System . Text . Encoding . UTF8 . GetBytes ( Left ( sEncryptionKey , 8 )) Dim des As New DESCryptoServiceProvider () inputByteArray = Convert . FromBase64String ( stringToDecrypt ) Dim ms As New MemoryStream () Dim cs As New CryptoStream ( ms String Try key = System . Text . Encoding . UTF8 . GetBytes ( Left ( SEncryptionKey , 8 )) Dim des As New DESCryptoServiceProvider () Dim inputByteArray () As Byte = Encoding . UTF8 . GetBytes ( _ stringToEncrypt ) Dim ms As New MemoryStream () Dim Also define below function to generate the Key private SymmetricAlgorithm GenerateKey( string password) { / / string SysUserName = WebConfigurationManager.AppSettings["SysUserName"]; string SysUserName = ViewState[ "UserID" ].ToString(); string saltValue = SysUserName; / / string saltValue = System.Environment.UserName
i write the function to decrypt it too? [CODE] string results = ""; byte[] myPassword; System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); SHA1 sha = new SHA1CryptoServiceProvider(); myPassword = sha.ComputeHash(encoding.GetBytes(pwd.ToString().Trim())); myPassword = encoding.GetBytes(Convert.ToBase64String(myPassword)); System.Text.ASCIIEncoding enc = new System.Text ASCIIEncoding(); results = enc.GetString(myPassword); [ / CODE] thanks thanks, urgent! Check out http: / / msdn.microsoft.com / library dnnetsec / html / cryptosimplified.asp [CODE] { byte[] data = System.Convert.FromBase64String(encrypted); byte[] rgbKey = System.Text.ASCIIEncoding.ASCII.GetBytes("12121212"); byte[] rgbIV = System.Text.ASCIIEncoding.ASCII.GetBytes("34343434"); MemoryStream memoryStream = new MemoryStream(data.Length); DESCryptoServiceProvider desCryptoServiceProvider = new DESCryptoServiceProvider(); CryptoStream cryptoStream = new CryptoStream(memoryStream, desCryptoServiceProvider.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Read); memoryStream.Write
URLEncoding .NET Framework How do I URLencode my data in the code below? ASCIIEncoding encoding = new ASCIIEncoding(); / / Server.URLEncode(strXML) string postData = "xml = " + doc.ToString(); byte[] data = encoding.GetBytes(postData); string EmailWebservices Dude (Doing classic ASP to get paid, and WPF for fun.) .NET Web Services Discussions ASCIIEncoding (1) Server.URLEncode (1) ConfigurationManager.AppSettings (1) HttpUtility.UrlEncode (1) HttpWebRequest (1) WebRequest.Create (1 for fun.) keywords: URLEncoding description: How do I URLencode my data in the code below ASCIIEncoding encoding = new ASCIIEncoding(); / / Server.URLEncode(strXML) string postData = xml = + doc.ToS
encryption / decryption of querystring public class encryptdecrypt { ASCIIEncoding textconverter = new ASCIIEncoding (); RC2CryptoServiceProvider rc2csp = new RC2CryptoServiceProvider (); byte [] key; byte [] IV; byte [] encryptedByte; public encryptdecrypt() { } public string EncryptionData
below. Where does the urlencding take place? string strId = UserId_TextBox.Text; string strName = Name_TextBox.Text; ASCIIEncoding encoding = new ASCIIEncoding(); string postData = "userid = "+strId; postData + = ("&username = "+strName); byte[] data = encoding.GetBytes(postData); / / Prepare web request Data Dude (Doing classic ASP to get paid, and WPF for fun.) ASP.NET Discussions ASCIIEncoding (1) HttpWebRequest (1) GetRequestStream (1) WebRequest (1) TextBox (1) WPF (1) UrlEncode (1) GetBytes (1 below. Where does the urlencding take place string strId = UserId_TextBox.Text; string strName = Name_TextBox.Text; ASCIIEncoding en