简体   繁体   English

使用目标 SMK 不同的加密列还原数据库

[英]Restoring database with encrypted columns where destination SMK is different

A similar question has been asked before , but I believe the circumstances are slightly different, and I'd also like to understand any alternative solutions.之前有人问过类似的问题,但我相信情况略有不同,我也想了解任何替代解决方案。 I'm at the stage of information overload right now:\我现在正处于信息过载阶段:\

Certain columns on a database on SERVER A have been encrypted using this approach: SERVER A 上数据库的某些列已使用此方法加密:

-- Key creation
USE [master];
GO
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'ComplexPasswordHere';
CREATE CERTIFICATE MyDbCertificate01 WITH SUBJECT = 'MyDatabase Certificate 01';
CREATE SYMMETRIC KEY SSN_Key_01 WITH ALGORITHM = TRIPLE_DES ENCRYPTION BY CERTIFICATE MyDbCertificate01;

-- Decryption example
USE [MyDB];
GO
OPEN SYMMETRIC KEY SSN_Key_01 DECRYPTION BY CERTIFICATE MyDbCertificate01;
SELECT
  CONVERT(nvarchar(50), DECRYPTBYKEY(PasswordEnc)) AS [Password]
FROM
  [tbl_Users]
CLOSE SYMMETRIC KEY SSN_Key_01;

This database needs to be restored onto SERVER B, which already has a Service Master Key that governs encryption on other databases on that server.该数据库需要恢复到服务器 B 上,该服务器已经有一个服务主密钥,用于管理该服务器上其他数据库的加密。

From research, other authors state we can backup/restore the SMK using FORCE , but I think that would obliterate existing encryption on the destination server:根据研究,其他作者表示我们可以使用FORCE备份/恢复 SMK,但我认为这会破坏目标服务器上的现有加密:

And here lies the problem: The current machine DMK cannot be used on data encrypted with another SMK.问题就在这里:当前机器的 DMK 不能用于使用另一个 SMK 加密的数据。 It will fail to decrypt, because the SMK has changed.它将无法解密,因为 SMK 已更改。 Source 资源

Assuming the above is still accurate, can the database be backed up, along with perhaps the certificate, to enable the destination server to decrypt the data successfully?假设以上仍然准确,是否可以备份数据库以及可能的证书,以使目标服务器能够成功解密数据?

Is there any other way to achieving this without damaging the destination server's existing data?有没有其他方法可以在不损坏目标服务器现有数据的情况下实现这一目标?

So with the pressure mounting, I decided to get my wallet out and set up a new Azure VM testing server, guaranteeing a completely fresh setup.因此,随着压力的增加,我决定拿出我的钱包并设置一个新的 Azure VM 测试服务器,以保证全新的设置。

The solution turned out as follows (links provided where possible).解决方案如下(尽可能提供链接)。

1. Restore/Clone Database on Different Server 1. 在不同的服务器上恢复/克隆数据库

Use the instructions here (SSMS Restore dialog, overwriting the necessary fields) to restore the database.使用此处的说明(SSMS 还原对话框,覆盖必要的字段)还原数据库。 At this point, the encrypted database fields cannot yet be decrypted此时,加密的数据库字段还无法解密

2. Restore Database Master Key 2.恢复数据库主密钥

Once restored, it turns out that this answer on dba.stackexchange is all that's required (modified to fit my question).一旦恢复,事实证明dba.stackexchange 上的这个答案就是所需要的(修改以适合我的问题)。

USE [MyRestoredDatabase]
GO
OPEN MASTER KEY DECRYPTION BY PASSWORD = 'ComplexPasswordHere'; -- This is the password used for --> CREATE MASTER KEY ENCRYPTION BY PASSWORD '....';
ALTER MASTER KEY ADD ENCRYPTION BY SERVICE MASTER KEY;
GO

From that point onwards, the DECRYPTBYKEY() and other encryption worked fine.从那时起, DECRYPTBYKEY()和其他加密工作正常。

If my assumptions below are incorrect, please post an answer that clarifies where I'm wrong, and I will accept your answer instead...如果我下面的假设不正确,请发表一个答案来澄清我错在哪里,我会接受你的答案......

From my research on the new VM, it appears that when the database is backed up, the certificate and symmetric key are backed up automatically as part of the database (which makes perfect sense).根据我对新 VM 的研究,似乎在备份数据库时,证书和对称密钥会作为数据库的一部分自动备份(这非常有意义)。 Therefore, the final step is to configure the existing database master key that gets restored, to be under the control of the service master key on the destination machine.因此,最后一步是配置已恢复的现有数据库主密钥,使其处于目标计算机上服务主密钥的控制之下。 SSMS does the re-wiring automatically. SSMS 会自动重新布线。

Forgot the details of how to do this again and had to go looking so just a bit of extra information for anyone who might need it.忘记了如何再次执行此操作的细节,不得不去寻找一些额外的信息,以供可能需要它的任何人使用。

There are at least two ways to get this done and the second is useful if you can't lay hands on the password for the database key but you are the SQL Server admin.至少有两种方法可以完成此操作,如果您不能掌握数据库密钥的密码但您是 SQL Server 管理员,则第二种方法很有用。

In that case you can just export the Server master key and overwrite the one in the destination server before you restore the backed up database.在这种情况下,您可以在恢复备份的数据库之前导出服务器主密钥并覆盖目标服务器中的主密钥。 Of course this only works on a new server, or one that doesn't already have certificates in use or, in extremis, if you know the key passwords for all the certificates in the destination and can reset them as the EvilDr describes.当然,这只适用于新服务器,或者尚未使用证书的服务器,或者在极端情况下,如果您知道目标中所有证书的密钥密码,并且可以按照 EvilDr 的描述重置它们。

USE MASTER
GO
--On original SQL Server set password
BACKUP SERVICE MASTER KEY TO FILE = 'C:\temp\smk' ENCRYPTION BY PASSWORD = 'password';
GO
--On new SQL Server - BEFORE restoring backed up database (or else conflict)
RESTORE SERVICE MASTER KEY FROM FILE = 'C:\temp\smk' DECRYPTION BY PASSWORD = 'password';
GO
--Then restore database from backup with certificate and key included

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM