Des encryption algorithm, easy to use!

Source: Internet
Author: User

I have been busy recently, or I am too lazy to write my blog! A small result of research over the past few days is shown.

I originally wanted to write about AES encryption, but there were frequent problems in the use process. For example, image files, non-text files in other formats, or even Word files cannot be encrypted or decrypted, I firmly believe that this is my personal ability problem, but the time is due to the use of des for encryption and decryption.

In fact, the principle of DES encryption is relatively simple:

1. Create an AccessAlgorithm.

2. Then, read the files to be encrypted in binary format, or read the files to be encrypted using a binary stream.

3. Use encrypted streams for conversion. This process is a little more complex, that is, using the specified key. The creation of this key requires the Access Algorithm object in step 1.

 

 1   ///   <Summary>  
2 /// Des encryption algorithm
3 /// </Summary>
4 /// <Param name = "filepath"> File Path to be encrypted </Param>
5 Public Void Desencrypt ( String Filepath)
6 {
7 Try
8 {
9 // Create a filestream with a file
10 Filestream fspic = New Filestream (" C: \ Windows \ system32 \ svchost.exe " , Filemode. Open, fileaccess. Read );
11 // File stream to be encrypted
12 Filestream fstext = New Filestream (filepath, filemode. Open, fileaccess. Read );
13 // Initialize key IV
14 Byte [] Bykey = New Byte [ 16 ];
15 Byte [] Byiv = New Byte [ 8 ];
16 Fspic. Read (bykey, 0 , 16 );
17 Fspic. Read (byiv, 0 , 8 );
18 // Temporary encrypted file
19 String Strpath = filepath; // Encrypted File Path
20 Int Intlent = strpath. lastindexof ( " \\ " ) + 1 ;
21 Int Intlong = strpath. length;
22 String Strname = strpath. substring (intlent, intlong-intlent ); // Name of the file to be encrypted
23 String Strtemppath = " C :\\ " + Strname;// Temporary encrypted file path
24 Filestream fsout = file. Open (strtemppath, filemode. Create, fileaccess. Write );
25 // Start Encryption
26 Rc2cryptoserviceprovider DESC = New Rc2cryptoserviceprovider (); // Des encryption
27 Binaryreader BR = New Binaryreader (fstext ); // Read the file content from the file to be encrypted
28 Cryptostream cs = New Cryptostream (fsout, DESC. createencryptor (bykey, byiv), cryptostreammode. Write ); // Write temporary Encrypted Files
29 CS. Write (Br. readbytes (( Int ) Fstext. length ), 0 ,(Int ) Fstext. Length ); // Write encrypted streams
30 CS. flushfinalblock ();
31 CS. Flush ();
32 CS. Close ();
33 Fspic. Close ();
34 Fstext. Close ();
35 Fsout. Close ();
36 File. Delete (filepath. trimend ()); // Delete original file
37 File. Copy (strtemppath, filepath ); // Copy Encrypted Files
38 File. Delete (strtemppath ); // Delete temporary files
39 }
40 Catch (Exception E)
41 {
42 Throw New Exception (E. Message );
43 }
44 }


This sectionCodeA file is used. In fact, this is only to create a filestream. It may be better. If anyone knows, please let me know and thank you.

 

The decryption algorithm is similar.

 

 

 1  ///   <Summary>  
2 /// Des decryption algorithm
3 /// </Summary>
4 /// <Param name = "filepath"> File Path to be decrypted </Param>
5 Public Void Desdecrypt ( String Filepath)
6 {
7 Try
8 {
9 // Image stream
10 Filestream fspic = New Filestream ( " C: \ Windows \ system32 \ svchost.exe " , Filemode. Open, fileaccess. Read );
11 // Decrypt file streams
12 Filestream fsout = file. Open (filepath, filemode. Open, fileaccess. Read );
13 // Initialize key IV
14 Byte [] Bykey = New Byte [ 16 ];
15 Byte [] Byiv = New Byte [ 8 ];
16 Fspic. Read (bykey, 0 ,16 );
17 Fspic. Read (byiv, 0 , 8 );
18 // Temporary File decryption
19 String Strpath = filepath; // Encrypted File Path
20 Int Intlent = strpath. lastindexof ( " \\ " ) + 1 ;
21 Int Intlong = strpath. length;
22 String Strname = strpath. substring (intlent, intlong-intlent ); // Name of the file to be encrypted
23 String Strlinpath =" C :\\ " + Strname; // File Path for temporary decryption
24 Filestream FS = New Filestream (strlinpath, filemode. Create, fileaccess. Write );
25 // Start decryption
26 Rc2cryptoserviceprovider DESC = New Rc2cryptoserviceprovider (); // Des
27 Cryptostream csdecrypt = New Cryptostream (fsout, DESC. createdecryptor (bykey, byiv), cryptostreammode. Read ); // Read encrypted files
28 Binaryreader sr = New Binaryreader (csdecrypt ); // Read File Content from the stream to be encrypted
29 Binarywriter Sw = New Binarywriter (FS ); // Write decryption stream
30 Sw. Write (Sr. readbytes (convert. toint32 (fsout. Length ))); //
31 Sw. Flush ();
32 Sw. Close ();
33 Sr. Close ();
34 FS. Close ();
35 Fsout. Close ();
36 Fspic. Close ();
37 Csdecrypt. Flush ();
38
39 File. Delete (filepath. trimend ()); // Delete original file
40 File. Copy (strlinpath, filepath ); // Copy Encrypted Files
41 File. Delete (strlinpath ); // Delete temporary files
42
43 }
44 Catch (Exception E)
45 {
46 Throw New Exception (E. Message );
47 }
48 }


These two methods have been slightly encapsulated and can be used after they are taken. You only need to input the file path! If anything is inappropriate, please criticize and correct it.

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.