Antonio Mailtraq
Antonio Mailtraq

Reputation: 1407

Invalid length for a Base-64 char array error in c#

In an remote server I have added this redirect to my web server:

On index.aspx web page where the value of querystring UserNumber is encrypted:

protected void Page_Load(object sender, EventArgs e)
{
    Response.Redirect("http://.../default.aspx?UserNumber=" + Encrypt(Request.QueryString["UserNumber"].ToString().Trim()));
}

The web page index.aspx redirects on the authentication page where the value of querystring UserNumber is decrypted.

I ran several tests and I have problem with this UserNumber: a425033 or A425033.

When the value of UserNumber is a425033 or A425033 the web page for authentication print this error:

enter image description here

The error is:

Invalid length for a Base-64 char array

My code below.

Please help me.

Thank you in advance.

private string Encrypt(string clearText)
{
    string EncryptionKey = "Some String";

    if (Request.QueryString["UserNumber"] != null)
    {
        byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { /*Some Bytes*/ });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(clearBytes, 0, clearBytes.Length);
                    cs.Close();
                }
                clearText = Convert.ToBase64String(ms.ToArray());
            }
        }
        return clearText;
    }
    else
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "alert('No user.');window.location='http://.../main.aspx';", true);
        return null;
    }
}

private string Decrypt(string cipherText)
{
    string EncryptionKey = "Some String";

    if (Request.QueryString["UserNumber"] != null)
    {
        byte[] cipherBytes = Convert.FromBase64String(cipherText);

        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { /*Some Bytes*/ });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(cipherBytes, 0, cipherBytes.Length);
                    cs.Close();
                }
                cipherText = Encoding.Unicode.GetString(ms.ToArray());
            }
        }
        return cipherText;
    }
    else
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "alert('No user.');window.location='http://.../main.aspx';", true);
        return null;
    }
}

Upvotes: 0

Views: 1143

Answers (1)

Hamamelis
Hamamelis

Reputation: 2113

Try this:

Decrypt(Request.QueryString["UserNumber"].ToString().Replace(" ", "+"))

I hope to have been helpful in solving your problem.

Upvotes: 2

Related Questions