Database Administrators
Q&A for database professionals who wish to improve their database skills
Latest Questions
0
votes
2
answers
1625
views
How to rotate the CMK of Always Encrypted using SSMS?
I have implemented the encryption in SQL Server 2016 using Always Encrypted. I want to know how to rotate the column master key after the initial one year validity using SQL Server Management Studio 17.0. I'm looking for the steps that can executed **using only SSMS** and not PowerShell or any other...
I have implemented the encryption in SQL Server 2016 using Always Encrypted.
I want to know how to rotate the column master key after the initial one year validity using SQL Server Management Studio 17.0.
I'm looking for the steps that can executed **using only SSMS** and not PowerShell or any other program.
Is it technically possible to rotate the key only using SSMS?
Experts advice please.
Sri
(403 rep)
Oct 23, 2017, 09:07 PM
• Last activity: Mar 14, 2025, 09:05 PM
0
votes
0
answers
497
views
Always Encrypted returning Timeout error
I am trying to encrypt a number of tables (1 column in multiple tables) in two different databases. On Database A I manage to encrypt these, while on database B it fails with an error: '**Unable to reconnect to database: Execution Timeout Expired. The timeout period elapsed prior to completion of th...
I am trying to encrypt a number of tables (1 column in multiple tables) in two different databases. On Database A I manage to encrypt these, while on database B it fails with an error: '**Unable to reconnect to database: Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.**'. Have tried this on another staging server and got the same result so issue seems to be database related and not server related. Tried updating statistics and restating the entire machines however problem persists. Also no activity is going on the staging server so should not be related to locks. Any suggestions?
Thanks in advance.
Ron
(21 rep)
Feb 18, 2019, 02:56 PM
• Last activity: Sep 7, 2024, 09:09 AM
0
votes
0
answers
98
views
Can we store always encrypted column into temp table in Azure SQL Database?
I have encrypted a column in Azure SQL Database and now when a proc which has used temp table and being used to UPDATE/INSERT data into always encrypted table. The exception detail is Msg 33293 Level 16 Cannot find the column encryption key because it does not exists or you do not have permission. I...
I have encrypted a column in Azure SQL Database and now when a proc which has used temp table and being used to UPDATE/INSERT data into always encrypted table. The exception detail is
Msg 33293 Level 16 Cannot find the column encryption key because it does not exists or you do not have permission.
I am not able to find the solution of it.
Mr. K
(141 rep)
Jul 8, 2024, 11:37 AM
3
votes
2
answers
1600
views
Is self signed cert the standard practice for SQL Server Always Encrypted?
We're implementing SQL Server Always Encrypted in our 2019 environment. We've done several successful POC's over the last few months, but in moving the solution to Prod, I was expecting to use a Public Trusted CA for the certificates. But now, combing back over approximately 10 AE tutorials on the w...
We're implementing SQL Server Always Encrypted in our 2019 environment. We've done several successful POC's over the last few months, but in moving the solution to Prod, I was expecting to use a Public Trusted CA for the certificates. But now, combing back over approximately 10 AE tutorials on the web, including the official Microsoft instructions, I've noticed that using a Public Trusted CA is never even mentioned...ever...at all. There's even an article on here asking if using a Trusted CA cert even adds any benefit, and it's suggested that it does not.
I'm trying to satisfy all of our security initiatives that are being rolled out company wide, so I guess I'm trying to find some sort of standard answer I can point to.
I've been talking about how self signed certs are subject to man-in-the-middle attacks, but I think I'm beginning to realize this is only for SQL connections being made, and any data that flows through that connection (because in the tables the data is unencrypted, and the SSL encrypts it). But with AE, the data in the database is already simply an encrypted string.
So does the nature of an Always Encrypted solution, if done where the self signed certs are never made or live on the same server that holds the data, preclude any MIM attacks? Is simply using self signed certs the accepted "standard" way of doing it? Thinking it through, I believe the answer is yes, but would just like some sort of "official" answer lol.
Emo
(143 rep)
Feb 13, 2024, 03:18 PM
• Last activity: Feb 14, 2024, 04:42 AM
0
votes
0
answers
343
views
Insert data into Always Encrypted column using stored procedure in MSSQL
I am facing an issue while trying to insert data into SQL table which is having encrypted columns in it. Following is my approach. I have created a table with three columns: **[ID], [Name], [Password]**. **[Name]** and **[Password]** columns are encrypted. with **COLLATE Latin1_General_BIN2 ENCRYPTE...
I am facing an issue while trying to insert data into SQL table which is having encrypted columns in it.
Following is my approach.
I have created a table with three columns: **[ID], [Name], [Password]**.
**[Name]** and **[Password]** columns are encrypted.
with **COLLATE Latin1_General_BIN2 ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = [Test_CEK], ENCRYPTION_TYPE = Deterministic, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256')**
- I have set "**Column Encryption Setting = Enabled**"
- I have set "**Parameterization for Always Encrypted**"
And I have created a procedure to insert records into Test table as follows:
USE [TestDB]
GO
CREATE PROCEDURE InsertTest
@name varchar(50),
@pwd varchar(max)
AS
BEGIN
INSERT INTO [dbo].[Test] ([Name] ,[Password]) VALUES (@name, @pwd)
END
GO
When I try to insert record using the above procedure I am getting error as shown below.
>Msg 206, Level 16, State 2, Procedure InsertTest, Line 0 [Batch Start Line 0]
Operand type clash: varchar is incompatible with varchar(1) encrypted with (encryption_type = 'DETERMINISTIC', encryption_algorithm_name = 'AEAD_AES_256_CBC_HMAC_SHA_256', column_encryption_key_name = 'Test_CEK', column_encryption_key_database_name = 'TestDB') collation_name = 'SQL_Latin1_General_CP1_CI_AS'
I want to insert data into SQL table through stored procedure.
Do not want to use ADO.Net for SQL connection.
Want to implement the same using EntityFramework.
Prathap r
(1 rep)
Jan 22, 2024, 09:34 AM
• Last activity: Jan 22, 2024, 11:49 AM
0
votes
1
answers
41
views
How to migrate a database from 2016 to 2014 if there are encrypted/locked objects(SPs) associated with it?
I am trying to migrate a database from 2016 to 2014 through Task-->Generate Scripts. There are some stored procedures that are locked and seems like they are encrypted as well. They are causing error while generating the script. I have tried both schema or schema and data. I have also tried using Ta...
I am trying to migrate a database from 2016 to 2014 through Task-->Generate Scripts. There are some stored procedures that are locked and seems like they are encrypted as well. They are causing error while generating the script. I have tried both schema or schema and data.
I have also tried using Task--> Export Data tier application but that is generating error related to logins.
Looking for an advice in terms of best way to do this or how to fix the above errors. Thanks in advance.

user2664231
(1 rep)
May 9, 2023, 06:00 PM
• Last activity: May 9, 2023, 09:00 PM
0
votes
2
answers
106
views
Always Encrypt C++
Our client application is developed in C++ and uses ODBC Driver (Version 17) to connect to remote SQL database. We have enabled Always Encrypt option to encrypt the data. Using proper flags in connection string we are able to connect and perform SQL operations. Since Always Encrypt requires certific...
Our client application is developed in C++ and uses ODBC Driver (Version 17) to connect to remote SQL database. We have enabled Always Encrypt option to encrypt the data. Using proper flags in connection string we are able to connect and perform SQL operations. Since Always Encrypt requires certificate for encryption/decryption, we have added the same in the certificate store of windows.
Query is, is there a way to identify if the Always Encrypt certificate is missing in the client store, when using CDatabase::OpenEx or when executing query?
Amal Jesudas
(69 rep)
Mar 2, 2023, 09:25 AM
• Last activity: Mar 3, 2023, 06:59 PM
0
votes
1
answers
729
views
SQL Always Encrypted- can be decrypted in sql function?
I turned on Always Encrypted in sql 2016 and was able to see the column to be crypted. My question is this: can I decrypt this column in sql function without "column encryption setting = Enabled" and use "DecryptByKeyAutoCert" function?
I turned on Always Encrypted in sql 2016 and was able to see the column to be crypted. My question is this: can I decrypt this column in sql function without "column encryption setting = Enabled" and use "DecryptByKeyAutoCert" function?
DevGirl
(3 rep)
Mar 2, 2023, 01:47 PM
• Last activity: Mar 2, 2023, 03:05 PM
2
votes
1
answers
1365
views
Error while querying 'Always encrypted' column
I have set up 'Always encrypted' on one of the column in Azure SQL DB. Everything worked well except when a user is trying to access he is getting below error. ' I have tried some of the rememdies as below.*'*Failed to decrypt a column encryption key using key store provider: 'AZURE_KEY_VAULT'. Veri...
I have set up 'Always encrypted' on one of the column in Azure SQL DB. Everything worked well except when a user is trying to access he is getting below error. '
I have tried some of the rememdies as below.*'*Failed to decrypt a column encryption key using key store provider: 'AZURE_KEY_VAULT'. Verify the properties of the column encryption key and its column master key in your database'**
1. User is mapped in Azure AD group which is also mapped in Azure SQL server. Keys are stored in Azure key vault and the AD group is given 'Contributor' access and 'decrypt', 'encrypt', 'get' , unwrap' access is given through access policy.
'GRANT VIEW ANY COLUMN MASTER KEY DEFINITION' granted to AD group in SQL DB.
Sandeep P
(31 rep)
May 28, 2021, 04:32 AM
• Last activity: Feb 27, 2023, 01:30 PM
0
votes
0
answers
62
views
Error from Recordset class
We have some fields of type nchar to support Unicode characters. Always encrypt is enabled for the table. Inserting data works fine but when I try to retrieve data, the below error is thrown from Recordset Open function: >The data types nchar(128) encrypted with (encryption_type = 'DETERMINISTIC', e...
We have some fields of type nchar to support Unicode characters.
Always encrypt is enabled for the table.
Inserting data works fine but when I try to retrieve data, the below error is thrown from Recordset Open function:
>The data types nchar(128) encrypted with (encryption_type = 'DETERMINISTIC', encryption_algorithm_name = 'AEAD_AES_256_CBC_HMAC_SHA_256', column_encryption_key_name = 'ColumnEncryptionKey1', column_encryption_key_database_name = 'DATABASE') and varchar(128) encrypted with (encryption_type = 'DETERMINISTIC', encryption_algorithm_name = 'AEAD_AES_256_CBC_HMAC_SHA_256', column_encryption_key_name = 'ColumnEncryptionKey1', column_encryption_key_database_name Statement(s) could not be prepared.
I am unable to understand why varchar is mentioned in the error when the table data type is nchar.
Can someone help to decipher what the error is trying to state?
SQL Server version:
SQL Server 2019 Express Edition
Table generation script:
USE [DATABASE]
CREATE TABLE [dbo].[TABLE_1](
[secondName] [nchar](128) NULL,
[firstName] [nchar](128) NULL,
[Id] [nchar](128) NOT NULL,
[height] [int] NULL,
PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
The query that is passed to Recordset open function is a select query with Id as search key.
select * from TABLE_1 where Id=?
The previous error was obtained when I did remote debugging. On a similar environment when done locally, the below error is obtianed:
> The data types nchar(128) encrypted with (encryption_type =
> 'DETERMINISTIC', encryption_algorithm_name =
> 'AEAD_AES_256_CBC_HMAC_SHA_256', column_encryption_key_name =
> 'ColumnEncryptionKey1', column_encryption_key_database_name =
> 'DATABASE') and varchar(128) encrypted with (encryption_type =
> 'DETERMINISTIC', encryption_algorithm_name =
> 'AEAD_AES_256_CBC_HMAC_SHA_256', column_encryption_key_name =
> 'ColumnEncryptionKey1', column_encryption_key_database_name =
> 'DATABASE') collation Statement(s) could not be prepared.
Setting sting parameter is done in DoFieldExchange() function of Recordset class as below:
RFX_Text(pFX, ParameterName, *pstring, Length);
where pstring is of type CString*
Amal Jesudas
(69 rep)
Feb 12, 2023, 06:02 AM
• Last activity: Feb 13, 2023, 01:19 PM
-1
votes
2
answers
898
views
Always Encrypted SQL Server : How to insert / updated using C++ code?
I have set the Always Encrypted option to my table columns. Now I need to insert or update my table from my C++ Client application. I know that we need to declare params and use them for Always Encrypted. From SSMS I am able to do so without any issues. However, when I try using C++ code, it fails....
I have set the Always Encrypted option to my table columns.
Now I need to insert or update my table from my C++ Client application.
I know that we need to declare params and use them for Always Encrypted.
From SSMS I am able to do so without any issues.
However, when I try using C++ code, it fails.
Say, for example, my table contains two columns that have data type int.
I connect to my database using the below connection string:
"Driver={ODBC Driver 17 for SQL Server};Server=192.122.200.200,1433;Encrypt=no;Trusted_Connection=no;ColumnEncryption=Enabled;DATABASE=AlwaysEncrypted;UID=sa;PWD=;";
I create the below SQL query string:
CString csQStrInsert = L"declare @val1 int = 3; declare @val2 int = 3; insert into [dbo].[Table_AlwaysEncrypted] ([col1], [col2]) values (@val1, @val2);";
And call
pDatabase->ExecuteSQL(csQStrInsert);
However, this throws exception with the following info:
*Encryption scheme mismatch for columns/variables '@val1'. The encryption scheme for the columns/variables is (encryption_type = 'PLAINTEXT') and the expression near line '1' expects it to be DETERMINISTIC, or PLAIN TEXT.*
Can someone help me out? There is mostly no help when it comes to C++ code for Always Encrypted.
[Trialed code that still fails in SQL Execute:]
SQLHENV henv = SQL_NULL_HENV;
SQLHDBC hdbc1 = SQL_NULL_HDBC;
SQLHSTMT hstmt = SQL_NULL_HSTMT;
SQLRETURN rc = 0;
RETCODE retcode;
retcode = SQLAllocHandle(SQL_HANDLE_ENV, NULL, &henv);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, SQL_IS_INTEGER); // Notify ODBC that this is an ODBC 3.0 app.
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc1); // Allocate ODBC connection handle and connect.
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
SQLCHAR retconstring;
SQLWCHAR connstr[] = L"Driver={ODBC Driver 17 for SQL Server};Server=192.168.555.128,1433;Encrypt=no;Trusted_Connection=no;ColumnEncryption=Enabled;DATABASE=AEncrypted;UID=Supervisor;PWD=;";
retcode = SQLDriverConnect(hdbc1, NULL, connstr, SQL_NTS, (SQLWCHAR*)retconstring, 1024, NULL, SQL_DRIVER_NOPROMPT);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
SQLWCHAR sqlstate;
SQLWCHAR message;
if (SQL_SUCCESS == SQLGetDiagRec(SQL_HANDLE_DBC, hdbc1, 1, sqlstate, NULL, message, 1024, NULL))
{
CString csMsg = message;
}
retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc1, &hstmt); // Allocate statement handle.
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
//
}
}
}
}
}
std::string queryText = "INSERT INTO [dbo].[Table_AEncrypted] ([col1], [col2]) VALUES (?, ?);";
rc = SQLPrepare(hstmt, (SQLWCHAR *)queryText.c_str(), SQL_NTS);
SQLINTEGER val1 = 123;
SQLINTEGER val2 = 456;
SQLINTEGER cbNull = SQL_NULL_DATA;
rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, SQL_DESC_PRECISION, 0, (SQLPOINTER)val1, 0, &cbNull);
rc = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, SQL_DESC_PRECISION, 0, (SQLPOINTER)val2, 0, &cbNull);
rc = SQLExecute(hstmt);
// Clean up.
SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
SQLDisconnect(hdbc1);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
Amal Jesudas
(69 rep)
Oct 26, 2022, 10:03 PM
• Last activity: Nov 7, 2022, 03:11 PM
2
votes
3
answers
2218
views
SQL Server Management Studio throws exception when using Always Encrypted
With the latest version of SSMS (v18.10), when we choose to sign in to Azure to use key vault, it always gives this exception: `Rotate (Microsoft.SqlServer.Management.ServiceManagement)` [![enter image description here][1]][1] And the error details: ``` =================================== Exception...
With the latest version of SSMS (v18.10), when we choose to sign in to Azure to use key vault, it always gives this exception:
And the error details:
Looks like it does not support the newly added Rotate permission of Azure key vault keys.
Rotate (Microsoft.SqlServer.Management.ServiceManagement)

===================================
Exception has been thrown by the target of an invocation. (mscorlib)
------------------------------
Program Location:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at Microsoft.SqlServer.Management.ActionHandlers.DialogBasedActionHandler.RunTaskFormThread(Object contextObject)
===================================
Rotate (Microsoft.SqlServer.Management.ServiceManagement)
------------------------------
Program Location:
at Microsoft.SqlServer.Management.ServiceManagement.ResourceManagement.AzureKeyVaultKeyPermissionEnumConverter.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter, JsonReader reader, Type objectType, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at Microsoft.SqlServer.Management.ServiceManagement.ResourceManagement.AzureKeyVaultMethods.GetAzureKeyVault(ServiceSubscription subscription, String resourceGroupName, String keyVaultName, AzureKeyVaultResource& keyVault)
at Microsoft.SqlServer.Management.ServiceManagement.ResourceManagement.ResourceManagement.GetAzureKeyVault(String resourceGroupName, String keyVaultName, AzureKeyVaultResource& keyVault)
at Microsoft.SqlServer.Management.AlwaysEncryptedCommonControl.ColumnMasterKeyStoreCommonControl.AzureKeyVaultComboBox_SelectedValueChanged(Object sender, EventArgs e)
at System.Windows.Forms.ListControl.OnSelectedValueChanged(EventArgs e)
at System.Windows.Forms.ComboBox.OnSelectedValueChanged(EventArgs e)
at System.Windows.Forms.ComboBox.OnSelectedIndexChanged(EventArgs e)
at System.Windows.Forms.ComboBox.set_SelectedIndex(Int32 value)
at Microsoft.SqlServer.Management.AlwaysEncryptedCommonControl.ColumnMasterKeyStoreCommonControl.SetAdalCredential(Object sender, AzureAuthenticationUIEventArgs e)
at Microsoft.SqlServer.Management.Azure.UI.CommonUserControl.OnSessionLoggedInEventSet(AzureAuthenticationUIEventArgs e)
at Microsoft.SqlServer.Management.Azure.UI.CommonUserControl.authenticationWorker_RunWorkerCompleted(Object sender, RunWorkerCompletedEventArgs e)
at System.ComponentModel.BackgroundWorker.OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
at System.ComponentModel.BackgroundWorker.AsyncOperationCompleted(Object arg)

unruledboy
(121 rep)
Jan 26, 2022, 11:09 PM
• Last activity: May 20, 2022, 01:08 PM
5
votes
2
answers
5968
views
Is it possible to bulk insert data into a table that has columns encrypted with Always Encrypted?
In SSMS we are attempting to bulk insert from a csv file into a table that has a column encrypted using SQL Server 2016's Always Encrypted feature. This is the command we're using: INSERT INTO membersE SELECT * FROM OPENROWSET( BULK 'c:\members.csv', FORMATFILE = 'c:\membersEFormat.xml', FIRSTROW =...
In SSMS we are attempting to bulk insert from a csv file into a table that has a column encrypted using SQL Server 2016's Always Encrypted feature.
This is the command we're using:
INSERT INTO membersE
SELECT *
FROM OPENROWSET(
BULK 'c:\members.csv',
FORMATFILE = 'c:\membersEFormat.xml',
FIRSTROW = 2
) m
This returns the typical error you get when attempting to insert into an encrypted column:
>Msg 206, Level 16, State 2, Line 6
Operand type clash: varbinary is incompatible with varchar(50) encrypted with (encryption_type = 'DETERMINISTIC', encryption_algorithm_name = 'AEAD_AES_256_CBC_HMAC_SHA_256', column_encryption_key_name = 'CEK_Auto1', column_encryption_key_database_name = 'DATABASE') collation_name = 'Latin1_General_BIN2'
We understand that you can't insert into an encrypted column via SSMS and that you need to use a .NET 4.6.1+ client, but we'd like to know if bulk insert operations are not possible as well?
**SAMPLE CODE THAT WORKED FOR ME**
(to satisfy Windows10's request)
SqlCommand cmd;
SqlConnection conn;
SqlBulkCopy copy;
SqlDataAdapter da;
DataTable dt;
using (conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database"].ConnectionString))
{
conn.Open();
using (cmd = new SqlCommand("SELECT * FROM members", conn))
using (copy = new SqlBulkCopy(conn))
using (da = new SqlDataAdapter(cmd))
using (dt = new DataTable())
{
cmd.CommandTimeout = 600;
da.Fill(dt);
cmd.CommandText = "TRUNCATE TABLE membersE";
cmd.ExecuteNonQuery();
copy.DestinationTableName = "membersE";
copy.WriteToServer(dt);
}
}
RoastBeast
(679 rep)
Jan 10, 2017, 03:56 PM
• Last activity: Jul 16, 2021, 01:13 PM
1
votes
0
answers
313
views
Error when trying to apply Always Encrypted encryption on aspnet_Membership table
I'm trying to encrypt the Email and LoweredEmail columns on a SQL database using Always Encrypted encryption using SSMS and I always get the same error: > Lock request time out period exceeded. There is no user table matching the input name '[dbo].[aspnet_Membership]' in the current database or you...
I'm trying to encrypt the Email and LoweredEmail columns on a SQL database using Always Encrypted encryption using SSMS and I always get the same error:
> Lock request time out period exceeded. There is no user table matching the input name '[dbo].[aspnet_Membership]' in the current database or you do not have permission to access the table.
I still get the same error when I log in as the SA account, sio I guess that it's not a permissions problem.
EDIT *** Added the PowerShell Script
# Generated by SQL Server Management Studio at 10:47 on 06/05/2021
Import-Module SqlServer
# Set up connection and database SMO objects
$password = ""
$sqlConnectionString = "Data Source=GALADRIEL\SKYLINE;Initial Catalog=SkylineMembership;User ID=sa;Password=$password;MultipleActiveResultSets=False;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;Packet Size=4096;Application Name=





"Microsoft SQL Server Management Studio
""
$smoDatabase = Get-SqlDatabase -ConnectionString $sqlConnectionString
# If your encryption changes involve keys in Azure Key Vault, uncomment one of the lines below in order to authenticate:
# * Prompt for a username and password:
#Add-SqlAzureAuthenticationContext -Interactive
# * Enter a Client ID, Secret, and Tenant ID:
#Add-SqlAzureAuthenticationContext -ClientID '' -Secret '' -Tenant ''
# Change encryption schema
$encryptionChanges = @()
# Add changes for table [dbo].[aspnet_Membership]
$encryptionChanges += New-SqlColumnEncryptionSettings -ColumnName dbo.aspnet_Membership.Email -EncryptionType Deterministic -EncryptionKey "CEK_Auto1"
$encryptionChanges += New-SqlColumnEncryptionSettings -ColumnName dbo.aspnet_Membership.LoweredEmail -EncryptionType Deterministic -EncryptionKey "CEK_Auto1"
Set-SqlColumnEncryption -ColumnEncryptionSettings $encryptionChanges -InputObject $smoDatabase
Paul Clark
(11 rep)
May 6, 2021, 08:28 AM
• Last activity: May 6, 2021, 09:54 AM
1
votes
2
answers
1520
views
Backup and restore SQL Server database with encrypted columns: what should I backup along with database?
Assume I've backed up a SQL Server database with Management Studio, and that database have some encrypted columns. Now I want to restore this database into another fresh-enrolled MSSQL server. To use encrypted columns I had to: 1. Create database master key; 2. Create certificate; 3. Create symmetri...
Assume I've backed up a SQL Server database with Management Studio, and that database have some encrypted columns. Now I want to restore this database into another fresh-enrolled MSSQL server. To use encrypted columns I had to:
1. Create database master key;
2. Create certificate;
3. Create symmetric key.
Which of those are stored along with backup, and which I should backup manually and restore on the other server manually as well? I see there is SQL statements
backup master key
, restore master key
, backup certificate
, create certificate ... from file
. So, along with database itself, there are:
1. Master key;
2. Master key password;
3. Certificate;
4. Certificate private key;
5. Symmetric key.
So what (and how) should I deal with when I restore my database on a fresh server? Thank you!
nyan-cat
(111 rep)
Apr 9, 2021, 11:08 AM
• Last activity: Apr 9, 2021, 11:50 AM
3
votes
1
answers
3116
views
Always Encrypted: How do I do an equality join with a non-encrypted column to a deterministic encrypted column?
The official Microsoft documentation for [Always Encrypted on SQL Server 2017][1] states: > Deterministic encryption always generates the same encrypted value for any given plain text value. > > Using deterministic encryption allows point lookups, **equality joins**, grouping and indexing on encrypt...
The official Microsoft documentation for Always Encrypted on SQL Server 2017 states:
> Deterministic encryption always generates the same encrypted value for any given plain text value.
>
> Using deterministic encryption allows point lookups, **equality joins**, grouping and indexing on encrypted columns.
(bold emphasis mine)
I'm currently using SQL Server 2017 RTM-CU17 (KB4515579) v14.0.3238.1 Standard Edition.
My SSMS (currently using v18.4) connection is already configured with the
Enable Always Encrypted (column encryption)
checkbox checked, and the Query Options -> Execution -> Advanced setting Enable Parameterization for Always Encrypted
is also checked.
Below is the table schema I have.
The EmployeeID
and FullName
columns are encrypted with Deterministic Encryption Type
.
The Temp
column is encrypted with Randomized Encryption Type
.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[EmployeeTemperature]
(
[Entry] [int] IDENTITY(1,1) NOT NULL,
[CheckerID] [varchar](26) NOT NULL,
[EmployeeID] [char](10) COLLATE Latin1_General_BIN2 ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = [CEK_Auto1], ENCRYPTION_TYPE = Deterministic, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256') NOT NULL,
[FullName] [varchar](50) COLLATE Latin1_General_BIN2 ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = [CEK_Auto1], ENCRYPTION_TYPE = Deterministic, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256') NULL,
[Temp] [decimal](4, 1) ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = [CEK_Auto1], ENCRYPTION_TYPE = Randomized, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256') NOT NULL,
[Date] [date] NOT NULL, -- to support Date-CheckerID-FullName unique constraint
[DateTime] [datetime] NOT NULL,
[Station] [smallint] NOT NULL,
[Question1] [bit] NOT NULL,
[Question2] [bit] NOT NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
CREATE UNIQUE CLUSTERED INDEX [UCI_EmployeeTemperature]
ON [dbo].[EmployeeTemperature]
(
[Date] ASC,
[CheckerID] ASC,
[FullName] ASC
)
WITH
(
PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON
) ON [PRIMARY]
GO
The below Stored Procedure
code is used to retrieve the encrypted data (eventually will be used by an ASPX Single Page Application (SPA) web app).
--SELECT OBJECT_ID('dbo.sp_GetEmployeeTemps','P') -- debug below
IF OBJECT_ID('dbo.sp_GetEmployeeTemps','P') IS NULL
EXEC('CREATE PROCEDURE [dbo].[sp_GetEmployeeTemps] AS BEGIN SET NOCOUNT ON; END')
GO
ALTER PROCEDURE [dbo].[sp_GetEmployeeTemps]
AS
SELECT
ET.[Entry]
,CASE
WHEN HR.[Employee_ID] IS NOT NULL THEN 'E'
ELSE 'V'
END AS [Visitor] -- Show if record is for Employee or Visitor
,ISNULL(HR.[Name],ET.[FullName]) AS [Name] -- ISNULL for visitor. return visitor's name if not an employee.
,ET.[Temp]
,(SELECT DISTINCT chk.[Name] FROM [dbo].[Checker] AS chk INNER JOIN [dbo].[EmployeeTemperature] ON ET.[CheckerID] = chk.[LoginID]) AS [Checker]
,CAST(FORMAT(ET.[DateTime], 'yyyy-MM-dd hh:mm:ss', 'en-US') AS DATETIME) AS [Time] -- so that it doesn't round seconds to minutes (converting to SMALLDATETIME does that) and shows to the second.
,CASE
WHEN ET.[Question1] = 1 THEN 'Yes'
WHEN ET.[Question1] = 0 THEN 'No'
ELSE NULL
END AS [Question1]
,CASE
WHEN ET.[Question2] = 1 THEN 'Yes'
WHEN ET.[Question2] = 0 THEN 'No'
ELSE NULL
END AS [Question2]
FROM [dbo].[vw_Employees] AS HR
FULL JOIN -- to allow Visitors to be retrieved
(
SELECT
[Entry]
,[Temp]
,[CheckerID]
,[FullName]
,[EmployeeID]
,[DateTime]
,[Question1]
,[Question2]
FROM [dbo].[EmployeeTemperature]
WHERE CONVERT(DATE, [DateTime]) = CONVERT(DATE, GETDATE())
) AS ET
ON HR.[Employee_ID] = ET.[EmployeeID] -- encrypted
WHERE ET.[Entry] IS NOT NULL -- to not show unchecked employees.
GO
EXEC sp_refresh_parameter_encryption 'dbo.sp_GetEmployeeTemps';
When I try to create or alter the above procedure, I receive the below error:
data types char and char(10) encrypted with (encryption_type = 'DETERMINISTIC', encryption_algorithm_name = 'AEAD_AES_256_CBC_HMAC_SHA_256', column_encryption_key_name = 'CEK_Auto1', column_encryption_key_database_name = 'Employee_Temperature') collation_name = 'Latin1_General_BIN2' are incompatible in the equal to operator.
This seems to indicate that the issue is with this JOIN clause:
ON HR.[Employee_ID] = ET.[EmployeeID] -- encrypted
In this join, the HR.[Employee_ID]
is not encrypted, and part of the [vw_Employees]
view, and the ET.[EmployeeID]
is the encrypted column.
Why does this equality join not work? The documentation states that encrypted columns can be used in equality joins, which this clearly is.
That's problem #1.
----------
Problem #2 seems to be with my ISNULL
involving the encrypted column ET.[FullName]
.
If i comment out that join and do ON 1 = 1
for debugging purposes, I get an additional error:
type clash: varchar(50) encrypted with (encryption_type = 'DETERMINISTIC', encryption_algorithm_name = 'AEAD_AES_256_CBC_HMAC_SHA_256', column_encryption_key_name = 'CEK_Auto1', column_encryption_key_database_name = 'Employee_Temperature') collation_name = 'Latin1_General_BIN2' is incompatible with varchar
Any suggestions to handle that scenario?
I have requested the MS Docs be updated with examples on GitHub:
https://github.com/MicrosoftDocs/sql-docs/issues/4550
John G Hohengarten
(674 rep)
Apr 13, 2020, 10:56 PM
• Last activity: Apr 14, 2020, 06:35 PM
0
votes
1
answers
812
views
How do we restore database configured with Always Encrypted between environments and enable web app to encrypt/decrypt data?
I have a SQL Server 2016 database configured with AE in Production environment. Column Master key Certificate is available(under Windows Certificate Store -> Local Machine location) in a different server other than database server and the web server hosting ASP.NET application also has the Certifica...
I have a SQL Server 2016 database configured with AE in Production environment. Column Master key Certificate is available(under Windows Certificate Store -> Local Machine location) in a different server other than database server and the web server hosting ASP.NET application also has the Certificate to encrypt/decrypt data.
For troubleshooting or operational support purpose, we usually backup and restore this Production database to our staging/UAT environment on need basis.
After restoring the database, web app is NOT working as expected since it's missing the Certificate used to configure AE in Production environment.
As per the security policy, client is NOT willing to bring the AE Certificate from Windows Certificate Store to the target environment.
In this regard, what are the best practices to be followed to make the web app working when we restore database from one environment to another? Or is there any other approaches to make the web app working without bringing the Certificate?
Sri
(403 rep)
Nov 18, 2019, 09:17 PM
• Last activity: Nov 19, 2019, 05:09 AM
1
votes
1
answers
143
views
SQL Server Security - Encryption
I have requirement to store the some sensitive information in **SQL Server 2016**. Organization policy demands, it should be encrypted. **Business Requirement:** 1. Data cannot be decrypted by DBA. 2. Data cannot be decrypted by Developers. 3. Only end users able to access the decrypted data. I have...
I have requirement to store the some sensitive information in **SQL Server 2016**. Organization policy demands, it should be encrypted.
**Business Requirement:**
1. Data cannot be decrypted by DBA.
2. Data cannot be decrypted by Developers.
3. Only end users able to access the decrypted data.
I have looked into some of the encryption options:
- Column Level Encryption
- Always Encrypted
But both of these options, does not satisfy the requirements. In
Column Level Encryption
, data can be accessed by DBA and Developers.
Always Encrypted
does not allow DBAs to decrypt data but still Developers can be able to view the decrypted data, since data decrypted at the client driver
.
I am thinking there must be some other method or combination of techniques can be used to achieve this requirements.
Please suggest on this. Thank you!
Rathish Kumar B
(2320 rep)
Jul 10, 2019, 11:54 AM
• Last activity: Nov 6, 2019, 01:00 PM
-1
votes
1
answers
179
views
Does Always Encrypted cause blocking queries or locking object?
I have perform some test with relatively small tables and the encryption took a very long time (maybe because there are many SQL objects that are referring the target table). I am wondering(worrying) when a table is being encrypted, is it possible to read the encrypted data or query the table? I onl...
I have perform some test with relatively small tables and the encryption took a very long time (maybe because there are many SQL objects that are referring the target table).
I am wondering(worrying) when a table is being encrypted, is it possible to read the encrypted data or query the table? I only found this :
> While the data is being re-encrypted, your applications cannot write
> to the impacted tables. Therefore, your organization needs to plan a
> column encryption key rotation very carefully.
and it is about the rotation.
gotqn
(4348 rep)
Feb 19, 2018, 10:18 AM
• Last activity: Oct 26, 2019, 07:02 PM
1
votes
0
answers
735
views
SSIS: Error from ODBC provider when inserting into Always Encrypted table using CMK in Azure Key Store
I'm trying to use SSIS (Visual Studio 2017) to insert data into an Azure SQL table with a column that's encrypted via Always Encrypted, and a column master key stored in Azure Key Vault, but I keep getting errors. Per advice I've found in other posts, I'm using an ADO.NET destination with the ODBC d...
I'm trying to use SSIS (Visual Studio 2017) to insert data into an Azure SQL table with a column that's encrypted via Always Encrypted, and a column master key stored in Azure Key Vault, but I keep getting errors.
Per advice I've found in other posts, I'm using an ADO.NET destination with the ODBC data provider. The user is an Azure Active Directory user that's provisioned in the Azure SQL database so it's able to log in. It has all key privileges granted to it in the key vault's access policy.
My ODBC connection string looks like this:
DRIVER=ODBC Driver 17 for SQL Server;SERVER=[dbs].database.windows.net;UID=[uid@example.com];PWD=[password];Authentication=ActiveDirectoryPassword;DATABASE=[database];ColumnEncryption=Enabled;KeyStoreAuthentication=KeyVaultPassword;KeyStorePrincipalId=[uid@example.com];KeyStoreSecret=[password]
I expect that to insert into the table, but it results in this error:
[ADO NET Destination ] Error: An exception has occurred during data insertion, the message returned from the provider is: ERROR [CE269] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Error 12038 sending request to https://[keyvault].vault.azure.net:443
ERROR [CE263] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Error verifying signature of ECEK.
ERROR [CE202] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The keystore provider AZURE_KEY_VAULT failed to decrypt the ECEK https://[keyvault].vault.azure.net:443/keys/[cmk]/[version] with RSA_OAEP.
I'm able to successfully insert into the table via SSMS and also via a custom console app, and verify the encryption works as expected. The error only occurs from SSIS.
jessewdouglas
(11 rep)
Jun 12, 2019, 03:04 PM
• Last activity: Oct 16, 2019, 08:22 PM
Showing page 1 of 20 total questions