Bharat  Bhushan

Bharat Bhushan

  • NA
  • 176
  • 158.6k

Problem to store encrypted data in database using asp.net

Jan 24 2011 12:15 AM

I am trying to store encrypted data in database. It stores correctly when i execute the query in sql server  like

CREATE TABLE login_details(uid integer,username varchar(10),password varbinary(100))
INSERT INTO  login_details(uid,username,password) VALUES(1,'smith',EncryptByPassPhrase('12','XXX'))
INSERT INTO  login_details(uid,username,password) VALUES(2,'kennal',EncryptByPassPhrase('12','YYY'))
INSERT INTO  login_details(uid,username,password) VALUES(3,'staurt',EncryptByPassPhrase('12','ZZZ'))
INSERT INTO  login_details(uid,username,password) VALUES(4,'bHARAT',EncryptByPassPhrase('12','BHARAT'))


But when i store using asp.net it create a problem. It stores only 1 character in database for eg: if i want to store

"hello" , it stores only first character 'h' not full string. below i am giving you  my complete code

html code

   <form id="form1" runat="server">
    <div>
   
        <table class="style1">
            <tr>
                <td class="style2">
                    Userid</td>
                <td>
                    <asp:TextBox ID="txt_uid" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Uname</td>
                <td>
                    <asp:TextBox ID="txt_uname" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Upass</td>
                <td>
                    <asp:TextBox ID="upass" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    <asp:Button ID="btn_submit" runat="server" onclick="btn_submit_Click"
                        Text="Submit" />
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
   
    </div>
    </form>


C# code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Globalization;
public partial class passwordencryption : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection();
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_submit_Click(object sender, EventArgs e)
    {
        con.ConnectionString = ConfigurationManager.ConnectionStrings["cnn"].ConnectionString;
        con.Open();


        SqlCommand cmd = new SqlCommand();
       // cmd.CommandText = "insert into login_details values(@uid,@username,(EncryptByPassPhrase @password))";
       // cmd.CommandText = " INSERT INTO  login_details  VALUES(@uid,@username,EncryptByPassPhrase('12',@password))";
       // cmd.CommandType = CommandType.Text;
        cmd.CommandText = "INSERT INTO  login_details(uid,username,password) VALUES(@uid,@username,EncryptByPassPhrase('12',@password))";
        cmd.Connection = con;
        cmd.Parameters.AddWithValue("@uid", txt_uid.Text);
        cmd.Parameters.AddWithValue("@username", txt_uname.Text);
        cmd.Parameters.AddWithValue("@password", upass.Text);
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        con.Close();
      //  bind_lbl();
    }
}


table login_detail

columnname             datatype

uid                               int

username                    varchar(100)

password                    varbinary(100)

Answers (5)