One of the many new features introduced in SQL Server 2016 is Always Encrypted. In the words of Microsoft: “Always Encrypted provides a separation between those who own the data (and can view it) and those who manage the data (but should have no access).”
If there is PII in a table or database, all Database Administrators (or anyone tasked with administering that database that have the enough permissions) can view that data. Generally, those who are hired to manage that database should be trusted to not go digging through personal information; however, with Always Encrypted, you can completely eliminate having to even think about who may or may not have access to the sensitive data. Because having access to an encryption certificate is needed to view the data, it becomes very easy to control who has access.
As a DBA, I can say that not even having to worry about what data I may or may not see is a huge relief.
In this post, I will cover:
1. How to encrypt a column with sensitive data using the GUI in SSMS.
2. How to view Always Encrypted Data from SSMS on the machine where the Always Encrypted certificate was generated.
3. How to export/import the Always Encrypted certificate so that Always Encrypted data can be viewed from multiple machines.
Encrypting a Column
I will be using the trusty AdventureWorks database for this demonstration (I know, I know. WorldWideImporters is sooo much better), and I will be looking at the Sales.CreditCard table.
Here is what the data in that CreditCard table looks like. Our goal will be to encrypt the CardNumber column.
Use AdventureWorks GO SELECT TOP 5 * FROM [Sales].[CreditCard];
First, let’s create a quick copy the CreditCard table. We’ll copy it over to a new table: CreditCard_Encrypted.
--create copy of the CreditCard table DROP TABLE IF EXISTS Sales.CreditCard_Encrypted Select * INTO Sales.CreditCard_Encrypted from Sales.CreditCard GO
Since I am a lazy millennial, I’m just going to use the GUI to encrypt the CardNumber column.
First, right-click on the table and choose Encrypt Columns…
Next, select the column(s) you want to encrypt and choose the Encryption Type (Deterministic or Randomized) and the Encryption Key you want to use. I’m just going to be generating a new key for this demonstration.
Choose the Column Master Key. Once again, I’m generating a new master key and storing it in the Windows Certificate store.
Finish navigating through the next few screens and finish the Encryption tasks:
Once that is complete, we can try querying the CreditCard_Encrypted table:
Use AdventureWorks GO SELECT TOP 5 * FROM [Sales].[CreditCard_Encrypted];
Our CardNumber column is now encrypted! That wasn’t too difficult.
We can also see the Master Key and Encryption Key that were created:
We can also script out the DDL for those keys…
…and we can script out the CREATE statement for the table to see what the Always Encrypted syntax looks like:
CREATE TABLE [Sales].[CreditCard_Encrypted]( [CreditCardID] [int] IDENTITY(1,1) NOT NULL, [CardType] [nvarchar](50) NOT NULL, [CardNumber] [nvarchar](25) COLLATE Latin1_General_BIN2 ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = [CEK_Auto1], ENCRYPTION_TYPE = Randomized, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256') NOT NULL, [ExpMonth] [tinyint] NOT NULL, [ExpYear] [smallint] NOT NULL, [ModifiedDate] [datetime] NOT NULL ) ON [PRIMARY] GO
So what do we do if we actually want to view the encrypted data in SSMS?
We’ll need to pass in this connection parameter in the SSMS connection options: Column Encryption Setting = Enabled
Now I’ll reconnect and try to select from the Encrypted table once again:
Use AdventureWorks GO SELECT TOP 5 * FROM [Sales].[CreditCard];
I am now able to view the encrypted data. Why is this?
Because I am connecting to the database from the database server, I have access to the Encryption Certificate that was generated. I can verify this by opening certmgr.msc and browsing to Personal -> Certificates:
Without access to that certificate, I would not be able to see the encrypted data.
We can test this by attempting to connect to the database with the encrypted table from another machine and passing in the “Column Encryption Setting = Enabled” connection option in SSMS.
When trying to do a select on CreditCard_Encrypted, we receive this error message:
Msg 0, Level 11, State 0, Line 0 Failed to decrypt column 'CardNumber'. Msg 0, Level 11, State 0, Line 0 Failed to decrypt a column encryption key using key store provider: 'MSSQL_CERTIFICATE_STORE'. The last 10 bytes of the encrypted column encryption key are: '36-B6-E0-8F-9F-54-AC-CF-9B-CB'. Msg 0, Level 11, State 0, Line 0 Certificate with thumbprint 'BA85A469AE80E4001B1FD9E71D89165593DE9009' not found in certificate store 'My' in certificate location 'CurrentUser'. Verify the certificate path in the column master key definition in the database is correct, and the certificate has been imported correctly into the certificate location/store. Parameter name: masterKeyPath
What about viewing the encrypted data from SSMS on another machine?
So how can we get a copy of the Always Encrypted certificate over to the other machine?
We must go back to the original machine or server where the certificate was generated. Re-open certmgr.msc, browse to the Always Encrypted Certificate, right-click -> All Tasks -> Export…
I’ve had success selecting these options:
Give the certificate a password if you wish:
Choose a file name for the exported certificate:
Finish the export wizard and browse to the destination folder. We now have a copy of the Always Encrypted certificate.
The certificate can now be copied to the secondary machine that needs access to the encrypted data. Open ‘Manage User Certificates’ (AKA certmgr.msc). Browse to Personal -> Certificates. Right click on the Certificates folder -> All Tasks -> Import…
Browse to and select the Always Encrypted certificate that was copied from the database server:
Enter the password if one was used. Also, check Include all extended properties.
Place the certificate in the Personal store.
Step through the final screens and finish the import process.
After the import is finished, open certmgr.msc and ensure the Always Encrypted certificate shows up under Personal -> Certificates.
Now that the certificate has been imported, we can try to query the Encrypted table again.
BOOM! We can now query the encrypted data on a different machine with SSMS.
In this blog post, I’ve shown how to easily encrypt a column using Always Encrypted. I’ve also shown how to enable users using SSMS to view that encrypted data.
I’m pretty excited about Always Encrypted and think that it could potentially be a great solution for encrypting sensitive data.