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.