title: Difference between Convert.ToBase64String/Convert.FromBase64String and Encoding.UTF8.GetBytes/Encoding.UTF8.GetString
body: <p>I currently am learning Symmetric Cryptography in .NET. I wrote a demo as following: </p>
  
  <pre><code>    private byte[] key = Encoding.ASCII.GetBytes("abcdefgh");
      private byte[] IV = Encoding.ASCII.GetBytes("hgfedcba");
      private byte[] encrypted;
  
      public Form1()
      {
          InitializeComponent();
  
      }
  
      private void btnEncrypt_Click(object sender, EventArgs e)
      {
          this.textBox2.Text = this.Encrypt(this.textBox1.Text);
      }
  
      private void btnDecrypt_Click(object sender, EventArgs e)
      {
          this.textBox3.Text = this.Decrypt(this.textBox2.Text);
      }
  
      private string Encrypt(string plainText)
      {
          try
          {
              using (DESCryptoServiceProvider crypto = new DESCryptoServiceProvider())
              {
                  crypto.Key = this.key;
                  crypto.IV = this.IV;
  
                  ICryptoTransform transform = crypto.CreateEncryptor(crypto.Key, crypto.IV);
  
                  using (MemoryStream stream = new MemoryStream())
                  {
                      using (CryptoStream cryptoStream = new CryptoStream(stream, transform, CryptoStreamMode.Write))
                      {
                          using (StreamWriter writer = new StreamWriter(cryptoStream))
                          {
                              writer.Write(plainText);
                          }
  
                          encrypted = stream.ToArray();
                      }
                  }
              }
  
              return Convert.ToBase64String(encrypted);
          }
          catch (Exception)
          {
  
              throw;
          }
      }
  
      private string Decrypt(string cipherText)
      {
          try
          {
              string plainText = string.Empty;
  
              using (DESCryptoServiceProvider crypto = new DESCryptoServiceProvider())
              {
                  crypto.Key = this.key;
                  crypto.IV = this.IV;
  
                  ICryptoTransform transform = crypto.CreateDecryptor(crypto.Key, crypto.IV);
  
                  using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(cipherText)))
                  {
                      using (CryptoStream cryptoStream = new CryptoStream(stream, transform, CryptoStreamMode.Read))
                      {
                          using (StreamReader reader = new StreamReader(cryptoStream))
                          {
                              plainText = reader.ReadToEnd();
                          }
                      }
                  }
              }
  
              return plainText;
          }
          catch (Exception)
          {
  
              throw;
          }
      }
  </code></pre>
  
  <p>Everything work as expected. But if I replace</p>
  
  <pre><code>return Convert.ToBase64String(encrypted);
  </code></pre>
  
  <p>And </p>
  
  <pre><code>using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(cipherText)))
  </code></pre>
  
  <p>To</p>
  
  <pre><code>return Encoding.UTF8.GetString(encrypted);
  </code></pre>
  
  <p>And </p>
  
  <pre><code>using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(cipherText)))
  </code></pre>
  
  <p>I got an error at CryptoStream <code>System.NotSupportedException</code>. After diagnostic the code, I found that <code>Encoding.UTF8.GetBytes(cipherText)</code> have more bytes than <code>encrypted</code></p>
  
  <p>So what is the difference between using <code>Convert.From/ToBase64String</code> and <code>Encoding.UTF8.GetBytes/GetString)</code> ?</p>