Azure Key Vault Encrypt and Decrypt File
Requirements
The Nautilus DevOps team is focusing on improving their data security by using Azure Key Vault. Your task is to create a Key Vault with a key and manage the encryption and decryption of a pre-existing sensitive file using this key.
Specific Requirements:
- Create a Key Vault:
- Name the Key Vault
nautilus-29948. - Set access policies to allow encryption and decryption operations.
- Create a Key:
- Create a symmetric key named
nautilus-keywithin the Key Vault for encryption and decryption operations.
- Encrypt the Sensitive Data:
- Use the key to encrypt the provided
SensitiveData.txtfile (located in/root/) on theazure-clienthost. - Base64 encode the ciphertext and save the encrypted version as
EncryptedData.binin the/root/directory.
- Verify Decryption:
- Attempt to decrypt
EncryptedData.binand verify that the decrypted data matches the originalSensitiveData.txtfile.
Ensure that the Key Vault and key are correctly configured. The validation script will test your configuration by decrypting the EncryptedData.bin file using the key you created.
Note
Use the same key name and key vault name exactly as required, because validation is name-sensitive.
Prerequisites
- Azure CLI installed and authenticated.
SensitiveData.txtexists in/root/.- Resource group exists and user has Key Vault permissions.
Steps
RG=$(az group list --query "[?contains(name, 'kml')].name" --output tsv)
KV_NAME="nautilus-29948"
KEY_NAME="nautilus-key"
LOCATION="eastus"
az keyvault create \
--name "$KV_NAME" \
--resource-group "$RG" \
--location "$LOCATION"
az keyvault key create \
--vault-name "$KV_NAME" \
--name "$KEY_NAME" \
--kty RSA
# Encrypt file content
PLAINTEXT_B64=$(base64 -w0 /root/SensitiveData.txt)
CIPHERTEXT=$(az keyvault key encrypt \
--vault-name "$KV_NAME" \
--name "$KEY_NAME" \
--algorithm RSA-OAEP \
--value "$PLAINTEXT_B64" \
--query result -o tsv)
echo "$CIPHERTEXT" | base64 -d > /root/EncryptedData.bin
# Decrypt and compare
ENC_B64=$(base64 -w0 /root/EncryptedData.bin)
DEC_PLAIN=$(az keyvault key decrypt \
--vault-name "$KV_NAME" \
--name "$KEY_NAME" \
--algorithm RSA-OAEP \
--value "$ENC_B64" \
--query result -o tsv)
echo "$DEC_PLAIN" | base64 -d > /root/DecryptedData.txt
cmp /root/SensitiveData.txt /root/DecryptedData.txt
Verification
- Verify key exists:
az keyvault key show --vault-name "$KV_NAME" --name "$KEY_NAME" --query key.kid -o tsv
- Verify encrypted file exists:
ls -l /root/EncryptedData.bin
- Verify decrypted file matches original:
cmp /root/SensitiveData.txt /root/DecryptedData.txt