4

I have created a MVC WebRole Window Azure application where i upload encrypted files to Azure blob storage using SymmetricAlgorithm (Rijndael) like this

Controler>Action is

[HttpPost]
public ActionResult UploadImage_post(HttpPostedFileBase fileBase)
{
    if (fileBase.ContentLength > 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();
    }

The above File encryption is working fine.

For Downloading code is

 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 & 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
        {
        }
    }

On downloading get following error

Server cannot set content type after HTTP headers have been sent.

Please suggest some solution.

Anil D
  • 1,989
  • 6
  • 29
  • 60
  • You could try spoofing the file type. If the file is myReport.pdf rename to myReportpdf.txt. I do something similar with an application pulling a crystal reports .rpt file. we rename it to a zip when we post it to the website. – dave k May 19 '14 at 18:30
  • @davek, changing file extension, does not make it open as text file instead of pdf? and it doesn't work.. – Anil D May 19 '14 at 19:02
  • Hey @davek, actually no file is downloading and i m getting an error "Server cannot set content type after HTTP headers have been sent.", i have edited my question – Anil D May 19 '14 at 19:24
  • Sorry, thought you were able to originally download a txt file type. Does the upload work? – dave k May 20 '14 at 03:14
  • yes upload works fine.. – Anil D May 20 '14 at 03:34
  • Check out this link: http://code.msdn.microsoft.com/windowsazure/How-To-Use-Azure-Blob-16882fe2 . They have the download process a little different then you. Try downloading it first without decrypting the file. – dave k May 20 '14 at 03:37
  • i have used this code and works fine, but not sure how to use it with encrypion/decryption.. – Anil D May 20 '14 at 04:56

1 Answers1

2

This should be fairly simple, hope this is enough to get you started:

public class CloudFileResult : ActionResult
{
  private string m_FileName;
  private CloudBlobContainer m_Container;

  public CloudFileResult(string imageName, CloudBlobContainer container)
  {
    if (string.IsNullOrEmpty(imageName))
    {
      throw new ArgumentNullException("imageName");
    }
    if (container == null)
    {
      throw new ArgumentNullException("container");
    }

    m_FileName = imageName;
    m_Container = container;
  }

  public override void ExecuteResult(ControllerContext context)
  {
    context.HttpContext.Response.Clear();
    var blockBlob = m_Container.GetBlockBlobReference(m_FileName);
    blockBlob.FetchAttributes();
    context.HttpContext.Response.ContentType = blockBlob.Metadata["ContentType"];
    const string key = "my secret";
    using (var pdb = new Rfc2898DeriveBytes(key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }))
    {
      using (var alg = RijndaelManaged.Create())
      {
        alg.Key = pdb.GetBytes(32);
        alg.IV = pdb.GetBytes(16);
        using (var stream = new CryptoStream(context.HttpContext.Response.OutputStream, alg.CreateDecryptor(), CryptoStreamMode.Write))
        {
          blockBlob.DownloadToStream(stream);
        }
      }
    }
  }
}

static void UploadFileToCloud(CloudBlobContainer container, HttpPostedFileBase file)
{
  const string key = "my secret";
  using (var pdb = new Rfc2898DeriveBytes(key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }))
  {
    using (var alg = RijndaelManaged.Create())
    {
      alg.Key = pdb.GetBytes(32);
      alg.IV = pdb.GetBytes(16);

      var blockBlob = container.GetBlockBlobReference(file.FileName);
      using (var stream = new CryptoStream(file.InputStream, alg.CreateEncryptor(), CryptoStreamMode.Read))
      {
        blockBlob.UploadFromStream(stream);
      }
      blockBlob.Metadata.Add("ContentType", file.ContentType);
      blockBlob.SetMetadata();
    }
  }
}

static CloudBlobContainer GetContainer()
{
  string connection = "DefaultEndpointsProtocol=http;AccountName=AzureAccount;AccountKey=AzureAccountKey;";
  var account = CloudStorageAccount.Parse(connection);
  var client = account.CreateCloudBlobClient();
  var container = client.GetContainerReference("container");
  return container;
}

As for download you can simple use:

[HttpGet]
public ActionResult Index(string fileName)
{
  if (!string.IsNullOrEmpty(fileName))
  {
    return new CloudFileResult(fileName, GetContainer());
  }
  return View();
}

Pointers:

  • I prefer using managed crypto algorithms
  • I store contenttype of original file in blob metadata (so you know how to serve it)
  • catch {} gives me creeps, at least log the exception somewhere
  • Rather than playing with HttpContext.Response, create custom ActionResult
  • Always dispose IDisposable stuff
Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89
  • Just in case it helps anyone, I kept running into the dreaded error:http://stackoverflow.com/questions/8583112/padding-is-invalid-and-cannot-be-removed". I added the following line: alg.Padding = System.Security.Cryptography.PaddingMode.PKCS7; before the alg.Key line and that fixed it. – Adrian Carr Aug 05 '14 at 03:51
  • Hello guys, is there any useful link to do this in .Net core? – Neel Aug 06 '19 at 08:28