2013-03-20 195 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