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 2005:
Database Master Keys
Symmetric and Asymmetric Keys
Putting It All Together-Using Data Encryption in the Production Environment
SQL Server 2005 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:
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 CREATE MASTER KEY ENCRYPTION BY PASSWORD = ''
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 BACKUP MASTER KEY TO FILE = '' ENCRYPTION BY PASSWORD = ''
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 RESTORE MASTER KEY FROM FILE = '' DECRYPTION BY PASSWORD = '' ENCRYPTION BY PASSWORD = ''
Consider using the encryption hierarchy built into SQL Server 2005 to either directly encrypt data or use in tandem with application-based encryption
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.