title: AES Encryption - Encryption/Decryption Adding Extra Bytes
body: <p>I'm writing a simple library for encryption/decryption, using the Framework's objects. The methods are as follows:</p>
  
  <pre><code>public static byte[] Encrypt(byte[] key, byte[] vector, byte[] input)
  {
      if(key.Length == 0)
          throw new ArgumentException("Cannot encrypt with empty key");
  
      if (vector.Length == 0)
          throw new ArgumentException("Cannot encrypt with empty vector");
  
      if (input.Length == 0)
          throw new ArgumentException("Cannot encrypt empty input");
  
      var unencryptedBytes = input;
  
      using(AesCryptoServiceProvider aes = new AesCryptoServiceProvider {Key = key, IV = vector})
      using(ICryptoTransform encryptor = aes.CreateEncryptor())
      using (MemoryStream ms = new MemoryStream())
      using (CryptoStream writer = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
      {
          writer.Write(unencryptedBytes, 0, unencryptedBytes.Length);
          writer.FlushFinalBlock();
          var byteArray = ms.ToArray();
  
          if(byteArray.Length == 0)
              throw new Exception("Attempted to encrypt but encryption resulted in a byte array of 0 length.");
  
          return byteArray;
      }
  }
  
  
  public static byte[] Decrypt(byte[] key, byte[] vector, byte[] encrypted)
  {
      if (key.Length == 0)
          throw new ArgumentException("Cannot encrypt with empty key");
  
      if (vector.Length == 0)
          throw new ArgumentException("Cannot encrypt with empty vector");
  
      if (encrypted == null || encrypted.Length == 0)
          throw new ArgumentException("Cannot decrypt empty or null byte array");
  
      byte[] unencrypted;
  
      using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider { Key = key, IV = vector })
      using (ICryptoTransform decryptor = aes.CreateDecryptor(key, vector))
      using (MemoryStream ms = new MemoryStream(encrypted))
      using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
      {
  
          cs.Read(encrypted, 0, encrypted.Length);
  
          unencrypted =  ms.ToArray();
      }
  
  
      return unencrypted;
  }
  </code></pre>
  
  <p>Feeding it in an encoded string, for example, as follows:</p>
  
  <pre><code>var expected = "This is an example message";
  
  var messageBytes = Encoding.UTF8.GetBytes(expected);
  </code></pre>
  
  <p>Goes in as (in this example) 26 bytes. After encryption it leaves as 32 bytes, and when this is translated back as a string it has random characters appended at the end but is otherwise perfectly valid, as follows:</p>
  
  <pre><code>"This is an example message�(4���"
  </code></pre>
  
  <p>Generating a new vector/key changes the random characters that are appended to the end.</p>
  
  <p>How can I eliminate this extra-byte behavior?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Included a requested unit test.</p>
  
  <pre><code>[TestMethod]
  public void CanEncryptAndDecryptByteArray()
  {
      var expected = "This is an example message";
  
      var messageBytes = Encoding.UTF8.GetBytes(expected);
  
  
      AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
      aes.GenerateIV();
      aes.GenerateKey();
  
  
      var byteKey = Convert.ToBase64String(aes.Key);
      var vectorKey = Convert.ToBase64String(aes.IV);
  
      var key = Convert.FromBase64String("Qcf+3VzYNAfPUCfBO/ePSxCLBLItkVfk8ajK86KYebs=");
      var vector = Convert.FromBase64String("aJNKWP7M2D44jilby6BzGg==");
  
      var encrypted = EncryptionManager.Encrypt(key, vector, messageBytes);
      var decrypted = EncryptionManager.Decrypt(key, vector, encrypted);
  
      var actual = Encoding.UTF8.GetString(decrypted);
  
      Assert.AreEqual(expected, actual);
  }
  </code></pre>