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();
    }

Answers


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.

Need Your Help

Timezone in MySql and Rails

mysql ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.2

In my application I am storing records in the database in the UTC format. I just want that while displaying those records at the user end The result will search according to the user's time zone. M...

Taking screenshots in Rails

ruby-on-rails capybara

I'm trying to take screenshots of pages in a Rails 4 app. I've tried several ways but the cleanest seems to be by using capybara with poltergeist.