none
Issue in using MemoryStream - Upload/Download the Azure Blob file using AesCryptoServiceProvider Encrypt and Decrypt algorithm

    Question

  • We have encrypting file and storing in the Azure blob using AesCryptoServiceProvider, no issues in storing the file in the Azure blob.

    When we try to decrypt and download the file using MemomryStream, file is not downloading from the blob.

    Always, MemoryStream will be empty. Please let us know if any solution to decrypt and download Azure Blob using MemoryStream

    Below code is being used.

     CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["TestConnectionString"]);

          CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
         CloudBlobContainer container = blobClient.GetContainerReference("TestStorage");
         container.CreateIfNotExists();
                CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
                using (Stream blobStream = blockBlob.OpenRead(AccessCondition.GenerateEmptyCondition(), null, null))
                {

                      Stream st = DecryptAzureBlob(blobStream, KeyIdentifier);
                    
                      using (var memoryStream = new MemoryStream())
                      {
                        fileStream.Write(memoryStream);
                      }
                        Response.Clear();
                        Response.ContentType = blockBlob.Properties.ContentType;
                        Response.AddHeader("content-disposition", "attachment;    filename=" + blobName.ToString());
                        Response.BinaryWrite(memoryStream.ToArray());
                        Response.End();
                }


     Decryption Code
     Public Stream DecryptAzureBlob(Stream blobStream, string KeyIdentifier)
     {
            using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
                {  
                    byte[] ivBytes = new byte[aesAlg.BlockSize / 8];
                    streamToDecrypt.Read(ivBytes, 0, ivBytes.Length);

                 
                    aesAlg.Key = GetEncryptedKey(KeyIdentifier);  // getting the key from Azure Blob
                    aesAlg.IV = ivBytes;

                   ICryptoTransform decryptor = aesAlg.CreateDecryptor();

                   CryptoStream cryptoStream = new CryptoStream(streamToDecrypt, decryptor, CryptoStreamMode.Read);

                   return cryptoStream;

               }
    }


    Marulasiddappa SB (Swamy)








    Thursday, January 07, 2016 6:24 PM

All replies