How to protect a SQL Server database with Transparent Data Encryption

Did you know SQL Server can protect your data even if someone gets a copy of your database files or backups? The feature is called Transparent Data Encryption (TDE).

TDE encrypts your database files, transaction logs, and backups at rest. SQL Server encrypts pages as it writes them to disk and decrypts them as it reads them, so applications can keep running the same queries. That work can add CPU and I/O overhead, so measure the impact on your workload before enabling TDE in production. Microsoft Learn: Transparent Data Encryption

You can still copy an encrypted database to another server. To attach it or restore a backup from it, the destination SQL Server instance needs the certificate and private key that protect the database encryption key. If you use an external key manager, the destination needs access to the corresponding key. Microsoft Learn: Move a TDE-protected database

  1. Create and back up the TDE certificate

This example uses a test database called DEKExample. Use a destination SQL Server version that supports TDE and is the same version as the source or a supported newer version. SQL Server cannot attach a database or restore a backup created by a newer version onto an older one.

Run the following on the source instance. Create a master key in master only if one doesn’t already exist. Replace the password placeholders and use paths on the source server that the SQL Server service account can write to.

USE master;

GO

CREATE MASTER KEY ENCRYPTION BY PASSWORD = '';

GO

CREATE CERTIFICATE TestSQLServerCert

WITH SUBJECT = 'Certificate to protect the TDE database encryption key';

GO

BACKUP CERTIFICATE TestSQLServerCert

TO FILE = 'C:\TDE\TestSQLServerCert.cer'

WITH PRIVATE KEY (

FILE = 'C:\TDE\TestSQLServerCert.pvk',

ENCRYPTION BY PASSWORD = ''

);

GO

Keep the certificate, private key, and private-key backup password in secure storage. You’ll need all three for the recovery test.

  1. Encrypt the sample database

Create a database encryption key protected by the certificate, then turn on encryption.

USE DEKExample;

GO

CREATE DATABASE ENCRYPTION KEY

WITH ALGORITHM = AES_256

ENCRYPTION BY SERVER CERTIFICATE TestSQLServerCert;

GO

ALTER DATABASE DEKExample SET ENCRYPTION ON;

GO

SQL Server encrypts the existing database in the background. Check that encryption_state reaches 3 (Encrypted) before taking the backup you’ll use for the test.

SELECT encryption_state, percent_complete

FROM sys.dm_database_encryption_keys

WHERE database_id = DB_ID(N'DEKExample');

  1. Test what happens on another SQL Server instance

Take a full backup of DEKExample and copy it to the destination server. If you try to restore it before importing the TDE certificate, SQL Server can return error 33111, which reports that it cannot find the server certificate with the required thumbprint.

You can test the same behavior with detach and attach using a disposable sample database. Copy all of its data and log files, including any .ndf files, and try attaching it on the destination.

  1. Import the certificate and retry

Copy TestSQLServerCert.cer and TestSQLServerCert.pvk to a protected location that the destination SQL Server service account can read. Run the following on the destination instance. As on the source, create the master key only if master doesn’t already have one.

USE master;

GO

CREATE MASTER KEY ENCRYPTION BY PASSWORD = '';

GO

CREATE CERTIFICATE TestSQLServerCert

FROM FILE = 'C:\TDE\TestSQLServerCert.cer'

WITH PRIVATE KEY (

FILE = 'C:\TDE\TestSQLServerCert.pvk',

DECRYPTION BY PASSWORD = ''

);

GO

The private-key password must match the one you used in BACKUP CERTIFICATE. The destination master key can have its own password.

Now retry the restore or attach. Once the database is online, run a query against a known table to verify that you can read the data. That gives you a practical recovery test for both the database backup and the certificate that protects it.