Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well I solved the issue.</p> <p>It turns out I was sending in the message data the <code>chunkLength</code> of the encrypted chunk data. So for every chunk I sent, even though I decrypted and wrote the correct filedata, I was advancing the stream position by the length of the encrypted data. This means every time I decrypted, when transferring more than 1 chunk(this is why the small files of only 1 chunk size didn't have problems) I was adding 16 bytes to the file size.</p> <p>People helping me probably wouldn't have been able to figure this out, because I didn't include all of the data in the client side or the server side to see this. But thankfully I managed to answer it myself.</p> <p>On the sender side, I was creating my FileMessage like this.</p> <pre><code> FileMessage message = new FileMessage(); message.FileMetaData = new FileMetaData(chunkedFile.MoreChunks, chunkedFile.ChunkLength, chunkedFile.CurrentPosition, iv); message.ChunkData = new MemoryStream(buffer); </code></pre> <p>If you see the second parameter of <code>FileMetaData</code> constructor, I'm passing in <code>chunkedFile.ChunkLength</code> which is supposed to be the length of the chunk. I was doing this on the encrypted chunk data, which resulted in sending the incorrect chunk length.</p> <p>The client on the other hand, was receiving this extra information. If you look near the end, you'll see the code <code>filePosition += message.FileMetaData.ChunkLength;</code>. I was using that erroneous <code>chunkLength</code> to advance the file position. It turns out that setting of the streamPosition was not even necessary.</p> <pre><code>using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(fileWritePath))) { writer.BaseStream.SetLength(0); while (moreChunks) { FileMessage message = hostChannel.ReceiveFile(); moreChunks = message.FileMetaData.MoreChunks; UpdateTotalBytesTransferred(message); writer.BaseStream.Position = filePosition; byte[] decryptedStream; // Copy the message stream out to a memory stream so we can work on it afterwards. using (var memoryStream = new MemoryStream()) { message.ChunkData.CopyTo(memoryStream); Debug.WriteLine("Received Encrypted buffer Length: " + memoryStream.Length); decryptedStream = cryptor.Decrypt(memoryStream.ToArray(), symmetricPrivateKey, message.FileMetaData.InitializationVector); Debug.WriteLine("Received Decrypted buffer Length: " + decryptedStream.Length); } writer.Write(decryptedStream); TotalBytesTransferred = message.FileMetaData.FilePosition; filePosition += message.FileMetaData.ChunkLength; } OnTransferComplete(this, EventArgs.Empty); StopSession(); } </code></pre> <p><strong>Such a simple bug, but one that wasn't leaping out at me quickly at all.</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload