Notes from Daily Encounters with Technology RSS 2.0
 
# Saturday, November 24, 2007

In my opinion RSACryptoServiceProvider class is seriously under-documented in MSDN. Since there is also no abundance of examples on the web, I spent more time than I should figuring out how to use it correctly. For future reference I'm listing below the solution to two problems I had.

The maximum byte array length for encrypting without OAEP padding is Modulus size - 11 which is written somewhere in the Encrypt() method documentation. If you pass it a larger array it will return a not so informative Unspecified error. To encrypt a larger chunk of data you have to split it in smaller parts, encrypt them individually and concatenate them back together. You have to do the same when decrypting the data, with the only difference that each part has the size of Modulus in stead of Modulus - 11. To get the modulus size you can use the following piece of code (rsa is an instance of RSACryptoServiceProvider):

RSAParameters rsaParams = rsa.ExportParameters(false);
int modulusSize = rsaParams.Modulus.Length;

Each time you instantiate RSACryptoServiceProvider it generates a new pair of keys. If you want to use existing ones, you can import them by calling:

rsa.FromXmlString(key);

The key parameter is a string with the XML representation of the keys. You can get it by calling the ToXmlString() method once and storing its results. It's only parameter specifies whether to also export the private key. I guess I don't have to remind you that you need the private key only for decryption and that you should always keep it private for the encryption to make any sense at all.

Saturday, November 24, 2007 5:31:53 PM (Central European Standard Time, UTC+01:00)  #    Comments [0] - Trackback
Development | .NET
Sponsored Ads

About Me

Damir Arh

Microsoft Certified Professional

View Damir Arh's profile on LinkedIn

Profile for ExAmigan

ExAmigan

Twitter
On my way to Celje today I've seen parhelion (sundog) for the first time http://imgur.com/NssST http://digs.by/dm1TUQ 13 hours ago
How to setup WinMerge as compare tool in Total Commander: http://digs.by/bquumI 2 days ago
Damir's Corner: EventLogTraceListener Can Cause an Application to Crash http://goo.gl/fb/bxEGC 2 days ago
How to debug .NET Framework Source in VS2010 http://digs.by/dr382h 2 days ago
And a list of SQL Server Agent stored procedures for editing data http://digs.by/ch701Z 2 days ago
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

All Content © 2010, Damir Arh, M. Sc. Send mail to the author(s) - Privacy Policy - Sign In
Based on DasBlog theme 'Business' created by Christoph De Baene (delarou)