title: Invalid length for a Base-64 char array error in c#
body: <p>In an remote server I have added this <code>redirect</code> to my web server:</p>
  
  
  
  <p>On <code>index.aspx</code> web page where the value of <code>querystring</code> <strong>UserNumber</strong> is <strong>encrypted</strong>:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      Response.Redirect("http://.../default.aspx?UserNumber=" + Encrypt(Request.QueryString["UserNumber"].ToString().Trim()));
  }
  </code></pre>
  
  <p>The web page <code>index.aspx</code> redirects on the authentication page where the value of <code>querystring</code> <strong>UserNumber</strong> is <strong>decrypted</strong>.</p>
  
  <p>I ran several tests and I have problem with this <strong>UserNumber</strong>: <code>a425033</code> or <code>A425033</code>.</p>
  
  <p>When the value of <strong>UserNumber</strong> is <code>a425033</code> or <code>A425033</code> the web page for authentication print this error:</p>
  
  <p><a href="https://i.stack.imgur.com/Rf8P4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Rf8P4.png" alt="enter image description here"></a></p>
  
  <p>The error is:</p>
  
  <pre><code>Invalid length for a Base-64 char array
  </code></pre>
  
  <p>My code below.</p>
  
  <p>Please help me.</p>
  
  <p>Thank you in advance.</p>
  
  <pre><code>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;
      }
  }
  </code></pre>