Encryption and Decryption coding in c#

Simple Way to make encryption and decryption in c#:

Use this steps:
1.First of all drag all this components and give name to that components for clear identification

2. Now make one class to make for both encryption and decryption do this like below..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EncryptDecrypt
{
    class encyptdecypt
    {
        public string org;
        public string enc;
        public string dec;
        public string encrpt(string org)
        {
            char[] chararray = org.ToCharArray();
            for (int i = 0; i <= chararray.Length - 1; i++)
            {
                int orgtxt = (int)chararray[i];
                if (orgtxt >= 97 && orgtxt <= 109)
                {
                    orgtxt = orgtxt + 13;
                }
                else if (orgtxt >= 110 && orgtxt <= 122)
                {
                    orgtxt = orgtxt - 13;
                }
                else if (orgtxt >= 65 && orgtxt <= 77)
                {
                    orgtxt = orgtxt + 13;
                }
                else if (orgtxt >= 78 && orgtxt <= 90)
                {
                    orgtxt = orgtxt -13;
                }
                else if (orgtxt >= 48 && orgtxt <= 52)
                {
                    orgtxt = orgtxt + 5;
                }
                else if (orgtxt >= 53 && orgtxt <= 58)
                {
                    orgtxt = orgtxt -5;
                }

                enc = enc + (char)orgtxt;

            }
            return enc;
        }
    }
}

it will work like this from we will add 13 to every alphabet which is under 13 count in alphabets and from above we will substract to 13 similarly by number add 5  and substract 5 ok..


3.Now you have to code in button click event so double click on button and use this code and remember that it is a window base application if you are trying this to web this MessageBox.show()
will now work their so just see the code carefully.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace EncryptDecrypt
{
    public partial class Form1 : Form
    {
        SqlConnection con;
        SqlCommand cmd;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            con =new  SqlConnection(@"Data Source=VAIO\SQLEXPRESS;Initial Catalog=Studentreg;Integrated Security=True");
            con.Open();
            MessageBox.Show("connected");
        }

        private void btnsubmit_Click(object sender, EventArgs e)
        {
            encyptdecypt ed = new encyptdecypt();
            string pwd = ed.encrpt(txtpwd.Text);
            string q1="insert into login values('"+txtid.Text+"','"+txtname.Text+"','"+txtusrid.Text+"','"+pwd+"')";
            cmd = new SqlCommand(q1, con);
            cmd.ExecuteNonQuery();
            MessageBox.Show("1.row inserted");
         //   label5.Text = pwd;

        }
    }
}

now here i am using database connectivity so that so be care full when you are using this code use
you "datasource" in formload 
so enjoy the code.............bye


No comments:

Post a Comment