Lesson 1 of 2 Data Encryption

Encryption Layers and Column-Level Encryption

This lesson places TLS, TDE and column encryption in context and explains how server-side column-level encryption works.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish between encryption in transit, TDE and column encryption.
  • Describe the key hierarchy of column-level encryption.
  • Explain who can decrypt data protected by keys stored in the database.

Layers of encryption

Encryption in transit uses TLS between clients and the database; Azure SQL Database requires encrypted connections, and clients should validate the server certificate (Encrypt=True or Strict, without TrustServerCertificate). Encryption at rest with transparent data encryption (TDE) encrypts data files, log files and backups with a database encryption key; it is on by default in Azure SQL Database and SQL database in Fabric, with a service-managed key or a customer-managed key in Azure Key Vault. TDE protects against stolen files and backups, but anyone who can query the database sees plaintext.

Protecting individual values from people who can query the database needs column encryption: server-side column-level encryption or client-side Always Encrypted. The difference is where the keys are and who can therefore decrypt.

The following diagram shows the layer each encryption method protects.

Layers of encryption and what each protects. TLS encrypts data in transit between client and database. Transparent data encryption encrypts data files, logs and backups at rest, with a service-managed or customer-managed key, but anyone who can query sees plaintext. Column encryption protects values from people who can query: server-side column-level encryption keeps keys in the database, while Always Encrypted keeps them with the client so the database engine never sees plaintext.
Where the keys are decides who can decrypt.
Sort into groups

Which layer protects it?

Exam skills 2.1.1

Choose the encryption layer that addresses each threat.

Log in to try this activity Interactive activities are free with an account.

Server-side column-level encryption

Column-level encryption uses T-SQL functions. A symmetric key (for example AES_256) is protected by a certificate, which is protected by the database master key. Code opens the key, calls ENCRYPTBYKEY on write and DECRYPTBYKEY on read, and closes the key. Encrypted values are stored as varbinary, so the column cannot be indexed for searching by value; a separate hashed column (for example HASHBYTES with a salt) supports equality lookups.

CREATE MASTER KEY ENCRYPTION BY PASSWORD = '<strong password>';
CREATE CERTIFICATE PayrollCert WITH SUBJECT = 'Payroll column key protector';
CREATE SYMMETRIC KEY PayrollKey WITH ALGORITHM = AES_256 ENCRYPTION BY CERTIFICATE PayrollCert;

OPEN SYMMETRIC KEY PayrollKey DECRYPTION BY CERTIFICATE PayrollCert;
UPDATE HR.Employee SET SalaryEnc = ENCRYPTBYKEY(KEY_GUID('PayrollKey'), CONVERT(varbinary(16), Salary));
SELECT EmployeeID, CONVERT(decimal(12,2), CONVERT(varbinary(16), DECRYPTBYKEY(SalaryEnc))) AS Salary
FROM HR.Employee;
CLOSE SYMMETRIC KEY PayrollKey;

Because the server performs encryption with keys stored in the database, any principal with permission to open the key, such as a database owner or a sysadmin, can decrypt the data. Column-level encryption therefore protects values from users who can read the table but not open the key, not from administrators.

Put in order

Setting up column-level encryption

Exam skills 2.1.1

Put the steps of server-side column-level encryption in order.

Log in to try this activity Interactive activities are free with an account.

Choosing an encryption approach

The following comparison shows which feature addresses which threat.

TDE
Protects againstStolen files and backups
Where encryption happensStorage engine, transparently
Application changesNone
Queries on encrypted valuesUnaffected
Choose when Baseline compliance for data at rest
Column-level encryption
Protects againstUsers who can read the table but not open the key
Where encryption happensServer, through T-SQL functions
Application changesQueries call ENCRYPTBYKEY and DECRYPTBYKEY
Queries on encrypted valuesOnly after decryption
Choose when Server-side protection with key permissions
Always Encrypted
Protects againstAnyone without the column master key, including DBAs and cloud operators
Where encryption happensClient driver
Application changesConnection setting and parameterized queries
Queries on encrypted valuesEquality (deterministic), more with enclaves
Choose when Data that administrators must never see
Scenario questions

Which encryption?

Exam skills 2.1.1

Choose the encryption approach that fits each requirement.

Log in to try this activity Interactive activities are free with an account.
Worked example

What does TDE protect?

A compliance auditor asks whether TDE in Azure SQL Database prevents the database administrator from reading customer passport numbers. The policy requires that only the passport office application can read them in plaintext. What should the developer recommend?

TDE does not meet the requirement because it decrypts data transparently for anyone who can query it. Encrypt the passport column with Always Encrypted, with the column master key in Azure Key Vault and key access granted only to the passport office application's identity. Column-level encryption with a certificate in the database would not satisfy the policy because the administrator could open the key.

Check yourself

Can a database owner read data protected by column-level encryption with a certificate in the same database?

Show answer

Yes, if they can open the key, because the keys are stored in the database and decryption happens on the server.

Exam Tips

Remember for the exam

  • TDE protects files and backups at rest; queries still see plaintext.
  • Column-level encryption: ENCRYPTBYKEY/DECRYPTBYKEY with a symmetric key protected by a certificate.
  • Keys stored in the database mean administrators can decrypt.
  • Validate the server certificate: Encrypt=True without TrustServerCertificate.