"Incorrect data" encountered when encrypts decryption, and "Data length to decrypt" problem solution

xiaoxiao2021-04-03  218

These two days have been decrypted for file encryption, due to shutting down data conversion link stream CryptostReam or using FlushFinalBlock

"Incorrect data" and "The data to decrypt" is very annoyed, and now I don't understand what is the problem. If a high person can give pointers, I am grateful.

However, the problem is finally solved, and the way to change the encryption and decryption of reading and writing is solved. It was originally directly on the file flow, and he modified them to direct traffic flow last night. Below is the comparison of the operation of the file stream and the operating memory code:

Direct operation of file stream --- Encryption:

code:

///

/// file encryption

///

/// Required files (full path to files)

/// encrypted file (full path to the file)

/// Symmetric algorithm instance

/// BOOL

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 file after encryption

Outfilestream.SetLength (0);

Long filelength = infilestream.length; // Total byte array length

Byte [] bytein = new byte [100]; // read the file that needs to be encrypted by 100 bytes

Long readledlen = 0; // Record the byte position that has been read

INT LEN = 0; / / // Write the byte length of each time

Cryptostream EncStream = New CryptostReam (Outfilestream, Salgorithm.createEncryptor (this.keyvalue, this.ivvalue), cryptostreammode.write;

Try

{

While (Readedlen

{

Len = InfileStream.read (Bytein, 0, 100); // Read 100 bytes of array data from the input stream to the Bytein buffer

EncStream.write (bytein, 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 does not have any problems at the time of encryption, but the same, slightly modified, the following is the corresponding decryption method:

code:

///

/// file decryption

///

/// Required files (full path to files)

/// Decrypted file (full path to the file)

/// Symmetric algorithm instance

/// BOOL

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 file after encryption

Outfilestream.SetLength (0);

Long filelength = infilestream.length; // Total byte array length

Byte [] bytein = new byte [100]; // read the file that needs to be encrypted by 100 bytes

Long readledlen = 0; // Record the byte position that has been read

INT LEN = 0; / / // Write the byte length of each time

Cryptostream EncStream = New CryptostReam (Outfilestream, Salgorithm.createDecryptor (this.keyvalue, this.ivvalue), cryptostreammode.write;

Try

{

While (Readedlen

{

Len = InfileStream.read (Bytein, 0, 100); // Read 100 bytes of array data from the input stream to the Bytein buffer

EncStream.write (bytein, 0, 100);

Readedlen = Readedlen Len;

}

OutfileStream.flush ();

InfileStream.flush ();

ENCSTream.flushfinalBlock (); // Add this sentence, sometimes there will be abnormalities, don't know why

Return True;

}

Catch (Exception Error)

{

Throw (Error);

}

Finally

{

EncStream.close (); // Add this sentence, sometimes there will be abnormalities, don't know why INFILESTREAM.CLOSE ();

OutfileStream.Close ();

}

}

Catch (Exception Error)

{

Throw (Error);

}

}

In the decryption method of the above decryption, the two lines of red color identifies, in some cases, it will prompt me to say above, and now I can have a little understanding now because I immediately decrypt it, I will appear above. The abnormal situation, because I didn't prompt these two anomalies without prompting many data. Below is the code I encrypted decryption, I changed to the code of the operating memory, the following code can be implemented normally.

encryption:

code:

///

/// file encryption

///

/// Required files (full path to files)

/// encrypted file (full path to the file)

/// Symmetric algorithm instance

/// BOOL

Private Bool Encryptfile (String InfileName, String Outfilename, Symmetricalgorithm Salgorithm)

{

// read the contents of the file to byte arrays

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 encryption source byhenometer

Encstream.write (Sourcebyte, 0, Sourcebyte.Length);

ENCSTream.flushfinalBlock ();

// Write 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:

///

/// file decryption

///

/// Required files (full path to files)

/// Decrypted file (full path to the file)

/// Symmetric algorithm instance

/// BOOL

Public Bool Decryptfile (String InfileName, String Outfilename, Symmetricalgorithm Salgorithm)

{

// Read the encrypted file to byte arrays

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;

}

转载请注明原文地址:https://www.9cbs.com/read-131594.html

New Post(0)