Implementing TDE
Implementing TDE
1. Make a duplicate of the existing database with a different name. The new database name is the
one we will use for this lab.
1. Log into SSMS and create a Master Key. We must first create the master key. It must be created
in the master database, so as a precautionary measure you should begin this statement with the
USE MASTER command. Create a query as follows and execute it:
ALTER SERVICE MASTER KEY FORCE REGENERATE;
GO
USE MASTER;
GO
CREATE MASTER KEY ENCRYPTION BY PASSWORD= 'putAveryStrongPasswordHere';
GO
3. Create a Certificate that is protected by the Master Key. The certificate’s name is “my_cert” and I
gave it a generic subject (the subject I chose is the actual name of the database we are about to
encrypt).
*** You will get a warning here about the importance of backing up your keys. ***
7. Exit out of the Database you want to encrypt (step 5). If there are any active connections the
encryption will not complete.
USE master;
GO
8. Enable Encryption - Finally, we can enable encryption on our database by using the ALTER
DATABASE command. Once the encryption is turned on, depending on the size of the database, it
may take some time to complete.
ALTER DATABASE AdventureWorks2019_new
SET ENCRYPTION ON;
GO
You can monitor the status by querying the sys.dm_database_encryption_keys DMV. We're looking for
encryption_state = 3. This may take awhile so be patient. Run the following SELECT statement until the
column reads 3 for all rows.
Value Description
0 No database encryption key present, no encryption
1 Unencrypted
2 Encryption in progress
3 Encrypted
4 Key change in progress
5 Decryption in progress
6 The certificate or asymmetric key encrypting the DEK is being changed
9. Backup Your Certificate and Private Key - It’s important to backup the certificate you created
and store it in a secure location. If the server ever goes down and you need to restore it
elsewhere, you will have to import the certificate to the server. Remember to store the
certificate in a safe and available locations (not a temporary one like this example) and
remember the password.
USE MASTER;
GO
BACKUP CERTIFICATE my_cert
TO FILE = ‘c:\temp\my_cert’
WITH PRIVATE KEY (file = ‘c:\temp\my_certKey.pvk’, ENCRYPTION BY PASSWORD = ‘access’);
GO
Above we generated a file for both the certificate and the private key, as well as providing a password
for the private key.
***** Now let’s assume we want to restore the database on a different server *****
10. *** Backup the AdventureWorks2019_new database to c:\temp. Call the backup
'c:\temp\AdventureWorks2019_new.bak'***
11. Be sure to do step 10 above (backup the database to c:\temp) because we are about to remove
encryption to simulate restoring our encrypted database to a new server. We can pretend a
hacker or a disgruntled user has got a copy of your encrypted database and wishes to read its
contents.
USE [AdventureWorks2019_new]
DROP DATABASE ENCRYPTION KEY;
GO
15. Double check the layout of the files. This is needed if the next query does not work.
USE master
GO
SELECT
DB_NAME([database_id]) [database_name]
, [file_id]
, [type_desc] [file_type]
, [name] [logical_name]
, [physical_name]
FROM sys.[master_files]
WHERE [database_id] IN (DB_ID('AdventureWorks2019_new'),
DB_ID('AdventureWorks2019_new'))
ORDER BY [type], DB_NAME([database_id]);
16. The first scenario for restoring a TDE protected database is the case where we try to do the
restore and we have none of the encryption pieces in place. We don't have the database master
key and we certainly don't have the certificate. This is why TDE is great. If you don't have these
pieces, the restore simply won't work. Let's attempt the restore
17. With the GUI you might not see the error message so you can try it in a query
USE MASTER
GO
RESTORE DATABASE AdventureWorks2019_copy FROM DISK =
'c:\temp\AdventureWorks2019_new.bak'
WITH
MOVE 'AdventureWorks2019' TO 'c:\temp\AdventureWorks2019_copy.mdf',
MOVE 'AdventureWorks2019_log' TO 'c:\temp\AdventureWorks2019_copy_log.ldf',
RECOVERY, REPLACE, STATS = 10
;
GO
18. Notice the error indicates something about not finding a server certificate. We cannot restore
this database without the certificate that was used to back it up.
19. You could even try creating a new database master key and a new certificate with the same
name and even the same subject that was used to backup the original database. Because this
new certificate is not the certificate that was used to backup the database this scenario would
still not allow you to restore the backup.
20. Now let’s try to recover the certificate but not the private key that we backed up.
We still get an error because although we have the correct certificate, we still do not have the
private key. Without the private key SQL Server cannot decrypt the database.
The error says the key appears to be corrupt. We know the key is not corrupt but that we have
not restored (created) it yet.
22. In order to perform a successful restore, we'll need the database master key in the master
database in place and we'll need to restore the certificate used to encrypt the database, but we'll
need to make sure we restore it with the private key. In checklist form:
23. Before we begin we will need to drop the certificate we created previously.
USE MASTER
GO
DROP CERTIFICATE TDECert
GO
USE MASTER
GO
RESTORE DATABASE AdventureWorks2019_copy FROM DISK =
'c:\temp\AdventureWorks2019_new.bak'
WITH
MOVE 'AdventureWorks2019' TO 'c:\temp\AdventureWorks2019_copy.mdf',
MOVE 'AdventureWorks2019_log' TO 'c:\temp\AdventureWorks2019_copy_log.ldf',
RECOVERY, REPLACE, STATS = 10
;
GO