Sunday 28 June 2015

Encrypted and decrypted password code using asp.net with c#


<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
   
    </div>
        <table class="auto-style1">
            <tr>
                <td class="auto-style2">UserName</td>
                <td>
                    <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">PassWord</td>
                <td>
                    <asp:TextBox ID="txtPassWord" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td>
                    <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />
                    <asp:Label ID="lblMsg" runat="server" Text="Msg"></asp:Label>
                </td>
            </tr>
        </table>
        <asp:GridView ID="GridView2" runat="server" OnRowDataBound="GridView2_RowDataBound">
        </asp:GridView>
        <table class="auto-style3">
            <tr>
                <td class="auto-style4">UserName</td>
                <td>
                    <asp:TextBox ID="txtUserName1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style4">PassWord</td>
                <td>
                    <asp:TextBox ID="txtPassWord1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style4">&nbsp;</td>
                <td>
                    <asp:Button ID="btnLogin" runat="server" OnClick="btnLogin_Click" Text="Login" />
                    <asp:Label ID="lblShow" runat="server" Text="Show"></asp:Label>
                </td>
            </tr>
        </table>
    </form>
</body>


using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Text;

public partial class encyptshow : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sai"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            bindencypt();
            binddecrypt();
        }
    }
    protected void bindencypt()
    {
        SqlDataAdapter sdr = new SqlDataAdapter("select * from myencrypt", con);
        DataSet ds = new DataSet();
        sdr.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void binddecrypt()
    {
        SqlDataAdapter sdr = new SqlDataAdapter("select * from myencrypt", con);
        DataSet ds = new DataSet();
        sdr.Fill(ds);
        GridView2.DataSource = ds;
        GridView2.DataBind();
    }
    private string Encryptdata(string password)
    {
        string strmsg = string.Empty;
        byte[] encode = new byte[password.Length];
        encode = Encoding.UTF8.GetBytes(password);
        strmsg = Convert.ToBase64String(encode);
        return strmsg;


    }
    private string Decryptdata(string encryptpwd)
    {
        string decryptpwd = string.Empty;
        UTF8Encoding encodepwd = new UTF8Encoding();
        Decoder Decode = encodepwd.GetDecoder();
        byte[] todecode_byte = Convert.FromBase64String(encryptpwd);
        int charcount = Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
        char[] decoded_char = new char[charcount];
        Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
        decryptpwd = new string(decoded_char);
        return decryptpwd;


    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string mypassword = Encryptdata(txtPassWord.Text);
        con.Open();
        string ins = "Insert into myencrypt(UserName,PassWord)values('" + txtUserName.Text + "', '" + mypassword + "')";
        SqlCommand cmd = new SqlCommand(ins, con);
        cmd.ExecuteNonQuery();
        con.Close();
        bindencypt();
        binddecrypt();
    }
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string decryptpassword = e.Row.Cells[1].Text;
            e.Row.Cells[1].Text = Decryptdata(decryptpassword);
        }
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    string decryptpassword = e.Row.Cells[1].Text;
        //    e.Row.Cells[1].Text = Decryptdata(decryptpassword);
        //}
     
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        string sanpassword = Decryptdata(txtPassWord1.Text);
        SqlCommand cmd1 = new SqlCommand("select count(*) from myencrypt where UserName='" + txtUserName1.Text + "' and PassWord='" + sanpassword + "'", con);
        con.Open();
        int countit = Convert.ToInt32(cmd1.ExecuteScalar());
        if (countit > 0)
        {
            Response.Redirect("modaledit.aspx");
        }
        else
        {
            lblShow.Text = "Invalid username and password";
        }
        con.Close();
       // Response.Redirect("modaledit.aspx");
    }
}

No comments:

Post a Comment