2013-03-20 135 views
0

最后一次是PBL与ENC并将其存储db和现在的PBL是在分解和从DB检索数据则显示错误作为一个更描述

的输入是不一个有效的Base-64字符串,因为它含有非基本的填充字符

代码是这样的间64 字符,两个以上的填充字符,或一个非空白 字符:

using System; 
using System.Collections; 
using System.Configuration; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Xml.Linq; 
using System.Security.Cryptography; 
using System.Data.SqlClient; 


namespace WebApplication5 
{ 
    public partial class WebForm4 : System.Web.UI.Page 
    { 
     SqlConnection connection; 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TestQueryConnectionString"].ConnectionString); 
     } 

     protected void btnSubmit_Click(object sender, EventArgs e) 
     { 
      SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["TestQueryConnectionString"].ConnectionString); 
      con1.Open(); 
      SqlCommand cmd1 = new SqlCommand("select * from admin where [email protected] and [email protected] ", con1); 
      cmd1.Parameters.AddWithValue("@username", txtUserName.Text); 
      string strpassword = DecodeFrom64(txtPassword.Text); 
      cmd1.Parameters.AddWithValue("@password", txtPassword.Text); 
      SqlDataAdapter da = new SqlDataAdapter(cmd1); 
      DataTable dt = new DataTable(); 
      da.Fill(dt); 
      if (dt.Rows.Count > 0) 
      { 
       Response.Redirect("emplist.aspx"); 
      } 
      else 
      { 
       ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>"); 
      } 
      con1.Close(); 
     } 
     protected void btnClear_Click(object sender, EventArgs e) 
     { 
      txtUserName.Text = ""; 
      txtPassword.Text = ""; 
     } 
     public string DecodeFrom64(string encodedData) 
     { 
      System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding(); 
      System.Text.Decoder utf8Decode = encoder.GetDecoder(); 
      byte[] todecode_byte = Convert.FromBase64String(encodedData); 
      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 result = new String(decoded_char); 
      return result; 
     } 

    } 

} 
+0

@ user2189723请给出正确的标题名称。 – 2013-03-20 07:48:40

+0

你不会/不能在问题中标记一个人,我说如果它有一个新问题,然后问一个新问题。此外,你需要给你的问题更好的标题,否则你可能会得到赞誉。 – Habib 2013-03-20 08:43:20

+0

请写出整个单词,而不是使用简写。你继续写“pbl”和“enc”这样的东西的方式会让你的问题很难理解。 – jadarnel27 2013-06-04 14:15:41

回答

2

对不起,我不是哈比卜,但

txtPassword.Text将用户输入的文本。用户通常不输入Base64编码数据。假设用户输入的密码是Base64编码是完全错误的。

摆脱这一行的应该帮助

string strpassword = DecodeFrom64(txtPassword.Text); 

你甚至不似乎以后使用它。

此外,如果要加密密码,请使用SHA之类的单向散列。 Base64不会加密它。虽然它不会是明文,但它很容易解码。

编辑:
要匹配的编码密码,则需要进行编码由用户输入的密码,然后选择。

string strpassword = EncodeToBase64(txtPassword.Text); 
cmd1.Parameters.AddWithValue("@password", strpassword); 
+0

我想从数据库中分解数据,以及它应该如何与编码相同......, – BHARATH 2013-03-20 08:05:06

+0

如何做到这一点nunespascal sir ..., – BHARATH 2013-03-20 08:17:23

+0

你只能解码你已经编码的东西。所以这取决于你如何编码它。要比较一个密码和你在db中编码的密码,你必须编码用户输入的密码,而不是解码它。 – nunespascal 2013-03-20 08:30:01