Use C # To create the file's MD5 Verification Code

Source: Internet
Author: User
Tags md5 hash
Due to the openness of the Internet, any file may be tampered during transmission, and the uncertainty of the transmission process leads to a lack of effective methods to ensure the security of the file to be transmitted. To avoid the above situation, the most common practice is to provide a verification code with the file being transmitted. After receiving the file, the user recalculates the file verification code and compares it with the original verification code, if they do not match, the file is changed during transmission. Next, I will use C # to demonstrate the specific implementation process.

1. Create a FileStream

Before building the file verification code, you must first load the file, which requires the FileStream class of. Net framework. In. Net framework, all files are represented as a Stream, which is the abstract concept of the byte sequence. All read/write operations involving files are implemented through the attributes and methods of the Stream class. The specific implementation code is as follows:

FileStream fst = new FileStream (txtFile. Text, FileMode. Open, FileAccess. Read, FileShare. Read, 8192 );

Here, we use FileStream to build a function. We need to mention that the last parameter value is 8192, which defines the buffer size, that is, when the file is larger than 8 K, in 8 K, the file is read in segments to improve the file reading performance.

Ii. Create an MD5 object

After successfully creating a FileStream object, you can use the MD5 class to calculate the hash value of the object. This implementation process is very simple. You only need to declare an MD5CryptoServiceProvider instance first, then use the ComputeHash method to complete the calculation process, and finally obtain the calculated byte array through the Hash attribute.

......

MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider ();

FileStream fst = new FileStream (txtFile. Text, FileMode. Open, FileAccess. Read, FileShare. Read, 8192 );

Md5.ComputeHash (fst );

Byte [] hash = md5.Hash;

......

3. Convert the verification code string

Because the MD5 hash algorithm returns a byte of data, it must be converted to a string. The following is the specific implementation code.

......

Byte [] hash = md5.Hash;

StringBuilder sb = new StringBuilder ();

Foreach (byte byt in hash)

{

Sb. Append (String. Format ("{0: X1}", byt ));

}

TextBox1.Text = sb. ToString ();

......

During the conversion process, a StringBuilder object is defined first, which is mainly considered for performance. Then, each byte in the MD5 hash value is traversed and the String is used. the Format method directly converts bytes to a hexadecimal string and outputs The result string.

Note: The MD5 verification code generated in this example is exactly the same as that generated by similar MD5 verification code verification tools in the algorithm. Therefore, it is applicable to practical applications.

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.