✅ Stream assistance needed
Alright I really need I'm completely lost. I have a file containing the following: 1 versioning byte used for retro-compatibility purposes, a 16 bytes long IV, X bytes of data and 64 bytes-long HMAC, everything in this exact order. Encryption is not the problem, decryption is. Since I used streams for better performances and less memory allocations than arrays, I read things in order, first the versioning byte to determine which method to use, then the IV to calculate the key and then problems are here. Since the HMAC is at the end, I need to decrypt the X following bytes = the rest of bytes - the 64 last. And I just can't. I tried using .SetLength on a new MemoryStream, also tried .Capacity, nothing is working. I am thus requesting your help.
9 Replies
here is the code in his current state:
Forgot to mention that since those streams can be linked to rather huge files, I try to avoid as much as possible .Seek
I guess the best workaround I could think of, is, when ecnrypting, to put the HMAC before the X bytes of data
but again that would require wizardry
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
If your code is too long, post it to: https://paste.mod.gg/here is the code but better
BlazeBin - pzyjeujzdazm
A tool for sharing your source code with the world!
Maybe it's a naive suggestion, but if the file isn't large, can't you read X bytes into a MemoryStream and then use it as input for CryptoStream?
Also, why isn't it possible to store the HMAC at the start of the file?
Alright thanks for your suggestions the first one might be very smart and could work, I'll try that tomorrow
For now it's 😴 time
would need some trickery since it's calculated from the ciphered data, so when it's done calculating, all the ciphered data is already written to the stream
if you need next data to decrypt previous data, you need to buffer some data, that's basically it
not sure to undertsna dhwta you jsut said
alright due to a change in design I did calculate the HMAC over the plain text data and thus making me able to append it before the encryprted payload, thanks to some stream manipulations inpired by @Kouhai's idea, I managed to sucessfully solve the problem.
Thank you @AntonC and Kouhai for helping me :)