Public Key Encryption (RSA Method) for Encryption and Decryption in ASP.Net


Description:-

In this article we will learn how use Public Key Encryption for Encryption and Decryption in ASP.Net. We already know about DES algorithm method. The RSA Public Key Encryption is useful method for Encryption and Decryption .The Code behind the Method is mention below.

Code:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Text;

namespace GridApplication
{
    public partial class Public_Key_Encryption : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Encryption();
            }
        }
        public void Encryption()
        {
            RSACryptoServiceProvider myRSAProvide = new RSACryptoServiceProvider();
            string strCrypt = null;
            byte[] bteCrypt = null;
            byte[] bteResult = null;
            try
            {
                strCrypt = "12345678";
                bteCrypt = Encoding.ASCII.GetBytes(strCrypt);
                bteResult = myRSAProvide.Encrypt(bteCrypt, false);
                Label1.Text = Encoding.ASCII.GetString(bteResult);
            }
            catch (CryptographicException ex)
            {
                Label3.Text = ex.Message;
            }
            string strResault = null;
            byte[] bteDecrypt = null;
            try
            {
                bteDecrypt = myRSAProvide.Decrypt(bteResult, false);
                strResault = Encoding.ASCII.GetString(bteDecrypt);
                Label2.Text = strResault;
            }
            catch (CryptographicException ex)
            {
                Label3.Text = ex.Message;
            }
        }
    }
}

Output:-

Related Posts

Previous
Next Post »

Thanks for comments.....