如果密码以加密格式存储在sql server中,如何登录 [英] how to sign in if the password is stored in encrpted format in sql server

查看:73
本文介绍了如果密码以加密格式存储在sql server中,如何登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的注册代码,我使用了md5来哈希密码.登录按钮的代码是什么?

This is my signup code, i have used md5 to hash the password. what is the code of sign-in button?

private void button5_Click(object sender, EventArgs e)
       {
           SqlConnection myconnection;
           SqlCommand mycommand;
           int ra;
           string query;
            Byte[] hashedDataBytes;
           myconnection = new SqlConnection("Data Source=AMEERA-PC\\SQLEXPRESS;Initial Catalog=dbbaas;User ID=baas1;Password=baas123");
           myconnection.Open();
           MD5CryptoServiceProvider md5hasher = new MD5CryptoServiceProvider();

           UTF8Encoding encoder = new UTF8Encoding();
           hashedDataBytes = md5hasher.ComputeHash(encoder.GetBytes(textBox3.Text));
           StringBuilder hex = new StringBuilder(hashedDataBytes.Length * 2);
           foreach (Byte b in hashedDataBytes)
           {
               hex.AppendFormat("{0:x2}", b);
           }
           label3.Text = hex.ToString();


           query = "Insert into nuser ( uid,uname,enpwd) values(" + textBox1.Text + ",'" +
           textBox2.Text + "','" + hex.ToString() + "')";
           mycommand = new SqlCommand(query, myconnection);
           ra = mycommand.ExecuteNonQuery();

           if (ra > 0)
           {
               MessageBox.Show("Record Inserted Sucessfuly");
           }
           else
           {
               MessageBox.Show("Unable to Insert Record ");
           }
           myconnection.Close();


       }


请帮助,我今天需要代码吗?
预先感谢


please help , i need the code today?
thanks in advance

推荐答案

您好,在登录时,请首先在文本框中加密输入的密码,然后将其与数据库进行比较并检查身份验证
Hi, At the time of login, first encrypt entered password in textbox and compare this with database and check authenticate or not.


您好,您需要再次在登录按钮中编写以下代码

Hi, you need to again write following code in login button

hashedDataBytes = md5hasher.ComputeHash(encoder.GetBytes(textBox3.Text));
            StringBuilder hex = new StringBuilder(hashedDataBytes.Length * 2);
            foreach (Byte b in hashedDataBytes)
            {
                hex.AppendFormat("{0:x2}", b);
            }
            label3.Text = hex.ToString();
 

            query = "Select * from nuser  where uname='" + textBox1.Text + "' and enpwd ='" + hex.ToString() + "')";
            mycommand = new SqlCommand(query, myconnection);
            ra = mycommand.ExecuteNonQuery();


你好朋友,

当您从数据库取回数据(密码)时,请对其解密并进行比较.我使用UTF8做到了这一点,然后解码回去:

像这样:
Hello friend,

when you are retrieving data (password) back from database then decrypt that and compare. And i did this using UTF8 and decoded back :

like this :
public string DecryptString(string pwd)
        {
          try
          {
            UTF8Encoding encoder = new System.Text.UTF8Encoding();
            Decoder utf8Decode = encoder.GetDecoder();

            byte[] toDecode_byte = Convert.FromBase64String(pwd);
            int charCount = utf8Decode.GetCharCount(toDecode_byte, 0, toDecode_byte.Length);
            char[] decoded_char = new char[charCount];
            utf8Decode.GetChars(toDecode_byte, 0, toDecode_byte.Length, decoded_char, 0);
            string decodedPwd = new string(decoded_char);
            return decodedPwd;
          }
          catch
          {
            return null;
          }
        }


希望对您有帮助.
如果有帮助,请不要忘记将其标记为答案. :)


Hope this will help you.
Don''t forget to mark as answer if it helps. :)


这篇关于如果密码以加密格式存储在sql server中,如何登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆