XML encryption and decryption

Source: Internet
Author: User

Encryption and decryption of XML or TXT files

# Region encrypts and decrypts files
Static string IV = "password ";
Static string key = "password ";

/// <Summary>
/// DES encryption offset, which must be an> = 8-bit long string
/// </Summary>
Public static string IV
{
Get {return IV ;}
Set {IV = value ;}
}

///


// des-encrypted private key, it must be an 8-bit long string
//
Public static string key
{< br> get {return key ;}
set {key = value ;}< BR >}

/// <Summary>
/// Perform DES encryption on the file content
/// </Summary>
/// <Param name = "sourcefile"> absolute path of the file to be encrypted </param>
/// <Param name = "destfile"> absolute path of the encrypted file </param>
Public static void encryptfile (string sourcefile, string destfile)
{

If (! File. exists (sourcefile) throw new filenotfoundexception ("the specified file path does not exist! ", Sourcefile );

Byte [] btkey = encoding. Default. getbytes (key );
Byte [] btiv = encoding. Default. getbytes (IV );
Descryptoserviceprovider des = new descryptoserviceprovider ();
Byte [] btfile = file. readallbytes (sourcefile );


Using (filestream FS = new filestream (destfile, filemode. Create, fileaccess. Write ))
{
Try
{
Using (cryptostream cs = new cryptostream (FS, Des. createencryptor (btkey, btiv), cryptostreammode. Write ))
{
CS. Write (btfile, 0, btfile. Length );
CS. flushfinalblock ();
}
}
Catch
{
Throw;
}
Finally
{
FS. Close ();
}
}
}

///


/// perform DES encryption on the file content, the encrypted file overwrites the original file
///
/// absolute path of the file to be encrypted
Public void encryptfile (string sourcefile)
{< br> encryptfile (sourcefile, sourcefile);
}

/// <Summary>
/// Perform des decryption on the file content
/// </Summary>
/// <Param name = "sourcefile"> absolute path of the file to be decrypted </param>
/// <Param name = "destfile"> absolute path of the decrypted file </param>
Public static void decryptfile (string sourcefile, string destfile)
{

If (! File. exists (sourcefile ))
Throw new filenotfoundexception ("the specified file path does not exist! ", Sourcefile );

Byte [] btkey = encoding. Default. getbytes (key );
Byte [] btiv = encoding. Default. getbytes (IV );
Descryptoserviceprovider des = new descryptoserviceprovider ();
Byte [] btfile = file. readallbytes (sourcefile );


Using (filestream FS = new filestream (destfile, filemode. Create, fileaccess. Write ))
{
Try
{
Using (cryptostream cs = new cryptostream (FS, Des. createdecryptor (btkey, btiv), cryptostreammode. Write ))
{
CS. Write (btfile, 0, btfile. Length );
CS. flushfinalblock ();
}
}
Catch
{
Throw;
}
Finally
{
FS. Close ();
}
}
}

/// <Summary>
/// Perform des decryption on the file content. After decryption, the original file will be overwritten.
/// </Summary>
/// <Param name = "sourcefile"> absolute path of the file to be decrypted </param>
Public static void decryptfile (string sourcefile)
{
Decryptfile (sourcefile, sourcefile );
}

# Endregion

 

Read/write encrypted XML or TXT

# Region read and operate the Assembly (XML file)
Public static void savexml (string connenctionstring, string strkey) // write dynamic database configuration information
{
Commonclass. decryptfile (filepath );
Xmldocument Doc = new xmldocument ();
// Obtain the full path of the configuration file
String strfilename = application. startuppath + "\ assembly. xml ";
Doc. Load (strfilename );
// Find all elements named "add"
Xmlnodelist nodes = Doc. getelementsbytagname ("add ");
For (INT I = 0; I <nodes. Count; I ++)
{
// Obtain the key attribute of the current element
Xmlattribute ATT = nodes [I]. attributes ["key"];
// Determine whether the current element is a target element based on the first attribute of the element.
If (Att. value = strkey)
{
// Assign values to the second attribute of the target Element
ATT = nodes [I]. attributes ["value"];
Att. value = connenctionstring;
Break;
}
}
Doc. Save (strfilename );
Commonclass. encryptfile (filepath, filepath );
}

Public static string readxml (string strkey) // write dynamic database configuration information
{
Commonclass. decryptfile (filepath );
String tempvalues = "";
Xmldocument Doc = new xmldocument ();
// Obtain the full path of the configuration file
String strfilename = application. startuppath + "\ assembly. xml ";
Doc. Load (strfilename );
// Find all elements named "add"
Xmlnodelist nodes = Doc. getelementsbytagname ("add ");
For (INT I = 0; I <nodes. Count; I ++)
{
// Obtain the key attribute of the current element
Xmlattribute ATT = nodes [I]. attributes ["key"];
// Determine whether the current element is a target element based on the first attribute of the element.
If (Att. value = strkey)
{
ATT = nodes [I]. attributes ["value"];
Tempvalues = Att. value;
Break;
}
}
Doc. Save (strfilename );
Commonclass. encryptfile (filepath, filepath );
Return tempvalues;
}

Public static sqlconnection getxmlconn () // write dynamic database configuration information
{
Commonclass. decryptfile (filepath );
Sqlconnection conn;
String tempdatabase = "", tempserverip = "", tempuser = "", temppassword = "", tempstr = "";
Xmldocument Doc = new xmldocument ();
// Obtain the full path of the configuration file
String strfilename = application. startuppath + "\ assembly. xml ";
Doc. Load (strfilename );
// Find all elements named "add"
Xmlnodelist nodes = Doc. getelementsbytagname ("add ");
For (INT I = 0; I <nodes. Count; I ++)
{
// Obtain the key attribute of the current element
Xmlattribute ATT = nodes [I]. attributes ["key"];
// Determine whether the current element is a target element based on the first attribute of the element.
If (Att. value = "serverip ")
{
Tempserverip = nodes [I]. attributes ["value"]. value;
}
Else if (Att. value = "Database ")
{
Tempdatabase = nodes [I]. attributes ["value"]. value;
}
Else if (Att. value = "user ")
{
Tempuser = nodes [I]. attributes ["value"]. value;
}
Else if (Att. value = "password ")
{
Temppassword = nodes [I]. attributes ["value"]. value;
}
}
Doc. Save (strfilename );
Tempstr = "uid =" + tempuser + "; Pwd =" + temppassword;
Tempstr + = "; initial catalog =" + tempdatabase + "; server =" + tempserverip + ";";
Tempstr + = "Connect timeout = 30 ";
Conn = new sqlconnection (tempstr );
Commonclass. encryptfile (filepath, filepath );
Return conn;
}

# Endregion

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.