我想通過始終加密來加密現有數據庫列。我的項目是一個ASP.NET項目,首先使用代碼,數據庫是SQL Server。數據庫已經有數據。我創建了一個遷移來實現我的目標。從解密列更新總是加密列
首先,我嘗試改變列的類型,使用以下內容。
ALTER TABLE [dbo].[TestDecrypted] ALTER COLUMN [FloatCol] [float] ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = [CEK_Auto1], ENCRYPTION_TYPE = Randomized, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256') NULL
我得到以下錯誤。
Operand type clash: float is incompatible with float encrypted with (encryption_type = 'RANDOMIZED', encryption_algorithm_name = 'AEAD_AES_256_CBC_HMAC_SHA_256', column_encryption_key_name = 'CEK_Auto1', column_encryption_key_database_name = 'TestEncrypt')
然後我決定創建另一列並遷移數據。
ALTER TABLE [dbo].[TestDecrypted] ADD [FloatCol2] [float] ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = [CEK_Auto1], ENCRYPTION_TYPE = Randomized, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256') NULL
UPDATE [dbo].[TestDecrypted] SET [FloatCol2] = [FloatCol]
而且我得到了同樣的錯誤。
我看着this後,我發現它可以插入數據,如以下
DECLARE @floatCol FLOAT = 1.1
UPDATE [dbo].[TestDecrypted] SET [FloatCol2] = @floatCol
但是,如果我嘗試獲得從現有列中的值,它失敗。
DECLARE @floatCol FLOAT = (SELECT TOP 1 FloatCol FROM TestDecrypted)
UPDATE [dbo].[TestDecrypted] SET FloatCol2 = @floatCol
錯誤如下。
Encryption scheme mismatch for columns/variables '@floatCol'. The encryption scheme for the columns/variables is (encryption_type = 'PLAINTEXT') and the expression near line '4' expects it to be (encryption_type = 'RANDOMIZED', encryption_algorithm_name = 'AEAD_AES_256_CBC_HMAC_SHA_256', column_encryption_key_name = 'CEK_Auto1', column_encryption_key_database_name = 'TestEncrypt').
有誰知道我該如何實現我的目標?
更新1
@尼基爾 - Vithlani微軟做了一些有趣的建議。
- 始終加密嚮導在SSMS - 我想實現我的代碼首先遷移目標,所以這個想法不適合。
- SqlBulkCopy - 它不適用於遷移,因爲新列將只在所有'Up'方法運行後才存在。因此我們不能在這個方法內以這種方式將數據插入到這個列中。
無論如何,他的建議驅使我到另一個嘗試:獲得解密的值,並與他們更新加密列。
var values = new Dictionary<Guid, double>();
var connectionString = ConfigurationManager.ConnectionStrings["MainDb"].ConnectionString;
using (var sourceConnection = new SqlConnection(connectionString))
{
var myCommand = new SqlCommand("SELECT * FROM dbo.TestDecrypted", sourceConnection);
sourceConnection.Open();
using (var reader = myCommand.ExecuteReader())
{
while (reader.Read())
{
values.Add((Guid)reader["Id"], (double)reader["FloatCol"]);
}
}
}
Sql("ALTER TABLE [dbo].[TestDecrypted] ADD [FloatCol2] [float] ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = [CEK_Auto1], ENCRYPTION_TYPE = Randomized, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256') NULL");
foreach (var valuePair in values)
{
// The error occurs here
Sql([email protected]"DECLARE @value FLOAT = {valuePair.Value}
UPDATE [dbo].[TestDecrypted] SET [FloatCol2] = @value WHERE Id = '{valuePair.Key}'");
}
實際上,我沒有嘗試創建另一列並遷移數據,如上面的示例中所述。我只在SSMS上試過。 現在我得到了一個不同的錯誤。
Transaction (Process ID 57) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
我試圖做到這一點,而沒有加密新列,它工作正常。
任何想法爲什麼會發生此錯誤?
您在更新1中編寫的代碼不起作用。請參閱我的答案更新,我已經解釋了原因,並提供了替代 –