0

前回はencでpblを実行し、dbを保存していましたが、pblはdecであり、dbからデータを取得すると、次のようなエラーが表示されます。

入力には、Base 64以外の文字、3つ以上のパディング文字、またはパディング文字の中に空白以外の文字が含まれているため、有効なBase-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 USERNAME=@USERNAME and PASSWORD=@PASSWORD ", 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;
        }

    }

}
4

1 に答える 1

2

私はハビブではありませんが、

txtPassword.Textユーザーが入力したテキストが表示されます。ユーザーはほとんどの場合、Base64でエンコードされたデータを入力しません。ユーザーが入力するパスワードがBase64でエンコードされていると想定するのは、まったく間違っています。

この行を取り除くことは役立つはずです

string strpassword = DecodeFrom64(txtPassword.Text);

後で使うことすらしないようです。

また、パスワードを暗号化する場合は、SHAのような一方向ハッシュとソルトを使用します。Base64はそれを暗号化するつもりはありません。クリアテキストではありませんが、デコードは非常に簡単です。

編集:
エンコードされたパスワードと一致させるには、ユーザーが入力したパスワードをエンコードしてから選択する必要があります。

string strpassword = EncodeToBase64(txtPassword.Text);
cmd1.Parameters.AddWithValue("@password", strpassword);
于 2013-03-20T07:54:38.083 に答える