PDA

View Full Version : Invalid length for a Base-64 char array



میلاد قاضی پور
دوشنبه 15 شهریور 1389, 18:00 عصر
سلام . من از توابع زیر برای انکریپت و دیکریپت استفاده میکنم . در نمونه ای که دوستان ارائه کرده بودن تو سایت این توابع کاملا صحیح کار میکردن و من هم از همون کلاسی که تو اون برنامه نمونه بود استفاده کردم .
مشکل اینه که وقتی میخوام یوزرنیم و پسورد وارد شده توسط کاربر رو چک کنم اون ارور رو میده.



public static string EncryptText(string usernameRegsiteredByUser)
{
RijndaelManaged rm = new RijndaelManaged();
byte[] plainText = Encoding.Unicode.GetBytes(usernameRegsiteredByUser );

string key = "MeeladGhazipour";
byte[] salt = Encoding.ASCII.GetBytes(key.Length.ToString());
PasswordDeriveBytes pdb = new PasswordDeriveBytes(key, salt);
rm.Key = pdb.GetBytes(32);
rm.IV = pdb.GetBytes(16);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, rm.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(plainText, 0, plainText.Length);
cs.FlushFinalBlock();
byte[] Chiper = ms.ToArray();
ms.Close();
cs.Close();
string ChipperText = Convert.ToBase64String(Chiper);
return ChipperText;
}
///////////////////////////////////////////////////////

public static string DecryptTextٌ(string ChipperText)
{
RijndaelManaged rm = new RijndaelManaged();
byte[] EncryptData = Convert.FromBase64String(ChipperText);
string key = "MeeladGhazipour";
byte[] salt = Encoding.ASCII.GetBytes(key.Length.ToString());
PasswordDeriveBytes pdb = new PasswordDeriveBytes(key, salt);
ICryptoTransform Decryptor = rm.CreateDecryptor(pdb.GetBytes(32), pdb.GetBytes(16));
MemoryStream ms = new MemoryStream(EncryptData);
CryptoStream cs = new CryptoStream(ms, Decryptor, CryptoStreamMode.Read);
byte[] PlainText = new byte[EncryptData.Length];
int DecryptCount = cs.Read(PlainText, 0, PlainText.Length);
ms.Close();
cs.Close();
string DecryptoData = Encoding.Unicode.GetString(PlainText, 0, DecryptCount);
return DecryptoData;
}



یوزر نیم و پسورد رو با این کد چک میکنم:




private void button1_vorud_Click(object sender, EventArgs e)
{
if ((textBox1_uname.Text == "") | (textBox2_pass.Text == ""))
{
msgs.unCompleteFields();
}
else
{
try
{
bool isValidUname;
isValidUname = rwd.checkUserPass("SELECT * FROM users where username='"
+Security.DecryptTextٌ(textBox1_uname.Text.ToStri ng()) + "' and pass='"
+Security.DecryptTextٌ( textBox2_pass.Text.ToString() )+ "'");
if (isValidUname == true)
{
this.Hide();
this.DialogResult = DialogResult.OK;
Form1 form1 = (Form1)Application.OpenForms["Form1"];
form1.SS_usersnamelabel.Text = this.textBox1_uname.Text;
}
}
catch(Exception ex)
{

MessageBox.Show(ex.Message);

//نمایش پیغام خطا
msgs.unableToDo();
}
}
}




public bool checkUserPass(string selectcommand)
{
connection = null;
connection = new SqlConnection(strConnection);
command = new SqlCommand();
command.Connection = connection;
connection.Open();
command.CommandText=selectcommand;
datareader=command.ExecuteReader();

if (datareader.HasRows)
{
return true;
}
else
{
appMessages.invalidUserPass();
}
datareader.Close();
connection.Close();
return false;
}

fjm11100
دوشنبه 15 شهریور 1389, 19:07 عصر
فکر کنم توابع را برعکس استفاده کرده ای