Quantcast
Channel: Security
Viewing all articles
Browse latest Browse all 4737

Encryption and Decryption usinf RSACryptoServiceProvider

$
0
0
<div class="vote">0 down vote favorite</div> <div> <div class="post-text" itemprop="description">

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);
        ServerRSA.FromXmlString(ServerKeyInfo);
        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 
        //InitializeComponent(); 
    }

    [WebMethod]
    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;
            ClientRSA.FromXmlString(ClientKeyInfo);
            ServerKeyInfo = ServerRSA.ToXmlString(false);
            return ServerKeyInfo;
        }
}

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

</div> </div>

Viewing all articles
Browse latest Browse all 4737

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>