i use this code to Crypt the value:-
public static string Hash(string value)
{
return Convert.ToBase64String(
System.Security.Cryptography.SHA256.Create()
.ComputeHash(Encoding.UTF8.GetBytes(value))
);
}
This code when i insert value :-
private void btn_Save_Click(object sender, EventArgs e)
{
if (txt_ID.Text != "" && txt_Name.Text != "")
{
string IfIdisExist = "select id from name where id = @id";
SqlCommand sqlComId = new SqlCommand(IfIdisExist, con);
sqlComId.Parameters.AddWithValue("@id", Convert.ToInt32(txt_ID.Text));
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(sqlComId);
DataTable dt = new DataTable();
sda.Fill(dt);
con.Close();
if (dt.Rows.Count == 0)
{
string QueryHistoryLogin = "INSERT INTO Name (id, name)";
QueryHistoryLogin += " VALUES (@id, @name)";
SqlCommand sCommand = new SqlCommand(QueryHistoryLogin, con);
sCommand.Parameters.AddWithValue("@id", Convert.ToInt32(txt_ID.Text));
sCommand.Parameters.AddWithValue("@name", Hash(txt_Name.Text));
con.Open();
sCommand.ExecuteNonQuery();
con.Close();
MessageBox.Show("Success Save!");
txt_ID.Text = "";
txt_Name.Text = "";
}
else
{
MessageBox.Show("Id is Exist!");
}
}
else
{
MessageBox.Show("Id or name is Empty!");
}
}
and this is code when i get that value i crypt to display it in textbox:-
private void btn_Search_Click(object sender, EventArgs e)
{
string QueryString = "select * from Name where id = @id ";
SqlCommand cmd = new SqlCommand(QueryString, con);
cmd.Parameters.AddWithValue("@id", Convert.ToInt32(txt_ID.Text));
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
int i = cmd.ExecuteNonQuery();
SqlDataReader rdr = cmd.ExecuteReader();
rdr.Read();
string name = rdr["name"].ToString();
con.Close();
if(dt.Rows.Count > 0)
{
}
}
What I have tried:
How i decrypt the value when i select this value using id to display it in textbox?