SQL Server Documentation

Security / Encryption

Transparent Data Encryption (TDE)

Applies to: SQL Server 2008 and later versions

Transparent Data Encryption (TDE) encrypts data files of a SQL Server database at rest. This includes the data and log files. TDE also encrypts backups. TDE encrypts data at the storage level, not at the application level.

How TDE Works

TDE protects data against the threat of unauthorized access to the physical files or their copies. It works by encrypting the entire database, including data and log files, using a symmetric key called the Database Encryption Key (DEK). This DEK is then protected by a Service Master Key (SMK), which is a master key managed by the SQL Server instance. For enhanced security and manageability, the DEK can also be protected by an Asymmetric Key or an X.509 Certificate.

Important: TDE encrypts data at rest, meaning the files on disk. It does not encrypt data in transit (network traffic) or data while it is being processed in memory. For these scenarios, consider other encryption methods like Always Encrypted or SSL/TLS.

Key Components of TDE

Benefits of TDE

Steps to Implement TDE

Implementing TDE involves several steps, primarily focused on creating and managing encryption keys:

  1. Create a Master Key: You can create a master key for the database, typically an asymmetric key or a certificate.
  2. -- Example: Creating a certificate for TDE
    CREATE CERTIFICATE MyDatabaseCert
       WITH SUBJECT = 'TDE Certificate for MyDatabase',
       EXPIRY_DATE = '2025-12-31';
    GO
    
    -- Example: Creating an asymmetric key to protect the certificate
    CREATE ASYMMETRIC KEY MyDatabaseKey
       FROM CERTIFICATE MyDatabaseCert;
    GO
    
    -- Example: Creating a master key for the database
    CREATE MASTER KEY ENCRYPTION BY ASYMMETRIC KEY MyDatabaseKey;
    GO
  3. Create a Database Encryption Key (DEK): This key will be used to encrypt the database.
  4. -- Example: Creating a DEK protected by the master key
    CREATE DATABASE ENCRYPTION KEY
       WITH ALGORITHM = AES_256
       ENCRYPTION BY SERVER MASTER KEY; -- Or BY CERTIFICATE MyDatabaseCert / BY ASYMMETRIC KEY MyDatabaseKey
    GO
  5. Enable TDE: Apply the encryption to the database.
  6. -- Example: Enabling TDE for a database
    ALTER DATABASE MyDatabase
    SET ENCRYPTION = ON;
    GO
  7. Backup Keys: Crucially, back up your master key and certificate/asymmetric key. Without these, you cannot decrypt your data if the database files are lost or corrupted.
  8. -- Example: Backing up a certificate
    BACKUP CERTIFICATE MyDatabaseCert
       TO FILE = 'C:\Certificates\MyDatabaseCert.cer'
       WITH PRIVATE KEY (
          FILE = 'C:\Certificates\MyDatabaseCert.key',
          ENCRYPTION BY PASSWORD = 'YourStrongPassword!'
       );
    GO
Security Best Practice: Always back up your database encryption keys (certificates or asymmetric keys) and the master key to a secure, offline location. Losing these keys will make your encrypted data irrecoverable.

Managing TDE

Considerations