Encrypt and decrypt blobs using Azure Key Vault - Azure Storage (2024)

  • Article

In this tutorial, you learn how to use client-side encryption to encrypt and decrypt blobs using a key stored with Azure Key Vault.

Azure Blob Storage supports both service-side and client-side encryption. For most scenarios, Microsoft recommends using service-side encryption features for ease of use in protecting your data. To learn more about service-side encryption, see Azure Storage encryption for data at rest.

The Azure Blob Storage client library for .NET supports client-side data encryption within applications before uploading to Azure Storage, and decrypting data while downloading to the client. The library also supports integration with Azure Key Vault for key management.

This tutorial shows you how to:

  • Configure permissions for an Azure Key Vault resource
  • Create a console application to interact with resources using .NET client libraries
  • Add a key to a key vault
  • Configure client-side encryption options using a key stored in a key vault
  • Create a blob service client object with client-side encryption enabled
  • Upload an encrypted blob, then download and decrypt the blob

Prerequisites

  • Azure subscription - create an account for free
  • Azure storage account - create a storage account
  • Key vault - create one using Azure portal, Azure CLI, or PowerShell
  • Visual Studio 2022 installed

Assign a role to your Microsoft Entra user

When developing locally, make sure that the user account that is accessing the key vault has the correct permissions. You'll need the Key Vault Crypto Officer role to create a key and perform actions on keys in a key vault. You can assign Azure RBAC roles to a user using the Azure portal, Azure CLI, or Azure PowerShell. You can learn more about the available scopes for role assignments on the scope overview page.

In this scenario, you'll assign permissions to your user account, scoped to the key vault, to follow the Principle of Least Privilege. This practice gives users only the minimum permissions needed and creates more secure production environments.

The following example shows how to assign the Key Vault Crypto Officer role to your user account, which provides the access you'll need to complete this tutorial.

Important

In most cases it will take a minute or two for the role assignment to propagate in Azure, but in rare cases it may take up to eight minutes. If you receive authentication errors when you first run your code, wait a few moments and try again.

  • Azure portal
  • Azure CLI
  • PowerShell
  1. In the Azure portal, locate your key vault using the main search bar or left navigation.

  2. On the key vault overview page, select Access control (IAM) from the left-hand menu.

  3. On the Access control (IAM) page, select the Role assignments tab.

  4. Select + Add from the top menu and then Add role assignment from the resulting drop-down menu.

  5. Use the search box to filter the results to the desired role. For this example, search for Key Vault Crypto Officer and select the matching result and then choose Next.

  6. Under Assign access to, select User, group, or service principal, and then choose + Select members.

  7. In the dialog, search for your Microsoft Entra username (usually your user@domain email address) and then choose Select at the bottom of the dialog.

  8. Select Review + assign to go to the final page, and then Review + assign again to complete the process.

Set up your project

  1. In a console window (such as PowerShell or Bash), use the dotnet new command to create a new console app with the name BlobEncryptionKeyVault. This command creates a simple "Hello World" C# project with a single source file: Program.cs.

    dotnet new console -n BlobEncryptionKeyVault
  2. Switch to the newly created BlobEncryptionKeyVault directory.

    cd BlobEncryptionKeyVault
  3. Open the project in your desired code editor. To open the project in:

    • Visual Studio, locate and double-click the BlobEncryptionKeyVault.csproj file.
    • Visual Studio Code, run the following command:
    code .

To interact with Azure services in this example, install the following client libraries using dotnet add package.

  • .NET CLI
  • PowerShell
dotnet add package Azure.Identitydotnet add package Azure.Security.KeyVault.Keysdotnet add package Azure.Storage.Blobs

Add the following using directives and make sure to add a reference to System.Configuration to the project.

using Azure;using Azure.Core;using Azure.Identity;using Azure.Security.KeyVault.Keys;using Azure.Security.KeyVault.Keys.Cryptography;using Azure.Storage;using Azure.Storage.Blobs;using Azure.Storage.Blobs.Models;using Azure.Storage.Blobs.Specialized;

Set environment variable

This application looks for an environment variable called KEY_VAULT_NAME to retrieve the name of your key vault. To set the environment variable, open a console window and follow the instructions for your operating system. Replace <your-key-vault-name> with the name of your key vault.

Windows:

You can set environment variables for Windows from the command line. However, when using this approach the values are accessible to all applications running on that operating system and may cause conflicts if you aren't careful. Environment variables can be set at either user or system level:

setx KEY_VAULT_NAME "<your-key-vault-name>"

After you add the environment variable in Windows, you must start a new instance of the command window. If you're using Visual Studio on Windows, you may need to relaunch Visual Studio after creating the environment variable for the change to be detected.

Linux:

export KEY_VAULT_NAME=<your-key-vault-name>

Add a key in Azure Key Vault

In this example, we create a key and add it to the key vault using the Azure Key Vault client library. You can also create and add a key to a key vault using Azure CLI, Azure portal, or PowerShell.

In the sample below, we create a KeyClient object for the specified vault. The KeyClient object is then used to create a new RSA key in the specified vault.

var keyName = "testRSAKey";var keyVaultName = Environment.GetEnvironmentVariable("KEY_VAULT_NAME");// URI for the key vault resourcevar keyVaultUri = $"https://{keyVaultName}.vault.azure.net";TokenCredential tokenCredential = new DefaultAzureCredential();// Create a KeyClient objectvar keyClient = new KeyClient(new Uri(keyVaultUri), tokenCredential);// Add a key to the key vaultvar key = await keyClient.CreateKeyAsync(keyName, KeyType.Rsa);

Create key and key resolver instances

Next, we'll use the key we just added to the vault to create the cryptography client and key resolver instances. CryptographyClient implements IKeyEncryptionKey and is used to perform cryptographic operations with keys stored in Azure Key Vault. KeyResolver implements IKeyEncryptionResolver and retrieves key encryption keys from the key identifier and resolves the key.

// Cryptography client and key resolver instances using Azure Key Vault client libraryCryptographyClient cryptoClient = keyClient.GetCryptographyClient(key.Value.Name, key.Value.Properties.Version);KeyResolver keyResolver = new (tokenCredential);

If you have an existing key in the vault that you'd like to encrypt with, you can create the key and key resolver instances by passing in the URI:

var keyVaultKeyUri = $"https://{keyVaultName}.vault.azure.net/keys/{keyName}";CryptographyClient cryptoClient = new CryptographyClient(new Uri(keyVaultKeyUri), tokenCredential);

Configure encryption options

Now we need to configure the encryption options to be used for blob upload and download. To use client-side encryption, we first create a ClientSideEncryptionOptions object and set it on client creation with SpecializedBlobClientOptions.

The ClientSideEncryptionOptions class provides the client configuration options for connecting to Blob Storage using client-side encryption. KeyEncryptionKey is required for upload operations and is used to wrap the generated content encryption key. KeyResolver is required for download operations and fetches the correct key encryption key to unwrap the downloaded content encryption key. KeyWrapAlgorithm is required for uploads and specifies the algorithm identifier to use when wrapping the content encryption key.

Important

Due to a security vulnerability in version 1, it's recommended to construct the ClientSideEncryptionOptions object using ClientSideEncryptionVersion.V2_0 for the version parameter. To learn more about mitigating the vulnerability in your apps, see Mitigate the security vulnerability in your applications. For more information about this security vulnerability, see Azure Storage updating client-side encryption in SDK to address security vulnerability.

// Configure the encryption options to be used for upload and downloadClientSideEncryptionOptions encryptionOptions = new (ClientSideEncryptionVersion.V2_0){ KeyEncryptionKey = cryptoClient, KeyResolver = keyResolver, // String value that the client library will use when calling IKeyEncryptionKey.WrapKey() KeyWrapAlgorithm = "RSA-OAEP"};// Set the encryption options on the client options.BlobClientOptions options = new SpecializedBlobClientOptions() { ClientSideEncryption = encryptionOptions };

Configure client object to use client-side encryption

In this example, we apply the client-side encryption configuration options to a BlobServiceClient object. When applied at the service client level, these encryption options are passed from the service client to container clients, and from container clients to blob clients. When the BlobClient object performs an upload or download operation, the Azure Blob Storage client libraries use envelope encryption to encrypt and decrypt blobs on the client side. Envelope encryption encrypts a key with one or more additional keys.

// Create a blob client with client-side encryption enabled.// Attempting to construct a BlockBlobClient, PageBlobClient, or AppendBlobClient from a BlobContainerClient// with client-side encryption options present will throw, as this functionality is only supported with BlobClient.Uri blobUri = new (string.Format($"https://{accountName}.blob.core.windows.net"));BlobClient blob = new BlobServiceClient(blobUri, tokenCredential, options).GetBlobContainerClient("test-container").GetBlobClient("testBlob");

Encrypt blob and upload

When the BlobClient object calls an upload method, several steps occur to perform the client-side encryption:

  1. The Azure Storage client library generates a random initialization vector (IV) of 16 bytes and a random content encryption key (CEK) of 32 bytes, and performs envelope encryption of the blob data using this information.
  2. Blob data is encrypted using the CEK.
  3. The CEK is then wrapped (encrypted) using the key encryption key (KEK) we specified in ClientSideEncryptionOptions. In this example, the KEK is an asymmetric key pair stored in the specified Azure Key Vault resource. The blob client itself never has access to the KEK, it just invokes the key wrapping algorithm that is provided by Key Vault.
  4. The encrypted blob data is then uploaded to the storage account.

Add the following code to encrypt a blob and upload it to your Azure storage account:

// Upload the encrypted contents to the blobStream blobContent = BinaryData.FromString("Ready for encryption, Captain.").ToStream();await blob.UploadAsync(blobContent);

Once the blob is uploaded, you can view the blob in your storage account to see the encrypted contents along with the encryption metadata.

Decrypt blob and download

The Azure Storage client library assumes that the user is managing the KEK either locally or in a key vault. The user doesn't need to know the specific key that was used for encryption. The key resolver specified in ClientSideEncryptionOptions will be used to resolve key identifiers when blob data is downloaded and decrypted.

When the BlobClient object calls a download method, several steps occur to decrypt the encrypted blob data:

  1. The client library downloads the encrypted blob data, including encryption metadata, from the storage account.
  2. The wrapped CEK is then unwrapped (decrypted) using the KEK. The client library doesn't have access to the KEK during this process, but only invokes the key unwrapping algorithm specified in ClientSideEncryptionOptions. The private key of they RSA key pair remains in the key vault, so the encrypted key from the blob metadata that contains the CEK is sent to the key vault for decryption.
  3. The client library uses the CEK to decrypt the encrypted blob data.

Add the following code to download and decrypt the blob that you previously uploaded.

// Download and decrypt the encrypted contents from the blobResponse<BlobDownloadInfo> response = await blob.DownloadAsync();BlobDownloadInfo downloadInfo = response.Value;Console.WriteLine((await BinaryData.FromStreamAsync(downloadInfo.Content)).ToString());

Next steps

In this tutorial, you learned how to use .NET client libraries to perform client-side encryption for blob upload and download operations.

For a broad overview of client-side encryption for blobs, including instructions for migrating encrypted data to version 2, see Client-side encryption for blobs.

For more information about Azure Key Vault, see the Azure Key Vault overview page

Encrypt and decrypt blobs using Azure Key Vault - Azure Storage (2024)

FAQs

How to encrypt blob storage in Azure? ›

How client-side encryption works
  1. The Azure Storage client library generates a content encryption key (CEK), which is a one-time-use symmetric key.
  2. User data is encrypted using the CEK.
  3. The CEK is then wrapped (encrypted) using the key encryption key (KEK). ...
  4. The encrypted data is then uploaded to Azure Blob Storage.
Aug 7, 2024

How to encrypt data using Azure key Vault? ›

  1. Prerequisites.
  2. Assign a role to your Microsoft Entra user.
  3. Set up your project.
  4. Set environment variable.
  5. Add a key in Azure Key Vault.
  6. Create key and key resolver instances.
  7. Configure encryption options.
  8. Configure client object to use client-side encryption.
Nov 15, 2022

How to decrypt a blob? ›

You can use either a keyObject or a passPhrase to decrypt the BLOB:
  1. keyObject: a JSON object containing the encryption key, with the same structure as the object returned by the New data key command.
  2. passPhrase: a string used to generate the encryption key.

Can you make use of the Azure key Vault service for the storage of encryption keys? ›

Secure key management is essential to protect data in the cloud. Use Azure Key Vault to encrypt keys and small secrets like passwords that use keys stored in hardware security modules (HSMs).

How do I access Azure Blob Storage using access key? ›

To access Azure Blob Storage using the access key, you need to create a storage account and obtain the account access key. You can then use the key to authenticate your access to Blob Storage.

Which two actions can you perform by using Azure key Vault? ›

Secrets Management - Azure Key Vault can be used to Securely store and tightly control access to tokens, passwords, certificates, API keys, and other secrets. Key Management - Azure Key Vault can be used as a Key Management solution.

What is the difference between key and secret in Azure key Vault? ›

A secret is anything that you want to tightly control access to, such as API keys, passwords, or certificates. A key is a cryptographic key represented as a JSON Web Key [JWK] object. Key Vault supports RSA and Elliptic Curve Keys only.

Are Azure key vault secrets encrypted? ›

The Azure Key Vault service encrypts your secrets when you add them, and decrypts them automatically when you read them.

How do I decrypt Azure disk encryption? ›

You can remove the encryption extension using Azure PowerShell or the Azure CLI. Disable disk encryption with Azure PowerShell: To remove the encryption, use the Remove-AzVMDiskEncryptionExtension cmdlet.

How to enable client-side encryption in Azure key Vault? ›

Use Automatic Client-Side Field Level Encryption with Azure
  1. Overview.
  2. Before You Get Started.
  3. Set Up the KMS.
  4. Register your Application with Azure.
  5. Create the Customer Master Key.
  6. Create the Application.
  7. Create a Unique Index on your Key Vault collection.
  8. Create a New Data Encryption Key.

How do I decrypt encrypted payload? ›

Open a command line window and change to the local folder containing the matched-data-cli binary. Replace <PRIVATE_KEY> with your private key and <ENCRYPTED_PAYLOAD> with the encrypted payload. Note: The first printf command will make your private key visible in your command history.

What are the different types of Azure key vault keys? ›

Azure Key Vault provides two types of resources to store and manage cryptographic keys. Vaults support software-protected and HSM-protected (Hardware Security Module) keys. Managed HSMs only support HSM-protected keys.

How to create a secret in Azure key Vault? ›

Add a secret to Key Vault
  1. Navigate to your key vault in the Azure portal:
  2. On the Key Vault left-hand sidebar, select Objects then select Secrets.
  3. Select + Generate/Import.
  4. On the Create a secret screen choose the following values: Upload options: Manual. Name: Type a name for the secret.
Aug 7, 2024

How many key vaults should you use Azure? ›

Our recommendation is to use a vault per application per environment (development, preproduction, and production), per region. Granular isolation helps you not share secrets across applications, environments and regions, and it also reduce the threat if there is a breach.

What would be the best way to protect the data for Azure storage blob? ›

Recommendations for basic data protection
  1. Configure an Azure Resource Manager lock on the storage account to protect the account from deletion or configuration changes. ...
  2. Enable container soft delete for the storage account to recover a deleted container and its contents. ...
  3. Save the state of a blob at regular intervals:
Jul 29, 2024

How do I restrict access to Azure Blob? ›

2 answers
  1. Navigate to your storage account in the Azure portal.
  2. Go to the Containers blade and select the container you want to restrict access to.
  3. Click on Access control (IAM) from the left-hand menu of the container.
  4. Click on Add role assignment .
  5. In the role assignment pane:
May 28, 2024

How do I encrypt my Azure data disk? ›

Under Encryption settings > Disks to encrypt, select OS and data disks. Under Encryption settings, choose Select a key vault and key for encryption. On the Select key from Azure Key Vault screen, select Create New. To the left of Key vault and key, select Click to select a key.

How do I encrypt an Azure database? ›

When you use client-side encryption with Key Vault, your data is encrypted using a one-time symmetric Content Encryption Key (CEK) that is generated by the Azure Storage client SDK. The CEK is encrypted using a Key Encryption Key (KEK), which can be either a symmetric key or an asymmetric key pair.

Top Articles
Dealing with a Financially Irresponsible Family Member
Who Gets the Money When a Company is Sold?
Use Copilot in Microsoft Teams meetings
2018 Jeep Wrangler Unlimited All New for sale - Portland, OR - craigslist
Pinellas County Jail Mugshots 2023
Catsweb Tx State
Gt Transfer Equivalency
Marion County Wv Tax Maps
Rhinotimes
Becu Turbotax Discount Code
Parent Resources - Padua Franciscan High School
Metro Pcs.near Me
Traveling Merchants Tack Diablo 4
Nevermore: What Doesn't Kill
Pjs Obits
Riversweeps Admin Login
Elbert County Swap Shop
Mythical Escapee Of Crete
Inkwell, pen rests and nib boxes made of pewter, glass and porcelain.
Cpt 90677 Reimbursem*nt 2023
Pain Out Maxx Kratom
Craigslist Brandon Vt
Ewg Eucerin
Issue Monday, September 23, 2024
Mumu Player Pokemon Go
EST to IST Converter - Time Zone Tool
Bee And Willow Bar Cart
1400 Kg To Lb
Arcane Odyssey Stat Reset Potion
Hotels Near New Life Plastic Surgery
Powerspec G512
Baywatch 2017 123Movies
10 games with New Game Plus modes so good you simply have to play them twice
Tiny Pains When Giving Blood Nyt Crossword
Tryst Houston Tx
San Bernardino Pick A Part Inventory
Craigslist Pets Plattsburgh Ny
Shane Gillis’s Fall and Rise
Riverton Wyoming Craigslist
Academy Sports New Bern Nc Coupons
Craigslist Com Panama City Fl
Emily Tosta Butt
Setx Sports
Myrtle Beach Craigs List
Grand Valley State University Library Hours
Sandra Sancc
Greatpeople.me Login Schedule
Market Place Tulsa Ok
300 Fort Monroe Industrial Parkway Monroeville Oh
Rise Meadville Reviews
Ret Paladin Phase 2 Bis Wotlk
Latest Posts
Article information

Author: Terence Hammes MD

Last Updated:

Views: 5571

Rating: 4.9 / 5 (49 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Terence Hammes MD

Birthday: 1992-04-11

Address: Suite 408 9446 Mercy Mews, West Roxie, CT 04904

Phone: +50312511349175

Job: Product Consulting Liaison

Hobby: Jogging, Motor sports, Nordic skating, Jigsaw puzzles, Bird watching, Nordic skating, Sculpting

Introduction: My name is Terence Hammes MD, I am a inexpensive, energetic, jolly, faithful, cheerful, proud, rich person who loves writing and wants to share my knowledge and understanding with you.