2013-03-26 170 views
0

我正在登錄應用程序,我必須使用TripleDES加密和解密密碼,我有一套編碼和編碼加密工作正常,但解密不起作用它顯示一個錯誤。使用TripleDES加密解密密碼

和錯誤是:

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. 

和編碼是:

newuser.aspx.cs

using System; 
using System.Web.UI; 
using System.Data.SqlClient; 
using System.Configuration; 
using System.Security.Cryptography; 
using System.Text; 
using System.IO; 

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 + 
       "','" + EncryptTripleDES(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 EncryptTripleDES(string value) 
     { 
      TripleDESCryptoServiceProvider cryptoProvider = new TripleDESCryptoServiceProvider(); 
      MemoryStream memoryStream = new MemoryStream(); 
      CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(), CryptoStreamMode.Write); 
      StreamWriter streamWriter = new StreamWriter(cryptoStream); 
      streamWriter.Write(value); 
      streamWriter.Flush(); 
      cryptoStream.FlushFinalBlock(); 
      memoryStream.Flush(); 
      return Convert.ToBase64String(memoryStream.GetBuffer(), 0, Convert.ToInt32(memoryStream.Length)); 
     } 

    } 
} 

login.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; 
using System.Security.Cryptography; 
using System.IO; 

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); 
      cmd1.Parameters.AddWithValue("@password", EncryptTripleDES(DecryptTripleDES(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 static string EncryptTripleDES(string value) 
     { 
      TripleDESCryptoServiceProvider cryptoProvider = new TripleDESCryptoServiceProvider(); 
      MemoryStream memoryStream = new MemoryStream(); 
      CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(), CryptoStreamMode.Write); 
      StreamWriter streamWriter = new StreamWriter(cryptoStream); 
      streamWriter.Write(value); 
      streamWriter.Flush(); 
      cryptoStream.FlushFinalBlock(); 
      memoryStream.Flush(); 
      return Convert.ToBase64String(memoryStream.GetBuffer(), 0, Convert.ToInt32(memoryStream.Length)); 
     } 


     public static string DecryptTripleDES(string value) 
     { 
      TripleDESCryptoServiceProvider cryptoProvider = new TripleDESCryptoServiceProvider(); 
      byte[] buffer = Convert.FromBase64String(value); 
      MemoryStream memoryStream = new MemoryStream(buffer); 
      CryptoStream cryptoSteam = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(), CryptoStreamMode.Read); 
      StreamReader streamReader = new StreamReader(cryptoSteam); 
      return streamReader.ReadToEnd(); 
     } 

    } 

} 

和PLZ幫我在這編碼PBL ......,

+0

爲什麼你解密加密+,填補了該密碼參數?最終結果應該等於'txtPassword.Text'。正如fredrik指出的那樣,除了解密純文本值之外。 – 2013-03-26 08:21:28

回答

1

,我可以看到它,問題是出在兩個地方:

byte[] buffer = Convert.FromBase64String(value); 

的int DecryptTripleDES()功能和

cmd1.Parameters.AddWithValue("@password", EncryptTripleDES(DecryptTripleDES(txtPassword.Text))); 

在你的按鈕提交處理程序。

在第一行嘗試將Base64編碼的字符串轉換爲字節數組。如果不是因爲這種情況下它可能只是一些隨機文本或用戶輸入到txtPassword控件中的密碼,那麼一切順利。

或者您是否期望用戶手動加密密碼,將Base64編碼並將其輸入到txtPassword字段?

嘗試這樣做,而不是:

cmd1.Parameters.AddWithValue("@password", EncryptTripleDES(txtPassword.Text)); 
+0

,雅它的好,但它仍然不工作,它顯示無效的用戶名和密碼是什麼意思...., – BHARATH 2013-03-26 09:08:33

+0

@漢斯凱斯汀我無法理解你告訴....的事情, – BHARATH 2013-03-26 09:20:12

+0

,先生我正在等待您的重播......, – BHARATH 2013-03-26 09:23:06