Restoring Databases with Encrypted Data to Another Server



I use the following code to create encrypted data in a table called TEST in a
SQL Server 2005 database.

-- Use the AdventureWorks database
USE AdventureWorks;

-- Create a Database Master Key
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'p@ssw0rd';

-- Create a Temp Table
CREATE TABLE Person.#Temp
(ContactID INT PRIMARY KEY,
FirstName NVARCHAR(200),
MiddleName NVARCHAR(200),
LastName NVARCHAR(200),
eFirstName VARBINARY(200),
eMiddleName VARBINARY(200),
eLastName VARBINARY(200));

-- Create a Test Certificate
CREATE CERTIFICATE TestCertificate
WITH SUBJECT = 'Adventureworks Test Certificate',
EXPIRY_DATE = '10/31/2009';

-- Create a Symmetric Key
CREATE SYMMETRIC KEY TestSymmetricKey
WITH ALGORITHM = TRIPLE_DES
ENCRYPTION BY CERTIFICATE TestCertificate;
OPEN SYMMETRIC KEY TestSymmetricKey
DECRYPTION BY CERTIFICATE TestCertificate;

-- EncryptByKey demonstration encrypts 100 names from the Person.Contact table
INSERT
INTO Person.#Temp (ContactID, eFirstName, eMiddleName, eLastName)
SELECT ContactID,
EncryptByKey(Key_GUID('TestSymmetricKey'), FirstName),
EncryptByKey(Key_GUID('TestSymmetricKey'), MiddleName),
EncryptByKey(Key_GUID('TestSymmetricKey'), LastName)
FROM Person.Contact
WHERE ContactID <= 100;

-- DecryptByKey demonstration decrypts the previously encrypted data
UPDATE Person.#Temp
SET FirstName = DecryptByKey(eFirstName),
MiddleName = DecryptByKey(eMiddleName),
LastName = DecryptByKey(eLastName);

-- View the results
SELECT convert(nvarchar(1000),DecryptByKey(eFirstName)) as FName,
convert(nvarchar(1000),DecryptByKey(eMiddleName)) AS MName,
convert(nvarchar(1000),DecryptByKey(eLastName)) AS LName
FROM Person.#Temp

--create physical table
select * into TEST from person.#temp

--view results from physical table
SELECT convert(nvarchar(1000),DecryptByKey(eFirstName)) as FName,
convert(nvarchar(1000),DecryptByKey(eMiddleName)) AS MName,
convert(nvarchar(1000),DecryptByKey(eLastName)) AS LName
FROM TEST

----------------------------------------------------

I backup the database and restore to another server. After the restore
finishes, I run the following commands in the restored database:

OPEN MASTER KEY DECRYPTION BY PASSWORD = 'p@ssw0rd';
ALTER MASTER KEY ADD ENCRYPTION BY SERVICE MASTER KEY;

When I run the following SELECT statement to view my data in the restored
database, I only get NULLs.

SELECT convert(nvarchar(1000),DecryptByKey(eFirstName)) as FName,
convert(nvarchar(1000),DecryptByKey(eMiddleName)) AS MName,
convert(nvarchar(1000),DecryptByKey(eLastName)) AS LName
FROM TEST

From what I have researched, this is all you have to do. I must be missing
a step. Can anyone tell me what I have forgotten or provide me a list of
steps to execute when I am restoring a database with encrypted data to
another server????

Thanks,

--
DB
.



Relevant Pages

  • 2005-Problem restoring database with encrypted columns to diff ser
    ... I need to start encrypting several fields in a database and have been doing ... OPEN MASTER KEY DECRYPTION BY PASSWORD = 'testAppleA3'; ... ALTER MASTER KEY ADD ENCRYPTION BY SERVICE MASTER KEY; ... encryption by certificate test; ...
    (microsoft.public.sqlserver.security)
  • Re: Restoring Databases with Encrypted Data to Another Server
    ... SQL Server 2005 database. ... -- Create a Database Master Key ... ENCRYPTION BY CERTIFICATE TestCertificate; ... I backup the database and restore to another server. ...
    (microsoft.public.sqlserver.security)
  • Re: SQL Server 2000 / 2005 Encryption
    ... Once it is on the Tablet, does Excel just know to use this certificate because the information in the data dump / pivot table needs to be decrypted? ... This appears to be a "connection only" type of encryption. ... Is this encryption enabled server wide, or for performance reasons, can you enable it only on a single database? ... You can find specifics in BOL under "CREATE MASTER KEY", "CREATE CERTIFICATE" and "CREATE SYMMETRIC KEY". ...
    (microsoft.public.sqlserver.security)
  • Re: Encrypted database deployment
    ... this new environment, even when I restore a backup... ... it isn't the database master key so you now have two different master ... ALTER MASTER KEY ADD ENCRYPTION BY SERVICE MASTER KEY ...
    (microsoft.public.sqlserver.security)
  • Re: Encrypted database deployment
    ... I restored the database, ran the open and alter commands as below, but still ... An error occurred during decryption. ... it isn't the database master key so you now have two different master ... ALTER MASTER KEY ADD ENCRYPTION BY SERVICE MASTER KEY ...
    (microsoft.public.sqlserver.security)