简体   繁体   中英

Encrypting image data before uploading to azure blob storage

I have the following code that uploads an image to Azure blob storage. I would like to encrypt the image data before uploading to the blob. I already have a helper class for encrypting and decrypting that I can use by calling AESEncryption.Encrypt("plainText", "key", salt");

I'm just trying to figure out how tom integrate my encryption method into the code. Also, I'm guessing that once it's encrypted instead of calling blob.UploadFromFile() I will be calling blob.UploadFromByteArray().

public override Task ExecutePostProcessingAsync()
    {
        try
        {
            // Upload the files to azure blob storage and remove them from local disk
            foreach (var fileData in this.FileData)
            {
                var filename = BuildFilename(Path.GetExtension(fileData.Headers.ContentDisposition.FileName.Trim('"')));

                // Retrieve reference to a blob
                var blob = _container.GetBlockBlobReference(filename);
                blob.Properties.ContentType = fileData.Headers.ContentType.MediaType;
                blob.UploadFromFile(fileData.LocalFileName, FileMode.Open);
                File.Delete(fileData.LocalFileName);
                Files.Add(new FileDetails
                {
                    ContentType = blob.Properties.ContentType,
                    Name = blob.Name,
                    Size = blob.Properties.Length,
                    Location = blob.Uri.AbsoluteUri
                });
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return base.ExecutePostProcessingAsync();
    }

As I see it, you could do it 3 ways:

  1. Encrypt the file beforehand and then upload that encrypted file.
  2. As you mentioned, you could read the file in byte array and then encrypt that byte array and upload it using UploadFromByteArray method.
  3. Similar to #2 but instead of uploading byte array, you could rely on streams and upload using UploadFromStream method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM