2013-03-26 171 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