SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Restoring database to another server cannot open SYMMETRIC key


Restoring database to another server cannot open SYMMETRIC key

Author
Message
diogenes1331
diogenes1331
SSC-Enthusiastic
SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)

Group: General Forum Members
Points: 190 Visits: 54
After restoring the database into a another server, I run the following T-SQL command:

OPEN MASTER KEY DECRYPTION BY PASSWORD = 'password'
ALTER MASTER KEY ADD ENCRYPTION BY SERVICE MASTER KEY

and when I try to open the SYMMETRIC key to retrieve the data on the encrypted field with a SELECT statement I get the following message (the same SELECT statement on the original server works fine as expected).

Msg 15466, Level 16, State 1, Line 1
An error occurred during decryption.

(402 row(s) affected)
Msg 15315, Level 16, State 1, Line 7
The key 'SymKey' is not open. Please open the key before using it.

Thanks.



K. Brian Kelley
K. Brian Kelley
Keeper of the Duck
Keeper of the Duck (10K reputation)

Group: Moderators
Points: 10242 Visits: 1917
What is the symmetric key encrypted with? If it's a certificate or asymmetric key, are you able to open that key explicitly?

K. Brian Kelley
@‌kbriankelley
diogenes1331
diogenes1331
SSC-Enthusiastic
SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)

Group: General Forum Members
Points: 190 Visits: 54
Thanks for your help.

>>>What is the symmetric key encrypted with?
>>>If it's a certificate or asymmetric key, are you able to open that key explicitly?

The symmetric key is encrypted with a Certificate.

No, I am not able to open the key. When I try to open the key with the statement:

OPEN SYMMETRIC KEY SymKey
DECRYPTION BY CERTIFICATE Cert

I get the error

Msg 15466, Level 16, State 1, Line 1
An error occurred during decryption.

I have also imported the Service Master Key from the original server and I still have the same problems.



K. Brian Kelley
K. Brian Kelley
Keeper of the Duck
Keeper of the Duck (10K reputation)

Group: Moderators
Points: 10242 Visits: 1917
And the certificate was encrypted by the database master key and not a password? If you export the database master key from the original server and restore it on the new, can you open the database master key manually and then everything works?

K. Brian Kelley
@‌kbriankelley
diogenes1331
diogenes1331
SSC-Enthusiastic
SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)

Group: General Forum Members
Points: 190 Visits: 54
And the certificate was encrypted by the database master key and not a password?

Yes it was encrypted by the DBMK because it was not encrypted with a password.

If you export the database master key from the original server and restore it on the new, can you open the database master key manually and then everything works?

No, I open the key by using the following command:


OPEN MASTER KEY DECRYPTION BY PASSWORD = 'pwd'

and then I try to open the symmetric key and it gives me the error.



K. Brian Kelley
K. Brian Kelley
Keeper of the Duck
Keeper of the Duck (10K reputation)

Group: Moderators
Points: 10242 Visits: 1917
Can you open the certificate? And what function are you using to open the Symmetric Key and return the data?

K. Brian Kelley
@‌kbriankelley
diogenes1331
diogenes1331
SSC-Enthusiastic
SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)

Group: General Forum Members
Points: 190 Visits: 54
Before continuing, please let me thank you for your help so far.

After your previous posting I went ahead and tried the following in the new server:

DECLARE @plaintext nvarchar(2000), @cyphertext nvarchar(2000)
SET @plaintext = N'AAA'
SET @cyphertext = EncryptByCert(Cert_ID('MyCert'), @plaintext)
PRINT @cyphertext
SELECT CAST(DecryptByCert(Cert_ID('MyCert'), @cyphertext) AS NVARCHAR)

This works in the original server but in the one where I restored the database instead of getting AAA at the end of the above code I get NULL. So it appears that the certificate can be used to encrypt but not to decrypt. However, when I run the same code in the original server, I get AAA at the end.

I hope this answers your question.



abraham leonido-427616
abraham leonido-427616
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 10
Just wondering if you were able to fix the issue.


We have the same issue.

We have 3 DB servers - dev , test and Prod. We have no decryption problems when we restore the database from Test to Dev.

We have problems when we restore from Prod to Test.

Only difference that I see is that our Prod Server is a Windows 2003 Enterprise Edition. Our Dev and Test servers are Windows 2003 Standard Edition.

Any feedback is greatly appreciated.

Thanks.
K. Brian Kelley
K. Brian Kelley
Keeper of the Duck
Keeper of the Duck (10K reputation)

Group: Moderators
Points: 10242 Visits: 1917
Are you able to open the master key on the database?

K. Brian Kelley
@‌kbriankelley
abraham leonido-427616
abraham leonido-427616
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 10
Yes.

OPEN MASTER KEY DECRYPTION BY PASSWORD = 'xxxxxx' was successful.

OPEN SYMMETRIC KEY XXX_KEY DECRYPTION BY CERTIFICATE XXX_CERTIFICATE is where we got the following error

'an error occurred during decryption'


Also got the same results for the test script posted by diogenes1331.
When we ran it in our Prod server,we got 'AAA'. When we ran it in the Test server, we got NULL
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search