Friday, August 5, 2011

Understanding the Asymmetric Encryption in console application.


The problem with secret keys is that you must exchange them over the Internet or a large network but prevent them from falling into the wrong hands. Any person who knows the secret key can decrypt the message. Using this knowledge, the person can impersonate the sender and issue a message to the receiver that masquerades as a secured message. A common way to solve this problem is to change the secret keys on a regular basis. Symmetric encryption does not provide a mechanism with which to manage the changing of secret keys; therefore, if you want to change keys, you must implement your own key management infrastructure.

You can use asymmetric encryption to solve these problems. Asymmetric encryption uses two related keys that are known as a key pair. The key pair consists of a public key and a private key. You make the public key freely available to anyone who may want to send you a message. You keep the private key secret so that only you know it. The public key and the private key are mathematically linked; data that is encrypted by using the public key can be decrypted only by using the private key, and data that is signed by using the private key can be verified only by using the public key. Anyone can use the public key to encrypt data to be sent to the keeper of the private key. Both keys are unique to the communication session. One problem with asymmetric encryption is that it is slower than symmetric encryption. It requires far more processing power to both encrypt and decrypt the content of the message.

Example of Asymmetric encryption:

1. Creating a windows application insert the two buttons, text, and label:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
namespace asymetricenc
{
    public partial class Form1 : Form
    {
        RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
        static byte[] encData;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //extract private key and public key
            string key = RSA.ToXmlString(true);
            FileStream fs = new FileStream("privatekey.xml", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            sw.Write(key);
            sw.Close();
            ////extract public key
            string key1 = RSA.ToXmlString(false);
            FileStream fs1 = new FileStream("publickey.xml", FileMode.Create);
            StreamWriter sw1 = new StreamWriter(fs1);
            sw1.Write(key1);
            sw1.Close();

        }

        private void button1_Click(object sender, EventArgs e)
        {
          
          
            FileStream fs = new FileStream("publickey.xml", FileMode.Open);
            StreamReader sr = new StreamReader(fs);
            string key = sr.ReadToEnd();
            sr.Close();
          
            RSA.FromXmlString(key);

            Byte[] datatoEnc = UnicodeEncoding.Default.GetBytes(textBox1.Text);
            foreach (byte b in datatoEnc)
            {
                label1.Text += b.ToString()+"  ";
            }
            encData = RSA.Encrypt(datatoEnc,false);
            label1.Text +="\n"+Encoding.Default.GetString(encData);
        
        }

        private void button2_Click(object sender, EventArgs e)
        {
            label1.Text = "";
            // Load an XML string with key information from a
            // file and set it on a new RSA algorithm object
            FileStream fs = new FileStream("privatekey.xml", FileMode.Open);
            StreamReader sr = new StreamReader(fs);
            string key = sr.ReadToEnd();
            sr.Close();
          
            RSA.FromXmlString(key);
            byte[] decdata = RSA.Decrypt(encData, false);
            label1.Text=Encoding.Default.GetString(decdata);

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}

Output is display below:

1. Click the encrypt button text is encrypt.


2. Then click decrypt button text is decrypt.









No comments:

Recent Posts