Encryption and Decryption usinf RSACryptoServiceProvider RRS feed

  • Question

  • User741259431 posted

    I would like to make a asp.net website using RSACryptoService where in:

    1. client encrypts the data using Server's public key
    2. the server decrypts the data using the server's private key.
    3. the server then returns some other data by encrypting it with the client's public key.
    4. client decrypts it using its private key.

    I have a few queries as to how to go about it.

    Firstly, how do i exchange the public key information between the client and the server. This is the code is used:

    //Code in client.
    public partial class LoginPage : System.Web.UI.Page
        string ServerKeyInfo;
        string ClientKeyInfo;
        RSACryptoServiceProvider ClientRSA = new RSACryptoServiceProvider();
        RSACryptoServiceProvider ServerRSA = new RSACryptoServiceProvider();
        WebService.WebService WebServer = new WebService.WebService();
        protected void Page_Load(object sender, EventArgs e)
            this.ClientKeyInfo = ClientRSA.ToXmlString(true);
            //Call Webservice passing the client's public key as a parameter and get the public key of the server
            this.ServerKeyInfo = WebServer.ExchangeKeyInfo(ClientKeyInfo);
            Label2.Text = ClientRSA.ToXmlString(false);
            Label1.Text = ServerRSA.ToXmlString(false);
    //WebService Code
    public class WebService : System.Web.Services.WebService {
        BackEndCode.BackEndCode Server = new BackEndCode.BackEndCode();
        public WebService () {
            //Uncomment the following line if using designed components 
        public string ExchangeKeyInfo(string ClientKeyInfo) {
            //Call the backend code to exchange the keys
            string ServerKeyInfo = Server.ExchangeKeyInfo(ClientKeyInfo);
            return ServerKeyInfo;
    //Backend Code in the Server
    public class BackEndCode
            string ServerKeyInfo;
            string ClientKeyInfo;
            RSACryptoServiceProvider ServerRSA;
            RSACryptoServiceProvider ClientRSA;
            public BackEndCode()
                ServerRSA = new RSACryptoServiceProvider();
                this.ServerKeyInfo = ServerRSA.ToXmlString(true);
                ClientRSA = new RSACryptoServiceProvider();
            private string opValidationSplChars(string Value)
                Value = Value.Replace("'", "").Replace(";", "");
                return Value;
            public string ExchangeKeyInfo(string ClientKeyInfo)
                this.ClientKeyInfo = ClientKeyInfo;
                string ServerKeyInfo;
                ServerKeyInfo = ServerRSA.ToXmlString(false);
                return ServerKeyInfo;

    But when i use it to decrypt i get a bad data Error.

    Friday, April 25, 2014 1:50 AM

All replies

  • User-734925760 posted


    So far as I know, if we want to decrypt data using private key and encrypt data with public key. please refer to the code below:

    string secret = "My secret message";
    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(512);  // Key bits length
    string key = "private or public key as xml string";
    byte[] secretData = Encoding.UTF8.GetBytes(secret);
    byte[] encrypted = rsa.PrivareEncryption(secretData);
    byte[] decrypted = rsa.PublicDecryption(encrypted);
    string decString = Encoding.UTF8.GetString(decrypted);  // And back to string
    Assert.AreEqual("My secret message", decString);

    There is an artical about this requirement, please refer to the link below:


    The same time hope you can provide more information about the error.

    Hope it's useful for you.

    Best Regards,

    Michelle Ge

    Sunday, April 27, 2014 11:06 PM