Due to the openness of the Internet, C # is widely used to create files. For example, any file may be tampered during transmission, and the transmission process is uncertain, this makes it impossible for us to effectively guarantee the security of transmitted files. How can we find a solution to the problem? Here we will talk about the application that uses C # To create files.
To avoid the above situation, the most common practice is to provide a verification code in the C # File Creation along with the file transfer. 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.
C # create a file application 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. Below is the specific implementationCode:
- Filestream Fst =NewFilestream (
-
- 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.
C # create a file 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;
-
- ......
C # create a file 3. Convert the verification code string
Because MD5 HashAlgorithmThe returned data is a byte. Therefore, you must convert the data to a string. The following is the specific implementation code.
-
- ......
-
-
-
- Byte[] Hash = md5.hash;
-
-
-
- Stringbuilder sb =NewStringbuilder ();
-
-
-
- Foreach(ByteBytInHash)
-
-
-
- {
-
-
- 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.