ASP. NET web. config encryption and decryption methods (CODE), asp. netweb. config

Source: Internet
Author: User

ASP. NET web. config encryption and decryption methods (CODE), asp. netweb. config

  1. /// <Summary>
  2. /// Protect web. config encryption and decryption
  3. /// </Summary>
  4. Public class ProtectHelper
  5. {
  6. /// <Summary>
  7. /// Decrypt
  8. /// </Summary>
  9. /// <Param name = "pToDecrypt"> encrypted connection string </param>
  10. /// <Param name = "sKey"> Custom key </param>
  11. /// <Returns> decrypt the string </returns>
  12. Public static string UnProtectSection (string pToDecrypt, string sKey)
  13. {
  14. Byte [] inputByteArray = Convert. FromBase64String (pToDecrypt );
  15. Using (DESCryptoServiceProvider des = new DESCryptoServiceProvider ())
  16. {
  17. Des. Key = ASCIIEncoding. ASCII. GetBytes (sKey );
  18. Des. IV = ASCIIEncoding. ASCII. GetBytes (sKey );
  19. System. IO. MemoryStream MS = new System. IO. MemoryStream ();
  20. Using (CryptoStream cs = new CryptoStream (MS, des. CreateDecryptor (), CryptoStreamMode. Write ))
  21. {
  22. Cs. Write (inputByteArray, 0, inputByteArray. Length );
  23. Cs. FlushFinalBlock ();
  24. Cs. Close ();
  25. }
  26. String str = Encoding. UTF8.GetString (ms. ToArray ());
  27. Ms. Close ();
  28. Return str;
  29. }
  30. }
  31. /// <Summary>
  32. /// Encryption
  33. /// </Summary>
  34. /// <Param name = "pToEncrypt"> connection string </param>
  35. /// <Param name = "sKey"> Custom key </param>
  36. /// <Returns> encrypted string </returns>
  37. Public static string ProtectSection (string pToEncrypt, string sKey)
  38. {
  39. Using (DESCryptoServiceProvider des = new DESCryptoServiceProvider ())
  40. {
  41. Byte [] inputByteArray = Encoding. UTF8.GetBytes (pToEncrypt );
  42. Des. Key = ASCIIEncoding. ASCII. GetBytes (sKey );
  43. Des. IV = ASCIIEncoding. ASCII. GetBytes (sKey );
  44. System. IO. MemoryStream MS = new System. IO. MemoryStream ();
  45. Using (CryptoStream cs = new CryptoStream (MS, des. CreateEncryptor (), CryptoStreamMode. Write ))
  46. {
  47. Cs. Write (inputByteArray, 0, inputByteArray. Length );
  48. Cs. FlushFinalBlock ();
  49. Cs. Close ();
  50. }
  51. String str = Convert. ToBase64String (ms. ToArray ());
  52. Ms. Close ();
  53. Return str;
  54. }
  55. }
  56. }

In aspnet, how does one encrypt config connectionstrings?

To encrypt the configuration information on the website, you do not need to write any code or modify any code. You only need to use the aspnet_regiis tool to modify the configuration file.
For example, the following configuration file needs to be encrypted:
<Configuration>
<ConnectionStrings>
<Add name = "SqlServices" connectionString = "Data Source = localhost;
Integrated Security = SSPI; Initial Catalog = Northwind; "/>
</ConnectionStrings>
</Configuration>

Assume that the configuration file is in the MyApplication directory.
Encryption command
Aspnet_regiis-pe "connectionStrings"-app "/MyApplication"

The aspnet_regiis command is in the. net Framework directory you have installed. The default value is:
C: \ WINDOWS \ Microsoft. Net \ Framework \ v2.0 .*

Encrypted effect:
<Configuration>
<ConnectionStrings configProtectionProvider = "RsaProtectedConfigurationProvider">
<EncryptedData Type = "www.w3.org/2001/04/xmlenc?element"
Xmlns = "www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm = "www.w3.org/2001/04/xmlenc?tripledes-cbc"/>
<KeyInfo xmlns = "www.w3.org/2000/09/xmldsig#">
<EncryptedKey xmlns = "...... the remaining full text>

C # & aspnet encryption and decryption all the value methods in a webconfig Node

Just give me an idea.
1. Write a class or package the symmetric encryption algorithm into a dll for encryption and decryption.
2. web. config is an XML file. You can encrypt the content in <re> </re> into a string, and add the CDATA at both ends of the content to indicate the XML content.
3. Read and decrypt the content in Xml.

Why don't I write it into the deleetting system? Aspnet also supports functions, which is easier than making XML directly.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.