2013-03-20 47 views
1

我計劃加密和解密在我的應用程序中輸入的密碼,並且我的加密工作正常,數據庫中的數據是加密形式的,但是涉及到解密和從數據庫檢索數據,它是否顯示錯誤..在asp.net中使用c加密和解密#

The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters. 

和示出錯誤的行是..

byte[] todecode_byte = Convert.FromBase64String(password); 

代碼

new.aspx.cs:(加密)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data.SqlClient; 
using System.Configuration; 
using System.Data; 

namespace WebApplication5 
{ 
    public partial class WebForm6 : 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); 
      cmd1.Parameters.AddWithValue("@password", txtPassword.Text); 
      SqlDataReader dr = cmd1.ExecuteReader(); 
      if (dr.HasRows) 
      { 
       ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('userName is already availables')</script>"); 

      } 

      else 
      { 

       SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TestQueryConnectionString"].ConnectionString); 
       con.Open(); 
       string strQuery = "insert into admin(USERNAME,PASSWORD) values('" + txtUserName.Text + 
        "','" + EncodePasswordToBase64(txtPassword.Text) + "')"; 
       connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TestQueryConnectionString"].ConnectionString); 
       connection.Open(); 
       SqlCommand cmd = new SqlCommand(strQuery, connection); 
       cmd.ExecuteNonQuery(); 
       connection.Close(); 
       Response.Redirect("login.aspx"); 

      } 

      con1.Close(); 
     } 
     public static string EncodePasswordToBase64(string password) 
     { 
      try 
      { 
       byte[] encData_byte = new byte[password.Length]; 
       encData_byte = System.Text.Encoding.UTF8.GetBytes(password); 
       string encodedData = Convert.ToBase64String(encData_byte); 
       return encodedData; 
      } 
      catch (Exception ex) 
      { 
       throw new Exception("Error in base64Encode" + ex.Message); 
      } 
     } 

    } 
} 

login.aspx.cs:(解密)

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 DecodeFrom64([email protected]) ", con1); 
      cmd1.Parameters.AddWithValue("@username", txtUserName.Text); 
      cmd1.Parameters.AddWithValue("@password", DecodeFrom64(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 password) 
     { 
      System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding(); 
      System.Text.Decoder utf8Decode = encoder.GetDecoder(); 
      byte[] todecode_byte = Convert.FromBase64String(password); 
      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; 
     } 

    } 

} 

PLZ任何一個可以幫助我在這個過程中... ...,

回答

5

除了一切之外,你調用錯誤的函數。你叫它如下:

DecodeFrom64(txtPassword.Text) 

我可以告訴你了,我覺得txtPassword.Text不包含Base64編碼字符串。


您正試圖太硬的DecodeFrom64功能:

public string DecodeFrom64(string password) 
{ 
    return System.Text.UTF8.GetString(Convert.FromBase64String(password)); 
} 

你要做的編碼功能的相反反向:

byte[] encData_byte = new byte[password.Length]; 
encData_byte = System.Text.Encoding.UTF8.GetBytes(password); 
string encodedData = Convert.ToBase64String(encData_byte); 

你做的最後一件事是Convert.ToBase64String所以你必須Convert.FromBase64String。 然後,在此之前,您使用System.Text.Encoding.UTF8.GetBytes。該功能的反面是System.Text.UTF8.GetString。正如你可以在我的回答看,你可以把所有的一起在1線:

System.Text.UTF8.GetString(Convert.FromBase64String(password)); 

但你不加密的密碼,您只需將混淆他們。如果我攻擊了您的數據庫並看到了這些密碼,我可以輕鬆破解它們。我只需要在http://www.motobit.com/util/base64-decoder-encoder.asp這樣的網站上輸入它們,或者編寫我自己的小程序,我擁有所有普通密碼。

如果要將密碼保存到數據庫,最好使用散列。如果你創建並保存一個密碼哈希到數據庫,那麼當黑客得到你的數據庫時,他/她看不到真正的密碼,因爲你不能像例如base64那樣反轉哈希。

如果有人正在嘗試登錄到您的網站,您創建了輸入密碼的散列,然後查看散列是否等於保存的散列。如果它的密碼是一樣的。

作爲哈希算法,我會推薦SHA512。它目前是最好的之一。 MD5比較老,並且在那裏有rainbow tables,可以立刻破解MD5。

+0

它不工作,先生它在UTF8顯示錯誤。 – BHARATH 2013-03-20 10:20:04

+0

@ user2189723編輯,看第一行 – SynerCoder 2013-03-20 10:23:40

+0

@nunespascal 看到這個可以理解我所面對的plb ...., – BHARATH 2013-03-20 10:35:12