First of All I like to mention that RSA has a limitation that it cannot Encrypt/Decrypt data which size is more than 117 byte. Check http://www.codeproject.com/useritems/SecureDataExchange.asp
Having that in your mind here is a simple example of RSA Encrypt/Decrypt:
string Encrypt(string publicKey, string plainString)
{
byte[] plainByte = System.Text.Encoding.Default.GetBytes(plainString); // Converting it to byte array
using(RSACryptoServiceProvider rsa = CreateRSA(publicKey))
{
byte[] cipherByte = rsa.Encrypt(plainByte, false); //passing false to support Windows 2000.
string cipherString = Convert.ToBase64String(cipherByte); // Converting to string
return cipherString;
}
}
string Encrypt(string privateKey, string cipherString)
{
byte[] cipherByte = Convert.FromBase64String(cipherString); // Converting it to byte array
using(RSACryptoServiceProvider rsa = CreateRSA(privateKey))
{
byte[] plainByte = rsa.Decrypt(cipherByte, false); //passing false to support Windows 2000.
string plainString = System.Text.Encoding.Default.GetString(plainByte); // Converting to string
return plainString;
}
}
RSACryptoServiceProvider CreateRSA(string key)
{
CspParameters parms = new CspParameters();
parms.Flags = CspProviderFlags.UseMachineKeyStore;
RSACryptoServiceProvider csp = new RSACryptoServiceProvider(parms);
csp.FromXmlString(key);
return csp;
}