title: .NET AES decryption breaks first few bytes
body: <p>I'm using <code>AesCryptoServiceProvider</code> to encrypt and decrypt an XML document on disk. There's an example in the MSDN reference that was helpful. I generate the AES key from the SHA-256 hash of a given password. The first half of it is assigned as IV, since I don't know of any better thing to use here. As far as I know, both key and IV must be the same for encrypting and decrypting.</p>
  
  <p>When I decrypt my file, this is what the beginning of it looks like:</p>
  
  <pre><code>I���H璧�-����[�="1.0" encoding="utf-8"?&gt;
  </code></pre>
  
  <p>The rest of the document is perfectly fine. There's not even some random padding after the content as I would have expected maybe.</p>
  
  <p>What is causing this random garbage at the beginning of the file?</p>
  
  <p>Here's more of the reading code:</p>
  
  <pre><code>using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
  {
      using (SHA256CryptoServiceProvider sha = new SHA256CryptoServiceProvider())
      {
          this.cryptoKey = sha.ComputeHash(Encoding.Unicode.GetBytes(password));
      }
      aes.Key = this.cryptoKey;
      Array.Copy(this.cryptoKey, aes.IV, 16);
  
      ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
  
      using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
      using (CryptoStream cs = new CryptoStream(fs, decryptor, CryptoStreamMode.Read))
      using (StreamReader sr = new StreamReader(cs))
      {
          string data = sr.ReadToEnd();
          xdoc.LoadXml(data);
  
          //xdoc.Load(sr);
      }
  }
  </code></pre>
  
  <p>And that's the encryption code:</p>
  
  <pre><code>XmlWriterSettings xws = new XmlWriterSettings();
  xws.Encoding = Encoding.UTF8;
  xws.Indent = true;
  xws.IndentChars = "\t";
  xws.OmitXmlDeclaration = false;
  
  using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
  {
      aes.Key = this.cryptoKey;
      Array.Copy(this.cryptoKey, aes.IV, 16);
  
      ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
  
      using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
      using (CryptoStream cs = new CryptoStream(fs, encryptor, CryptoStreamMode.Write))
      using (StreamWriter sw = new StreamWriter(cs, Encoding.UTF8))
      {
          XmlWriter writer = XmlWriter.Create(sw, xws);
          xdoc.Save(writer);
          writer.Close();
      }
  }
  </code></pre>