During file encryption and decryption over the past two days,
"Incorrect data" and "invalid data length to be decrypted" are abnormal and worried. I haven't understood the specific problem till now. If any of you can give me some advice, we are very grateful.
However, the problem was finally solved, and the reading and writing methods during encryption and decryption were changed. Initially, file stream operations were performed directly, last night, they were modified to directly operate the memory stream. Here is the operation Composition Component stream and operation memory stream Code Comparison:
Directly operate on the file stream-encryption:
ProgramCode: |
/// <Summary> /// File encryption /// </Summary> /// <Param name = "infilename"> file to be encrypted (full path of the file) </param> /// <Param name = "outfilename"> encrypted file (full file path) </param> /// <Param name = "salgorithm"> symmetric Algorithm Instance </param> /// <Returns> bool </returns> Private bool encryptfile (string infilename, string outfilename, symmetricalgorithm salgorithm) { Try { // Create an encrypted file stream Filestream infilestream = new filestream (infilename, filemode. Open, fileaccess. Read ); Filestream outfilestream = new filestream (outfilename, filemode. openorcreate, fileaccess. Write ); // Lock the encrypted file Infilestream. Lock (0, infilestream. Length ); // Set the initial length of the encrypted file Outfilestream. setlength (0 ); Long filelength = infilestream. length; // The total length of the object's byte array Byte [] bytein = new byte [100]; // read the file to be encrypted in 100 bytes each time Long readedlen = 0; // records the location of the read bytes Int Len = 0; // The length of each written byte Cryptostream encstream = new cryptostream (outfilestream, salgorithm. createencryptor (this. keyValue, this. ivvalue), cryptostreammode. Write ); Try { While (readedlen <filelength) { Len = infilestream. Read (bytein, 0,100); // read 100 bytes of array data from the input stream to the bytein Buffer Encstream. Write (bytes, 0,100 ); Readedlen = readedlen + Len; } Outfilestream. Flush (); Infilestream. Flush (); Encstream. Flush (); Return true; } Catch (Exception error) { Throw (error ); } Finally { Infilestream. Unlock (0, infilestream. Length ); Encstream. Close (); Infilestream. Close (); Outfilestream. Close (); } } Catch (Exception error) { Throw (error ); } } |
The above file encryption method has no problem during encryption, but with the same modification, the following is the corresponding decryption method:
Program code: |
/// <Summary> /// File decryption /// </Summary> /// <Param name = "infilename"> file to be decrypted (full path of the file) </param> /// <Param name = "outfilename"> decrypted file (full file path) </param> /// <Param name = "salgorithm"> symmetric algorithm instance </param> /// <Returns> bool </returns> Public bool decryptfile (string infilename, string outfilename, symmetricalgorithm salgorithm) { Try { // Create a decryption file stream Filestream infilestream = new filestream (infilename, filemode. Open, fileaccess. Read ); Filestream outfilestream = new filestream (outfilename, filemode. openorcreate, fileaccess. Write ); // Set the initial length of the encrypted file Outfilestream. setlength (0 ); Long filelength = infilestream. length; // The total length of the object's byte array Byte [] bytein = new byte [100]; // read the file to be encrypted in 100 bytes each time Long readedlen = 0; // records the location of the read bytes Int Len = 0; // The length of each written byte Cryptostream encstream = new cryptostream (outfilestream, salgorithm. createdecryptor (this. keyValue, this. ivvalue), cryptostreammode. Write ); Try { While (readedlen <filelength) { Len = infilestream. Read (bytein, 0,100); // read 100 bytes of array data from the input stream to the bytein Buffer Encstream. Write (bytes, 0,100 ); Readedlen = readedlen + Len; } Outfilestream. Flush (); Infilestream. Flush (); Encstream. flushfinalblock (); // when this clause is added, an exception may occur sometimes. Return true; } Catch (Exception error) { Throw (error ); } Finally { Encstream. Close (); // if this sentence is added, an exception may occur sometimes. Infilestream. Close (); Outfilestream. Close (); } } Catch (Exception error) { Throw (error ); } } |
In the above decryption method, the two lines of code marked in red will prompt the two exceptions I mentioned above in some cases, the only thing I can understand a little now is that the above exception may occur when I decrypt the data immediately after encryption, because I checked a lot of information and did not prompt the occurrence of these two exceptions. The following code changes my encryption and decryption method to the memory stream operation. The following code can be executed normally.
Encryption:
Program code: |
/// <Summary> /// File encryption /// </Summary> /// <Param name = "infilename"> file to be encrypted (full path of the file) </param> /// <Param name = "outfilename"> encrypted file (full file path) </param> /// <Param name = "salgorithm"> symmetric algorithm instance </param> /// <Returns> bool </returns> Private bool encryptfile (string infilename, string outfilename, symmetricalgorithm salgorithm) { // Read the file content to the byte array Filestream infilestream = new filestream (infilename, filemode. Open, fileaccess. Read ); Byte [] sourcebyte = new byte [infilestream. Length]; Infilestream. Read (sourcebyte, 0, sourcebyte. Length ); Infilestream. Flush (); Infilestream. Close (); Memorystream encryptstream = new memorystream (); Cryptostream encstream = new cryptostream (encryptstream, salgorithm. createencryptor (), cryptostreammode. Write ); Try { // Use the link stream to encrypt the Source byte array Encstream. Write (sourcebyte, 0, sourcebyte. Length ); Encstream. flushfinalblock (); // Write the byte array information to the specified file Filestream outfilestream = new filestream (outfilename, filemode. openorcreate, fileaccess. Write ); Binarywriter bwriter = new binarywriter (outfilestream ); Bwriter. Write (encryptstream. toarray ()); Encryptstream. Flush (); Bwriter. Close (); Encryptstream. Close (); } Catch (Exception error) { Throw (error ); } Finally { Encryptstream. Close (); Encstream. Close (); } Return true; } |
Corresponding decryption method:
Program code: |
/// <Summary> /// File decryption /// </Summary> /// <Param name = "infilename"> file to be decrypted (full path of the file) </param> /// <Param name = "outfilename"> decrypted file (full file path) </param> /// <Param name = "salgorithm"> symmetric algorithm instance </param> /// <Returns> bool </returns> Public bool decryptfile (string infilename, string outfilename, symmetricalgorithm salgorithm) { // Read the encrypted file to the byte array Filestream encryptfilestream = new filestream (infilename, filemode. Open, fileaccess. Read ); Byte [] encryptbyte = new byte [encryptfilestream. Length]; Encryptfilestream. Read (encryptbyte, 0, encryptbyte. Length ); Encryptfilestream. Flush (); Encryptfilestream. Close ();
Memorystream decryptstream = new memorystream (); Cryptostream encstream = new cryptostream (decryptstream, salgorithm. createdecryptor (), cryptostreammode. Write ); Try { Encstream. Write (encryptbyte, 0, encryptbyte. Length ); Encstream. flushfinalblock (); Byte [] decryptbyte = decryptstream. toarray (); Filestream decryptfilestream = new filestream (outfilename, filemode. openorcreate, fileaccess. Write ); Binarywriter bwriter = new binarywriter (decryptfilestream, encoding. getencoding ("gb18030 ")); Bwriter. Write (decryptbyte ); Decryptfilestream. Flush (); Bwriter. Close (); Decryptfilestream. Close (); } Catch (Exception error) { Throw (error ); } Finally { Decryptstream. Close (); Encstream. Close (); } Return true; } |