Encrypting an Entire Database using Transparent Database Encryption (TDE) on Sql Server
I needed to encrypt a database for a client. You can do this by following the information on the page below. In my case I am using a Docker instance to test this out... and of course it uses my super secret password (which you will recognize if you took my database class).
Information was learned starting here: Enable Transparent Data Encryption (TDE)
1 USE master;
2 GO
3
4 CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'ConstraintDB123!';
5 GO
6 CREATE CERTIFICATE MyDockerCert WITH SUBJECT = 'My DEK Certificate';
7 GO
8 USE UniversitySmall;
9 GO
10 CREATE DATABASE ENCRYPTION KEY
11 WITH ALGORITHM = AES_256
12 ENCRYPTION BY SERVER CERTIFICATE MyDockerCert;
13 GO
14 /* Warning: The certificate used for encrypting the database encryption key has not been backed up.
15 You should immediately back up the certificate and the private key associated with the certificate.
16 If the certificate ever becomes unavailable or if you must restore or attach the database on another
17 server, you must have backups of both the certificate and the private key or you will not be able to
18 open the database. */
19
20 ALTER DATABASE UniversitySmall
21 SET ENCRYPTION ON;
The comment above is the output of creating the Database Encryption Key (DEK). Consequently, we need to backup the recommended keys and certificates. To identify keys that have not been backed up:
To backup the Certificate see: Backup Certificate.
By default these are stored in the DATA folder of the SQL Server. On Docker this is in /var/opt/mssql/data. If you want to you can specify the place for the files to be stored using either a local file or a UNC path.
Once you have downloaded them to a secure location, you should delete the Certificate and Private Key files from the data directory.
To see the encryption state:
For information on the output see here. IMPORTANT: percent_complete is not how much of the database has been encrypted, but rather the percent complete of the database encryption state change AND WILL BE 0 if there is not state change. If the database was in the process of being encrypted, the encryption_state = 2.
Note: FILESTREAM data isn't encrypted, even when you enable TDE.