last day (16 days later) » 

5:11 PM
1
A: How do I avoid timeout when using BinaryReader and BinaryWriter on the same CryptoStream?

usrYou can't flush a block cipher in the middle of a block because those bytes are not ready from an algorithmic standpoint. They are not determined. Probably, the best fix is to use a stream cipher. .NET has poor built-in support for that. Pull in a library that implements AES in counter mode. Be...

 
The symmetric keys are somewhat temporary and hidden behind a pre-shared asymmetric keys. I'm not a security expert, I don't know if it's vulnerable or not.
What do you think of this implementation? gist.github.com/hanswolff/8809275
 
usr
I'm not qualified to audit this code. I'd pick the code from the web that seems to be used most often... Thankfully counter mode is not hard to implement. For AES-GCM there is a solid implementation that seems to come from Microsoft.
 
Now I've tried both RijndaelManaged, setting its cipher mode to CFB and feedback size to 8 bits, and I've tried AES128 Counter mode... safe or not, they both had the same characteristics in my program. The first message gets through, the second doesn't. Maybe it's more related to BinaryReader than actual encryption?
 
usr
OK, the encryption issue was definitively a problem that needed to be solved. There is another. Something in the chain is not flushed. Can you post more complete code? You also can use the debugger to peek into object internals and discover data that is not flushed.
 
Ah! Some new information.... the first message doesn't actually get through... until the sender closes the stream due to timeout.... that's when the first message actually gets through.
 
usr
5:11 PM
Maybe your socket code is broken. Most socket code is broken... Post more.
 
Code posted.... the 'New' message gets through, when the client's ReadString hits its timeout. I guess 'New' gets through because the stream is closed at that time.
Went back to RijndaelManaged, FeedbackSize 8, Mode CFB, Padding None (no padding is new). Now I get this: "CryptographicException: invalid block length"
 
usr
Using a block cipher cannot work because it buffers data by nature. Btw, why are you sending the key in the clear? That makes the encryption useless.
That comment was not quite accurate. The problem is not the block cipher, it is the blocking mode of operation.
 
I'm actually trying to find a usable stream cipher. According to this thread, stackoverflow.com/questions/11776227/c-sharp-aes-128-cfb-error, Rijndael should work as a stream cipher in this case. I also thought that the AES block mode implementation would suffice, but it was incomplete, and dind't allow 'ReadString' to return immediately as I had hoped.
No, the key is not in the clear, even in this example.
 
usr
Why are you still working on finding an algorithm? You already had a working one: AES in CTR mode. Not sure what properties CFB with feedbacksize 8 has. That sounds dangerous. There's probably a reason the feedback size normally is much higher.
 
When I first tried the code that I linked here, it wasn't working because CryptoStream was expecting an IV etc. etc. And when I had patched it up to run.... it had the same issue as the others. But I don't mind making another go at it. That is.... this code here gist.github.com/hanswolff/8809275
 
usr
5:11 PM
That's because there are two bugs: The blocking cipher and something else.
 
Ok, I'll be digging a little deeper. Thanks so far!
I don't know quite what to look for, but I managed to replicate the exact same situation in a new, small c# project fredrik1.com:22080/download_res/CryptoStreamDebug.zip
 
usr
that helps
i found out that if you reduce the length of the string that the client sends by one char it kind of works
there is still buffering going on
ok the transform is broken. it reports a block size > 1 but it really is 1. use:
public int InputBlockSize { get { return 1; } }
public int OutputBlockSize { get { return 1; } }
ctr mode has blocksize 1
cryptostream apparently buffers up to the block size
 
 
3 hours later…
8:31 PM
I could have sworn that I tried this earlier... but I can see now that it's working in the example project. Thanks for pointing it out :-)
 
usr
9:12 PM
sounds good
are there any problems left? i lost track
 
9:56 PM
I need to try it out in the boo project tomorrow... and it seems that the IDisposable implementation was a bit faulty, even for my use.
But yeah... it seems like this could be the way forward :-)
Happy that I didn't have to resolve to use one MemoryStream per message.
 

  last day (16 days later) »