Encrypt the installer's connection string for the configuration file

Source: Internet
Author: User

Due to security requirements, we releasedProgramConnection strings should not be directly exposed to end users, so it is necessary to encrypt the connection strings in the configuration file.

Here is a brief introduction to the installationProgramThe method used to encrypt the connection string (for winformProgram).

First, create a class library Program The name is clientinstallaction, and the new class installaction. CS is created. Code As follows. Free Resources

[Runinstaller (true)]
Public class installaction: Installer
{
Public override void install (system. Collections. idictionary statesaver)
{
Base. Install (statesaver );
Mencryptconfig ();
}

VD; k; l rdfg

 

/// <Summary>
/// Installation path
/// </Summary>

Private string _ installpath
{
Get
{
Return context. Parameters ["targetdir"];
}
}
/// <Summary>
/// Configuration file path
/// </Summary>
Private string xmlpath
{
Get
{
Return Path. Combine (_ installpath, @ ""); online tutorial
}
}

 

Private void mencryptconfig ()
{
Xmldocument Doc = new xmldocument ();
Doc. Load (xmlpath );
Xmlnode connectionstringsnode = Doc. selectsinglenode ("/configuration/connectionstrings"); // find the connectionstring Node

Desprotectedconfigurationprovider provider = new desprotectedconfigurationprovider ();

 

Xmlelement newconnectionstringsnode = Doc. createelement ("encrypteddata ");
Newconnectionstringsnode. innerxml = provider. Encrypt (connectionstringsnode). innerxml; // encrypted string

 

Connectionstringsnode. removeall ();
Connectionstringsnode. appendchild (newconnectionstringsnode as xmlnode); // Delete and replace connectionstring node 45398 it55 learn IT knowledge and enjoy it life 4 dfkjn

Xmlattribute configprotectionatt = Doc. createattribute ("configprotectionprovider ");
Configprotectionatt. value = "desprotectedconfigurationprovider ";

It55.com

 

Connectionstringsnode. Attributes. append (configprotectionatt );
Doc. Save (xmlpath );
}
}

Then create the desprotectedconfigurationprovider. CS class for encryption and decryption.

The Code is as follows:

Public class desprotectedconfigurationprovider: protectedconfigurationprovider
{
// Set key and IV
Private byte [] cipher ey = {23, 44, 54, 12, 42, 77, 45, 33 };
Private byte [] desiv = {39, 48, 5, 32, 55, 46, 30, 99 };

 

// Create des provider
Private descryptoserviceprovider mprovider = new descryptoserviceprovider ();Online tutorial

Private string pname;

 

// Gets the provider name.
Public override string name
{
Get {return pname ;}
}

 

// Performs provider initialization.
Public override void initialize (string name,
Namevaluecollection config)
{
Pname = Name;
}

 

Public override system. xml. xmlnode decrypt (xmlnode encryptednode)
{
String decrypteddata = decryptstring (encryptednode. innertext );

Xmldocument xmldoc = new xmldocument ();
Xmldoc. preservewhitespace = true;
Xmldoc. loadxml (decrypteddata); Return xmldoc. documentelement;
}

Public override system. xml. xmlnode encrypt (xmlnode node)
{
String encrypteddata = encryptstring (node. outerxml );

 

xmldocument xmldoc = new xmldocument ();
xmldoc. preservewhitespace = true;
xmldoc. loadxml (" " +
encrypteddata + " ");
return xmldoc. documentelement;
}< br> // encrypts a configuration section and returns
// The encrypted XML as a string.
private string encryptstring (string encryptvalue)
{< br> // create a memory stream.
memorystream MS = new memorystream ();

// Create a cryptostream using the memory stream and
// CSP des key.
Cryptostream encstream = new cryptostream (MS, mprovider. createencryptor (Cipher ey, desiv), cryptostreammode. Write );
// Create a streamwriter to write a string
// To the stream.
Streamwriter Sw = new streamwriter (encstream );

 

// Write the plaintext to the stream.
Sw. writeline (encryptvalue );

// Close the streamwriter and cryptostream.
Sw. Close ();
Encstream. Close ();

// Get an array of bytes that represents
// The memory stream.
String encryptedvalue = convert. tobase64string (Ms. toarray ());

 

// Close the memory stream.
Ms. Close (); sflj kg ^ & FGD

// Return the encrypted byte array.
Return encryptedvalue;
}
// Decrypts an encrypted configuration section and
// Returns the unencrypted XML as a string.
Private string decryptstring (string encryptedvalue)
{
// Create a memory stream to the passed buffer.
Memorystream MS = new memorystream (convert. frombase64string (encryptedvalue ));

// Create a cryptostream using the memory stream and
// CSP des key.
Cryptostream encstream = new cryptostream (MS, mprovider. createdecryptor (Cipher ey, desiv), cryptostreammode. Read );

 

 

// Create a streamreader for reading the stream.
Streamreader sr = new streamreader (encstream );

// Read the stream as a string.

String val = Sr. readtoend ();

 

 

// Close the streams.
Sr. Close ();
Encstream. Close ();
Ms. Close ();

Return val;
}
} Http :///

InstallProgramOutput the project together.

Select View> Custom operation on the installation project, add the project on the installation item, and edit the customeractiondata attribute, the value is/targetdir = "[targetdir] \". This attribute is used to determine the path during installation.

 

OK. Everything is finished.

Note: Simply put, the configuration file is opened during project installation and encrypted Algorithm Encrypts the connection string and replaces the previous node of connectionstring.

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.