RSA encryption between .NET and Win32 made easier!

Posted by nhughes Tue, 31 May 2005 03:55:00 GMT

Today we have a Win32 client (written in C++ and COM) that will communicate over TCP/IP to our new .NET server-side code. The Win32 client won’t be ported to .NET anytime in the near future. So, the two code bases have to be compatible. This challenge is amplified by the fact the client uses a combination of asymmetric encryption (RSA) and symmetric encryption (RC2) to encrypt the data that will be sent over the socket. So, our .NET code must be able to decrypt that data and then encrypt the response to be sent back to the client.

This quickly became a challenge because the two platforms implement encryption very differently. In .NET, I ended up having to parse the RSA public key from the client. This process included parsing the RSA key header, the exponent and modulus out of the key blob to be read into the RSACryptoServiceProvider. Also, because the .NET default for byte ordering is big-endian and Win32 is little-endian, the exponent and modulus byte ordering had to be reversed before it could be read by .NET. This is quite a bit of overhead. Then, this whole process had to be done again (only in reverse) for creating a .NET key that a Win32 client can read. Suffice it to say, it took quite a bit of trial and error to get it all correct, and it was a big pain.

This is no longer necessary with Whidbey Beta 2!! There are new methods on the RSACryptoServiceProvider for importing and exporting the key blob (ImportCSPBlob & ExportCSPBlob). No more parsing, no more byte order reversing! It is a very good thing. I only wish it had been available earlier, so I didn’t spend so much time doing it the hard way.

Now, if only Microsoft would add this for the RC2CryptoServiceProvider!

Comments are disabled