title: Downloading encrypted file from Window Azure storage
body: <p>I have created a MVC WebRole Window Azure application where i upload encrypted files to Azure blob storage using SymmetricAlgorithm (Rijndael) like this</p>
  
  <p>Controler>Action is</p>
  
  <pre><code>[HttpPost]
  public ActionResult UploadImage_post(HttpPostedFileBase fileBase)
  {
      if (fileBase.ContentLength &gt; 0)
      {
         // Retrieve a reference to a container 
         Microsoft.WindowsAzure.StorageClient.CloudBlobContainer blobContainer =
                _myBlobStorageService.GetCloudBlobContainer();
  
         Microsoft.WindowsAzure.StorageClient.CloudBlob blob =
                  blobContainer.GetBlobReference(fileBase.FileName);
         using (BlobStream blobStream = blob.OpenWrite())
         {
               string encryptionKey = //somekey;
               byte[] file = new byte[fileBase.ContentLength];
               EncDecAlgo.EncryptBlobFile(file, blobStream, encryptionKey);
         }
      }
  }
  
  public void EncryptBlobFile(byte[] file, BlobStream bs, string key)
      {
          PasswordDeriveBytes pdb = new PasswordDeriveBytes(key,
              new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 
          0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
          Rijndael alg = Rijndael.Create();
  
          alg.Key = pdb.GetBytes(32);
          alg.IV = pdb.GetBytes(16);
  
          CryptoStream cs = new CryptoStream(bs,
             alg.CreateEncryptor(), CryptoStreamMode.Write);
  
          foreach (var data in file)
          {
              cs.WriteByte((byte)data);
          }
  
          cs.Close();
          bs.Close();
      }
  </code></pre>
  
  <p>The above File encryption is working fine.</p>
  
  <p>For Downloading code is</p>
  
  <pre><code> public ActionResult DownloadFile(string filename)
      {
          // Retrieve reference to a previously created container.
          Microsoft.WindowsAzure.StorageClient.CloudBlobContainer blobContainer =
           _myBlobStorageService.GetCloudBlobContainer();
  
          Microsoft.WindowsAzure.StorageClient.CloudBlob blob =
              blobContainer.GetBlobReference(filename);
          blob.FetchAttributes();
          string encryptionKey = //same key used in encryption;
          using (BlobStream blobStream = blob.OpenRead())
          {
              EncDecAlgo.DecryptBlobFile(blobStream, encryptionKey, filename);
          }
      }
  
      public static void DecryptBlobFile(BlobStream bs, string key, string filePath)
      {
          try
          {
              PasswordDeriveBytes pdb = new PasswordDeriveBytes(key,
                  new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 
          0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
  
          Rijndael alg = Rijndael.Create();
  
          alg.Key = pdb.GetBytes(32);
          alg.IV =  pdb.GetBytes(16);
  
          CryptoStream cs = new CryptoStream(bs,
              alg.CreateDecryptor(), CryptoStreamMode.Read);
  
          // Decrypt &amp; Download Here
          System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
          System.Web.HttpContext.Current.Response.ContentType = "application/" + Path.GetExtension(filePath).Replace(".", "");
  
  
          int data;
          while ((data = cs.ReadByte()) != -1)
          {
              if (data != 0)
              {
              }
              System.Web.HttpContext.Current.Response.OutputStream.WriteByte((byte)data);
              System.Web.HttpContext.Current.Response.Flush();
  
          }
          cs.Close();
          bs.Close();
          }
          catch
          {
          }
      }
  </code></pre>
  
  <p>On downloading get following error</p>
  
  <pre><code>Server cannot set content type after HTTP headers have been sent.
  </code></pre>
  
  <p>Please suggest some solution.</p>