

- CREATE MASTER KEY SYSTEM HOW TO
- CREATE MASTER KEY SYSTEM UPDATE
- CREATE MASTER KEY SYSTEM PASSWORD
- CREATE MASTER KEY SYSTEM WINDOWS
To help understand, we stood up a test environment to show how to set up basic encryption. I explained how the symmetric key, certificate, database master key, and service key all relate to each other. In this article, we took a look at a common issue relating to database master keys and how we can get errors when moving at database to another server. I can now open the symmetric key to decrypt the information in the table: Resolving the IssueĪll I need to do here is explicitly open the database master key and encrypt it using the new service key for this particular server\instance: This is due to the service key being different on this instance\server than the original so it cannot automatically decrypt the database master key to open the TestSymKey01. This is the same error I was getting before. Once complete, I connect to the new instance, and run the following query: I need to take a backup of my newly created TestDB database and restore it onto another server or instance.

To reproduce the error that I was originally getting:

Now of course, we want to make sure we can decrypt it by using the DecryptByKey function:īehind the scenes, the database master key is automatically decrypted by the service key. Note that we need to open the symmetric key first: Next, we need to use the ENCRYPTBYKEY function to encrypt the data. First, create the table and insert some records: This is the fun part using this stuff! We will first create a table, insert a few test records, encrypt the data, and decrypt it. Now, I just need to create the symmetric key: Notice that the certificate is encrypted with the database master key I just created: This will be used to encrypt the symmetric key I create after. I can also verify that the master key was created and encrypted by the service key:
CREATE MASTER KEY SYSTEM PASSWORD
By default, there are no database master keys in the database so the 1 st thing I need to do is create one:ĬREATE MASTER KEY ENCRYPTION BY PASSWORD = 'Password123!'

On a test server, I created a database called TestDB (I know, very original…lol!). Let’s take a look at a mock up: Setting up a Test Environment To fix the issue, you need to manually open the database master key and add the new service master key. Due to the copy of the database from one server to another, that database master key cannot be automatically decrypted because the service key is now invalid. The issue is caused because when I go to open the symmetric key it is decrypted by the certificate which is encrypted by the database master key.
CREATE MASTER KEY SYSTEM UPDATE
Basically, the master key is encrypted by the server instance so when you move that database master key (located in the database) to another server, you need to update it with the new service key of the new server.
CREATE MASTER KEY SYSTEM WINDOWS
In summary, the Database Master Key is encrypted by the service key and the service key is created during SQL Server setup and it is encrypted with the Windows Data Protection API. The service key: Used to encrypt the database master key.The database master key: Used to encrypt the certificate in this environment.The certificate: Used to encrypt the symmetric key in this environment.The symmetric key: Used to perform the encryption and decryption of the passwords.The moving pieces that need to be understood are: Please note: there are a few ways that you can set this up, but for this article, assume the following: To fully understand the above error, I needed to fully understand all the moving pieces with that functionality as well as how the master key mentioned in the error relates. Understanding the IssueĪt my current employer, we use symmetric encryption keys to encrypt and decrypt passwords in the database. I do not claim to be an expert in security nor is this a reference for all things encryption, but if we start at a smaller scale for this issue, I hope it provides a platform to understanding when this error can be raised and why along with some basic knowledge in the application of encryption. Please create a master key in the database or open the master key in the session before performing this operation. After restoring a copy of the production database onto a test server, I would run into some strange issues relating to the database master key that look like this:
