File encryption and decryption in. net

Source: Internet
Author: User
Keywords :. NET 2005 Framework encryption and decryption file body: When you use an XML file to record configuration information, sometimes you do not want others to see the configuration information. how can this problem be achieved. several common encryption and decryption algorithms are introduced here. these algorithm frameworks have been encapsulated. we don't need to care about the specific implementation, just use it. the following is a source program for your reference.
Using system;
Using system. Security. cryptography;
Using system. IO;
Using system. Text; namespace coder
{
/// <Summary>
/// Summary of symmcrypto.
/// The symmcrypto class implements the encryption and decryption service under the. NET Framework.
/// Original Author: Frank Fang: fangfrank@hotmail.com
/// Receiver: Ligang: nkligang@163.com
/// </Summary>
Public class symmcrypto
{
Public Enum symatrix rovenum: int
{
Des, RC2, Rijndael
} Private encryption ricalgorithm mobjcryptoservice; // <remarks>
/// Use the. NET javasricalgorithm class constructor.
/// </Remarks>
Public symmcrypto (symatrix rovenum netselected)
{
Switch (netselected)
{
Case symatrix rovenum. Des:
Mobjcryptoservice = new descryptoserviceprovider ();
Break;
Case symatrix rovenum. RC2:
Mobjcryptoservice = new rc2cryptoserviceprovider ();
Break;
Case symatrix rovenum. Rijndael:
Mobjcryptoservice = new rijndaelmanaged ();
Break;
}
} /// <Remarks>
/// Use the constructor of the custom symmetricalgorithm class.
/// </Remarks>
Public symmcrypto (symmetricalgorithm serviceprovider)
{
Mobjcryptoservice = serviceprovider;
} /// <Remarks>
/// Depending on the legal key size limitations
/// A specific cryptoservice provider and length
/// The Private Key provided, padding the secret key
/// With space character to meet the legal size of the algorithm.
/// </Remarks>
Private byte [] getlegalkey (string key)
{
String stemp;
If (mobjcryptoservice. legalkeysizes. length> 0)
{
Int lesssize = 0, moresize = mobjcryptoservice. legalkeysizes [0]. minsize;
// Key sizes are in bits
While (key. length * 8> moresize)
{
Lesssize = moresize;
Moresize + = mobjcryptoservice. legalkeysizes [0]. skipsize;
}
Stemp = key. padright (moresize/8 ,'');
}
Else
Stemp = key; // convert the secret key to byte array
Return asciiencoding. ASCII. getbytes (stemp );
} Public byte [] encrypting (byte [] bytin, string key)
{// Create a memorystream so that the process can be done without I/O files
System. Io. memorystream MS = new system. Io. memorystream (); byte [] bytkey = getlegalkey (key); // set the Private Key
Mobjcryptoservice. Key = bytkey;
Mobjcryptoservice. IV = bytkey; // create an encryptor from the Provider Service instance
Icryptotransform encrypto = mobjcryptoservice. createencryptor (); // create crypto stream that transforms a stream using the encryption
Cryptostream cs = new cryptostream (MS, encrypto, cryptostreammode. Write); // write out encrypted content into memorystream
CS. Write (bytin, 0, bytin. Length );
CS. flushfinalblock (); // get the output and trim the '/0' bytes
Byte [] bytout = Ms. getbuffer (); Return bytout;
} Public byte [] decrypting (byte [] bytin, string key)
{
// Create a memorystream with the input
System. Io. memorystream MS = new system. Io. memorystream (bytin, 0, bytin. Length); byte [] bytkey = getlegalkey (key); // set the Private Key
Mobjcryptoservice. Key = bytkey;
Mobjcryptoservice. IV = bytkey; // create a decryptor from the Provider Service instance
Icryptotransform encrypto = mobjcryptoservice. createdecryptor (); // create crypto stream that transforms a stream using the decryption
Cryptostream cs = new cryptostream (MS, encrypto, cryptostreammode. Write );
// Read out the result from the crypto stream
CS. Write (bytin, 0, bytin. Length );
CS. Flush (); Return Ms. toarray ();
}
}
} The following is an example: // encode public bool encoding (string inputfile, string outputfile)
{
Byte [] buffer = new byte [1024];
Byte [] Buf = new byte [1032]; int temp;
Coder. symmcrypto coding = new symmcrypto (CODER. symmcrypto. symatrix rovenum. Des );
If (system. Io. file. exists (inputfile ))
{
// Define the File Read class
Stream instream = file. Open (inputfile, filemode. Open );
Stream outstream = file. Create (outputfile );
// Read the file
Do
{
Temp = instream. Read (buffer, 0, 1024 );
Buf = coding. encrypting (buffer, "12345678 ");
If (temp> 0)
{
Outstream. Write (BUF, 0, temp + 8 );
}
Else
Break;
}
While (true); // releases stream Resources
Instream. Close ();
Outstream. Close ();
} Return true;
} // Decode public bool decoding (string inputfile, string outputfile)
{
Byte [] buffer = new byte [1032];
Byte [] Buf = new byte [1, 1024];
Int temp;
Coder. symmcrypto coding = new symmcrypto (CODER. symmcrypto. symatrix rovenum. Des );
If (system. Io. file. exists (inputfile ))
{
Stream instream = file. Open (inputfile, filemode. Open );
Stream outstream = file. Create (outputfile );
Do
{
Temp = instream. Read (buffer, 0, 1032 );
Buf = coding. Decrypting (buffer, "12345678"); If (temp> 0)
{
Outstream. Write (BUF, 0, temp-8 );
}
Else
Break;
}
While (true); instream. Close ();
Outstream. Close ();
} Return true;
} Notes: 1. this program applies to file encryption and decryption. the source author's encryption and decryption programs have problems in reading and writing binary files and even text files. this program removes the base64 encoding and decoding process in the source program. because of this problem, after the file content is encrypted, the character 0 (ASCII) may occur, and this special character may cause problems in base64 encoding. the base64 encoding function considers 0 as the end and fills the subsequent part with some characters. 3. this is applicable to the file input/output buffer size issue. the number of characters After encryption is increased by 8. therefore, the buffer to be read should be 8 characters smaller than the buffer to be written during encryption, but the size of the buffer to be read cannot be greater than that of the buffer to be written by 8 characters, however, we recommend that you follow this solution.

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.