Managing SQL Server Master Keys for Encryption

By:   |   Comments (9)   |   Related: > Encryption


I recently accepted the challenge of designing a database for an employee application, and the first thought to cross my mind was," I will need to encrypt at least one piece of data". Along with SQL Server 2005 came the native ability to encrypt data in the database. Prior versions depended on third-party tools or encryption via the front end application. In this four-tip series we will cover the objects needed and steps required to encrypt data using SQL Server:

  • Database Master Keys
  • Certificates
  • Symmetric and Asymmetric Keys
  • Putting It All Together-Using Data Encryption in the Production Environment

SQL Server has an encryption hierarchy, as shown below, that needs to be followed in order to properly support the native encryption capabilities. The top-level resource in the SQL Server encryption hierarchy is the Service Master Key, which is encrypted by the Windows Data Protection API and created the first time a newly-created key needs to be encrypted. Second in line is the Database Master Key. This key can be used to create certificates and asymmetric keys. Next come certificates and asymmetric keys. Both can be used to create symmetric keys or encrypt data directly. Last in line are symmetric keys, which can also be used to encrypt data.

Behind the scenes, an encrypted copy of the Database Master Key is stored in the Master database, encrypted by the Service Master Key (this behavior can be changed using the ALTER MASTER KEY statement). Information on keys can be found in the sys.symmetric_keys dynamic management view:

query result

How Do I Create A Database Master Key?

Before creating certificates (assuming the Database Master Key is used to create the certificate) or other keys can be generated, a Database Master Key must be created. Since there is no GUI method for creating one, the syntax for creating one is:

USE <DatabaseName> 

How Do I Protect Database Master Keys?

Once a Database Master Key is created, you should back it up to a secure location, preferably offsite. To back up a Database Master Key, use the following syntax:

USE <DatabaseName> 
BACKUP MASTER KEY TO FILE = '<FolderLocation>'

Can I Restore Database Master Keys?

A Database Master Key can be restored if the need arises. When the key is restored, all keys encrypted by the active Master Key are decrypted and re-encrypted using the new Master Key. If your infrastructure utilizes many keys this process can be resource-intensive, so restorations should take place during periods of low server activity when possible. To restore a Database Master Key, use the following syntax:

USE <DatabaseName> 
Next Steps

sql server categories

sql server webinars

subscribe to mssqltips

sql server tutorials

sql server white papers

next tip

About the author
MSSQLTips author Tim Cullen Tim Cullen has been working in the IT industry since 2003 and currently works as a SQL Server Reports Developer.

This author pledges the content of this article is based on professional experience and not AI generated.

View all my tips

Comments For This Article

Monday, December 1, 2014 - 10:02:07 AM - Dinesh DBA Back To Top (35443)


Where is sql certificate by default stored ?




Monday, May 27, 2013 - 9:36:02 AM - arsha Back To Top (25150)

pl tell me . how to stop modify my databases.


Monday, February 25, 2013 - 11:53:17 AM - CPR Back To Top (22406)

Hi Tim,

I have question regarding encryption key that i have already in my production server..

Right now if I want to reset password for user with  sql authentication and I’m using 5 character encrypted password and it's giving me error for windows policy..

my question is that can i make new encrypted password with 8 character? I have right now Symmetric keys on my master DB.

Name: ##MS_ServiceMasterKey##, Principal _id:1, Symmetric_key_id:102,Key_lenght:128,Key_algorithm:D3,algorithm_desc:TRPLE_DEC then it's create date and modify date..

Thursday, April 26, 2012 - 4:29:01 PM - Tim Cullen Back To Top (17140)

You can query the sys.symmetric_keys:



[name] AS KeyName

, symmetric_key_id AS KeyID

, key_algorithm AS KeyAlgorithm

, create_date AS DateCreated

, modify_date AS LastModified

FROM sys.symmetric_keys WITH (NOLOCK)

Thursday, April 26, 2012 - 3:30:29 PM - Charles Back To Top (17138)

is it possible via the database to retrieve the creation date of the service master key and the database master key?

thank you in advance

Wednesday, April 18, 2012 - 11:38:58 AM - EC Back To Top (16991)

The database is locally on my labtop. and i login with sa account.


Wednesday, April 18, 2012 - 8:52:09 AM - Tim Cullen Back To Top (16987)

Good morning, EC:


A couple of questions:


1) Is the connection pointing to the correct database (I do that all the time)?

2) Are you a member of the sysadmin group in the SQL instance?

3) Are you a member of the db_owner group in the database?

Wednesday, April 18, 2012 - 6:59:34 AM - EC Back To Top (16985)

My company uses moble team to enroll  client in the remote villages. i want to encrypt the database on the labtop.(TDE)

i follow the neccessay steps but when i try to backup the master key. i received an error: You do not have permission or master key don't exit.

After create the maskey and the ceritificates i check the encryption state it was:3




Tuesday, October 28, 2008 - 8:59:58 AM - lacorrigan Back To Top (2100)

I pretty much follow these concepts.  I am having an additional problem in that we have 4 environments, Dev, QA, UAT and Production.

I am not real clear as to what happens or what should happen to the keys and encryption when an encrypted database is moved from one environment to another.  I have initial tried this and once moved to a different server I can't properly decrypted the data.  I don't really know whether I should use the same keys in all environments or different keys and if so how to make that work moving these databases around.  I have learned it is very easy to loose your encrypted data.

 Any help would be greatly appreciated.


get free sql tips
agree to terms