0

This is my Encrypt and Decrypt Classes

 public class SharedUtility
    {
        public static String Encrypt(string strData)
        {
            if (strData != "")
            {
                strData = string.Format("{0}|{1}", HttpContext.Current.Session.SessionID, strData);
                SHA1Managed shaM = new SHA1Managed();
                Convert.ToBase64String(shaM.ComputeHash(Encoding.ASCII.GetBytes(strData)));
                Byte[] encByteData;
                encByteData = ASCIIEncoding.ASCII.GetBytes(strData);
                String encStrData = Convert.ToBase64String(encByteData);
                return encStrData;
            }
            else
            {
                return "";
            }

        }

        public static String Decrypt(string strData)
        {
            if (string.IsNullOrEmpty(strData) == false)
            {
                Byte[] decByteData;
                decByteData = Convert.FromBase64String(strData);
                String decStrData = ASCIIEncoding.ASCII.GetString(decByteData);

                String[] SplitValue = decStrData.Split('|');

                String ReturnValue = SplitValue[1];
                return ReturnValue;
            }
            else
            {
                return "";
            }

        } 

This is my Login Page Code :

 protected void btnSign_Click(object sender, EventArgs e)
    {
        try
        {
            string UserName = TextBoxUserName.Text.Trim().Replace("'", " ");
            string Password = SharedUtility.Encrypt(TextBoxPassword.Text.Trim().Replace("'", " "));
            string Result = WebUsers.AuthenticateUser(UserName, Password);
            if (Result.Contains("Success"))
                Response.Redirect("~/Home/home.aspx", false);
            else
            {

                divResult.Visible = true;
                ResultLabel.Text = Result;
                ResultLabel.Visible = true;
            }
        }
        catch (Exception ex)
        {
            ResultLabel.Text = ex.Message;
        }
    }


}

But it Through the following error :

Invalid length for a Base-64 char array or string.

Note: Also I try Trim().Replace("+", " "); Trim().Replace(" ", "+");

sir I am new to using professional template like this , and I try more and more time and also try and search on net but no solution was made successful

 public static DataTable GetUserByUserName(string UserName)
    {
        try
        {
            string ConnectionString = ConfigurationManager.ConnectionStrings["AMLiveConnectionString"].ToString();
            SqlConnection conn = new SqlConnection(ConnectionString);
            SqlCommand cmd = new SqlCommand("WebUsers_GetUsersByUserName", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@UserName", UserName);

            DataTable DT = new DataTable();
            SqlDataAdapter Adapter = new SqlDataAdapter(cmd);
            Adapter.Fill(DT);
            return DT;

        }
        catch
        {
            throw;
        }
    }     
  • refer this http://stackoverflow.com/a/2925959/795683 – Sain Pradeep Jan 26 '16 at 06:33
  • Can you show us an example of input that fails to decode? – Rob Jan 26 '16 at 06:35
  • string ConnectionString = ConfigurationManager.ConnectionStrings["AMLiveConnectionString"].ToString(); SqlConnection conn = new SqlConnection(ConnectionString); SqlCommand cmd = new SqlCommand("WebUsers_GetUsersByUserName", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@UserName", UserName); DataTable DT = new DataTable(); SqlDataAdapter Adapter = new SqlDataAdapter(cmd); Adapter.Fill(DT); return DT; – Abdul Jalil Marwat Jan 26 '16 at 06:47

1 Answers1

0

Change your code to the following. Hope it will work:

 public static DataTable GetUserByUserName(string UserName)
{
    DataTable DT = new DataTable();
    try
    {
        string ConnectionString = ConfigurationManager.ConnectionStrings["AMLiveConnectionString"].ToString();
        SqlConnection conn = new SqlConnection(ConnectionString);
        SqlCommand cmd = new SqlCommand("WebUsers_GetUsersByUserName", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@UserName", UserName);
        conn.Open();

        SqlDataAdapter Adapter = new SqlDataAdapter(cmd);
        Adapter.Fill(DT);
    }
    catch
    {
        throw;
    }
    finally{
        conn.Close();
        return DT;
    }
}     

Change is that you didn't open your connection, and your return should be in finally block, not in try

Khazratbek
  • 1,656
  • 2
  • 10
  • 17