title: Encrypt & Decrypt querystring values using AES 256
body: <p>I am using the following code to Encrypt/Decrypt a querystring and pass it from one page to another. The resulting output is missing a '+' (see at the bottom of the question). What can I do to make sure the '+' comes thru as I am already using urlencode/urldecode?</p>
  
  <p>//Encryption page</p>
  
  <pre><code>    protected void Page_Load(object sender, EventArgs e)
      {
          string text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";            
          Response.Write("256:" + Decrypt256(Encrypt256(text)));
          Response.Write(string.Format("&lt;br/&gt;&lt;a href=\"decrypt.aspx?p={0}\"&gt;{0}&lt;/a&gt;", HttpUtility.UrlEncode(Encrypt256(text))));            
      }
  
  
      private const string AesIV256 = @"!QAZ2WSX#EDC4RFV";
      private const string AesKey256 = @"5TGB&amp;YHN7UJM(IK&lt;5TGB&amp;YHN7UJM(IK&lt;";
  
  
      private string Encrypt256(string text)
      {            
          // AesCryptoServiceProvider
          AesCryptoServiceProvider aes = new AesCryptoServiceProvider();            
          aes.BlockSize = 128;
          aes.KeySize = 256;
          aes.IV = Encoding.UTF8.GetBytes(AesIV256);
          aes.Key = Encoding.UTF8.GetBytes(AesKey256);
          aes.Mode = CipherMode.CBC;
          aes.Padding = PaddingMode.PKCS7;
  
          // Convert string to byte array
          byte[] src = Encoding.Unicode.GetBytes(text);
  
          // encryption
          using (ICryptoTransform encrypt = aes.CreateEncryptor())
          {
              byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length);
  
              // Convert byte array to Base64 strings
              return Convert.ToBase64String(dest);
          }
      }
  
      /// &lt;summary&gt;
      /// AES decryption
      /// &lt;/summary&gt;
      private string Decrypt256(string text)
      {            
          // AesCryptoServiceProvider
          AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
          aes.BlockSize = 128;
          aes.KeySize = 256;
          aes.IV = Encoding.UTF8.GetBytes(AesIV256);
          aes.Key = Encoding.UTF8.GetBytes(AesKey256);
          aes.Mode = CipherMode.CBC;
          aes.Padding = PaddingMode.PKCS7;
  
          // Convert Base64 strings to byte array
          byte[] src = System.Convert.FromBase64String(text);
  
          // decryption
          using (ICryptoTransform decrypt = aes.CreateDecryptor())
          {
              byte[] dest = decrypt.TransformFinalBlock(src, 0, src.Length);
              return Encoding.Unicode.GetString(dest);
          }
      }
  </code></pre>
  
  <p>Decryption page (I use this page to print out what I the decrypted string from the first page and then compare it what I get in the URL:</p>
  
  <pre><code>     protected void Page_Load(object sender, EventArgs e)
      {
          string urlValue = HttpUtility.UrlDecode(Request.QueryString["p"].Trim());
       Decrypt256(Encoding.ASCII.GetString(s2));            
  
          Response.Write(urlValue + "&lt;br /&gt;&lt;br /&gt;");     
          Response.Write("AUwsHc8j/llULnuwVnspNwolBUAhl5GFqC6iOrUN5euZFrOgFVypqTGfhAaooLxa0Fko+9KGtRh3UcQJtzkfSw==");            
  
      }
  </code></pre>
  
  <p>The end result is the following two lines (the first line is the output from the URL). They almost match except the first URL (the encoded/decoded querystring result) is missing the '+' symbol. Any idea how to avoid this?</p>
  
  <p>AUwsHc8j/llULnuwVnspNwolBUAhl5GFqC6iOrUN5euZFrOgFVypqTGfhAaooLxa0Fko 9KGtRh3UcQJtzkfSw==</p>
  
  <p>AUwsHc8j/llULnuwVnspNwolBUAhl5GFqC6iOrUN5euZFrOgFVypqTGfhAaooLxa0Fko+9KGtRh3UcQJtzkfSw== </p>