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
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)
Suthish NairPosted Jan 24, 2011, 6:15 AM
Happy to see you got the answer. Also thanks for posting the solution also.
This something new to all here.
Bharat BhushanPosted Jan 24, 2011, 3:03 AM
con.ConnectionString = ConfigurationManager.ConnectionStrings["cnn"].ConnectionString;
VALUES(@uid,@username,EncryptByPassPhrase('12',@password))";con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText =@ "INSERT INTO login_details(uid,username,password)
cmd.Connection = con;
cmd.Parameters.AddWithValue("@uid", txt_uid.Text);
cmd.Parameters.AddWithValue("@username", txt_uname.Text);
string sStr = upass.Text;
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
byte[] bArray = enc.GetBytes(sStr);
cmd.Parameters.AddWithValue("@password", bArray);
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
Krishna GaradPosted Jan 24, 2011, 12:55 AM
http://www.c-sharpcorner.com/UploadFile/syedshakeer/5180/Default.aspx
Bharat BhushanPosted Jan 24, 2011, 12:29 AM
Dear , In your article, the data encrypted only in the textbox but i want to know that how we can store varbinary type of data store in the sql server. please read my problem correctly and give me answer again.
Krishna GaradPosted Jan 24, 2011, 12:18 AM
http://www.c-sharpcorner.com/UploadFile/krishnasarala/5184/Default.aspx
you can stor this data in database in encrypted form.