We have a TCP/IP sockets server written in C# used for transferring binary files to clients. e.g., clips, images. Asynchronous BeginSend/EndSend with callbacks are being utilized to send byte[] buffers.
New requirement is to encrypt the data being transferred. Each client connection will supply an encryption key for the server to use. Actual encryption algorithm is not as essential, i.e., the goal is to simply ensure that the data is not sent in the clear. Even RC2CryptoServiceProvider with 40-bit keys would suffice... RijndaelManaged with 128-bit keys is an overkill and rather CPU-intensive in comparison with RC2.
It is certainly possible to first generate an encrypted version of data files before transferring them. Ideally though, we should encrypt the file on the fly as the data is read from file and sent on the socket. Given the size of data files, reading entire file contents to memory is neither efficient nor scalable.
Are there a few good patterns to follow when encrypting data from files on the fly to send to a sockets peer?