Encrypting data from file and sending on a socket via MemoryStream( byte[] ) using C# / .Net -


we have sockets server needs transfer encrypted binary files connected clients. files need encrypted when requests them arrive, preferably without making encrypted copies of files on disk first.

due fact use asynchronous sockets api, sockets.networkstream cannot used. instead, need utilize socket.beginsend() byte[] buffer input.

a seemingly straight forward possibility use cryptostream memorystream destination encrypted content. memorystream use byte [] buffer own data repository.

for example:

int blocksizebytes = 1024 * 64; byte [] plaindata = new byte [blocksizebytes]; byte [] encdata = new byte [blocksizebytes]; memorystream memory = new memorystream( encdata );  icryptotransform encryptor = ... ***cryptoserviceprovider.createencryptor(); cryptostream csenc = new cryptostream( memory, encryptor, cryptostreammode.write ); 

a file can read , encrypted buffer @ time, sent on socket client. example:

socket clientsocket = ...; // connected peer. int bytesread = 0; filestream streamin = new filestream( strinputfile, filemode.open ); {     bytesread = streamin.read( plaindata , 0, blocksizebytes );     if (bytesread > 0)     {         csenc.write( plaindata , 0, bytesread );   // write crypto stream          // @ point underlying byte array encdata hold         // encrypted buffer of data.          // ideally send encdata buffer on socket client via          // following pseudo-code:          // 1) how can determine precise number of encoded bytes send?         clientsocket.beginsend( encdata, 0, bytesread, ... /* other params */ );          ...          memory.seek( 0, seekorigin.begin );  // reset memory stream start     } } while (bytesread > 0);  streamin.close();  // close intput file stream  outenc.flushfinalblock();  // deal padding, etc.  // send final buffer necessary padding client. // 2) again, how can determine exact number of encoded bytes send? clientsocket.beginsend( encdata, 0, ??how-many-bytes??, ... );  outenc.close(); 

for testing purposes wrote out encoded buffers file instead of socket. when trying decrypt resulting file, following exception thrown: cryptographicexception: length of data decrypt invalid.

as can seen in items 1) , 2) above, don't know precise number of encoded bytes transfer client (or save file). tried memory.position buffer size after flushfinalblock(), though without success.

please note, when cryptostream using filestream output, i.e., neither memorystream nor byte[] buffers used, resulting file encrypted , subsequently decrypted successfully. our goal, though, able write out encrypted byte[] buffers directly without output streams.

if memorystream byte[] buffer not feasible, there other alternatives able incrementally encrypt buffers , forward them on, e.g., socket?

i've tested using memory.position count parameter in both clientsocket.beginsend(...) methods, , able round-trip (i.e. encrypt decrypt encrypted data) successfully. if isn't working you, it'd worth providing code complete, self-contained, compilable example demonstrates issue.


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -