Windows Develop Bookmark and Share   
 index > Windows Forms General > RSACryptoServiceProvider encryption
 

RSACryptoServiceProvider encryption

Hi. I believe that in Asymmetric encryption if i encrypt data using the private key, it can only be decrypted using the public key. However i also believe the inverse is possible. If the public key is used for encryption, it can be decrypted using the private key. But i am unable to specify in the RSACryptoserviceProvider object, which key is to be used for encryption and which one for decryption. Can someone help me achieve this here.

They keys are being obtained from a X509Certificate2 object. but am not able to encrypt using public key and decrypt using private key.

Please help.
krarun  Tuesday, September 08, 2009 9:03 AM

Hi krarun,

RSACryptoServiceProvider only supports public key encryption and private key decryption. If we want to use private key to encrypt and public key to decrypt, we have to write our own program to implement it. This is an article about RSA Private Key Encryption:

http://www.codeproject.com/KB/security/PrivateEncryption.aspx.

Let me know if this does not help.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, September 14, 2009 8:06 AM
Hi krarun,

You said I believe that in Asymmetric encryption if i encrypt data using the private key, it can only be decrypted using the public key. However i also believe the inverse is possible. If the public key is used for encryption, it can be decrypted using the private key.
Reply: Yes, you are right.

You said which key is to be used for encryption and which one for decryption.
Reply: You can choose a key to encrypt and the other key to decrypt according to your issues. This is a rule: The public key can be made public to anyone, while the private key must known only by the party who will decrypt the data encrypted with the public key. With RSA algorithm, we often use the public key to encrypt data and use the private key to decrypt data.

You said They keys are being obtained from a X509Certificate2 object. but am not able to encrypt using public key and decrypt using private key.
This is a sample shows how to use RSACryptoServiceProvider:
http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.aspx.


This documenttalks about Generating Keys for Encryption and Decryption: http://msdn.microsoft.com/en-us/library/5e9ft273.aspx.
This document shows how to Store Asymmetric Keys in a Key Container: http://msdn.microsoft.com/en-us/library/tswxhw92.aspx.
This is a sample about Public Key RSA Encryption in C# .NET: http://www.codeproject.com/KB/security/RSACryptoPad.aspx.

By the way, we often use RSA to encrypt/decrypt the key, not the data. Because the performance of the RSAalgorithm is low. To encrypt/decrypt data, we often use symmetric algorithms, such as Rijndael.
This document is about encrypt/decrypt data including both symmetric and asymmetric algorithms:
http://msdn.microsoft.com/en-us/library/e970bs09.aspx.

Let me know if this does not help.
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
  • Unmarked As Answer bykrarun Monday, September 14, 2009 6:58 AM
  • Marked As Answer bykrarun Monday, September 14, 2009 6:55 AM
  •  
Aland Li  Thursday, September 10, 2009 6:53 AM
Thanks Aland, that was quite helpful. And yes, what i a doing is encrypting a huge amount of data symmetrically and then encrypting the key using RSACryptoserviceprovider.

However, the main issue is that the RSACryptoServiceProvider.Decrypt() method uses the private key but the other end of my application that is written in java also encrypts it using the private key. Is there no way to specify to the decrypt method that it is to use the public key and not the private one for the decryption process.
krarun  Monday, September 14, 2009 6:58 AM

Hi krarun,

RSACryptoServiceProvider only supports public key encryption and private key decryption. If we want to use private key to encrypt and public key to decrypt, we have to write our own program to implement it. This is an article about RSA Private Key Encryption:

http://www.codeproject.com/KB/security/PrivateEncryption.aspx.

Let me know if this does not help.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, September 14, 2009 8:06 AM

You can use google to search for other answers

Custom Search

More Threads

• Listview - text wrapper
• A little trouble with custom controls
• Windows XP Visual Style
• Problem in saving/select Rich Text in MySql Database.
• WebBrowser window.external
• Can winforms apps be developed for a client/server architecture?
• Force the use of XP theme in app.
• ActiveX control issues
• when does control.created property get set to True
• WMPOCX.OCX help about URL property