使用对称密钥加密时无法使经典 ASP 参数化查询正常工作
Cannot get classic ASP parameterized queries working when using Symmetric key encryption
我在数据库上设置了密钥和证书,并且在使用 HeidiSQL 直接输入查询时我可以加密。但我遇到的问题是使用经典 asp 让它工作。
我设置了一个简单的 table 用于测试,其中包含一个自动递增标识列和一个用于保存加密数据的 var binary max 列。
这是我的代码:
chtest.commandtext="OPEN SYMMETRIC KEY SymmetricKey DECRYPTION BY CERTIFICATE [Certificate];"
set rschtest = chtest.execute
chtest.commandtext="INSERT into thetable(thevalue) values(?)"
chtest.Parameters.Append chtest.CreateParameter ("@thevalue", adVarBinary, adParamInput,8000, "EncryptByKey(Key_GUID('SymmetricKey'), 'some text here')" )
set rschtest = chtest.execute
当二进制数据输入 table 时,这似乎有效,但是当直接在服务器上解密时,它只显示 'Null'.
直接在服务器上执行这些查询确实有效,例如:
OPEN SYMMETRIC KEY SymmetricKey DECRYPTION BY CERTIFICATE Certificate
INSERT INTO thetable (thevalue) VALUES (EncryptByKey(Key_GUID('SymmetricKey'), 'some text here'))
然后...
OPEN SYMMETRIC KEY SymmetricKey DECRYPTION BY CERTIFICATE [Certificate]
SELECT CONVERT(varchar, DecryptByKey(thevalue)) AS thevalue FROM thetable
有效并显示正确的解密值。
我认为使用经典 asp 方法时的问题是必须将加密的字符串作为 varchar 插入到 varbinary 中?或者类似这样的东西。
此外,如果我不使用参数化查询,那么一切正常,但显然我想使用准备好的查询来确保安全。
请注意,这是一个遗留应用程序,所以我必须使用经典 asp。
感谢任何帮助。
目前 .CreateParameter()
调用没有意义,您需要对 ADODB.Command
进行一些重组。您应该始终通过参数传递值的基本数据类型,之后任何操作 (在本例中为 EncryptByKey()
) 应该在 CommandText
定义的查询中完成.
Dim sql: sql = ""
sql = sql & "OPEN SYMMETRIC KEY SymmetricKey DECRYPTION BY CERTIFICATE [Certificate]" & vbCrLf
sql = sql & "INSERT INTO thetable (thevalue) VALUES (EncryptByKey(Key_GUID('SymmetricKey'), ?))"
With chtest
.ActiveConnection = your_connection_string
.CommandText = sql
.CommandType = adCmdText
Call .Parameters.Append(.CreateParameter("@thevalue", adVarChar, adParamInput, 8000, "some text here")
'As you are doing an INSERT use adExecuteNoRecords to cut down on processing
'and improve performance.
Set rschtest = chtest.Execute(, , adExecuteNoRecords)
End With
你会发现我变了
.CreateParameter ("@thevalue", adVarBinary, adParamInput,8000, "EncryptByKey(Key_GUID('SymmetricKey'), 'some text here')" )
到
.CreateParameter("@thevalue", adVarChar, adParamInput, 8000, "some text here")
这是因为当您在这种情况下操作基值时,字符串 'some text here'
对它的任何操作都需要在传递基值之外完成。
这就是 .CommandText
现在包含 EncryptByKey(Key_GUID('SymmetricKey'), ?)
的原因,因此在执行期间只有基值通过 Parameter
集合传递。
我在数据库上设置了密钥和证书,并且在使用 HeidiSQL 直接输入查询时我可以加密。但我遇到的问题是使用经典 asp 让它工作。 我设置了一个简单的 table 用于测试,其中包含一个自动递增标识列和一个用于保存加密数据的 var binary max 列。
这是我的代码:
chtest.commandtext="OPEN SYMMETRIC KEY SymmetricKey DECRYPTION BY CERTIFICATE [Certificate];"
set rschtest = chtest.execute
chtest.commandtext="INSERT into thetable(thevalue) values(?)"
chtest.Parameters.Append chtest.CreateParameter ("@thevalue", adVarBinary, adParamInput,8000, "EncryptByKey(Key_GUID('SymmetricKey'), 'some text here')" )
set rschtest = chtest.execute
当二进制数据输入 table 时,这似乎有效,但是当直接在服务器上解密时,它只显示 'Null'.
直接在服务器上执行这些查询确实有效,例如:
OPEN SYMMETRIC KEY SymmetricKey DECRYPTION BY CERTIFICATE Certificate
INSERT INTO thetable (thevalue) VALUES (EncryptByKey(Key_GUID('SymmetricKey'), 'some text here'))
然后...
OPEN SYMMETRIC KEY SymmetricKey DECRYPTION BY CERTIFICATE [Certificate]
SELECT CONVERT(varchar, DecryptByKey(thevalue)) AS thevalue FROM thetable
有效并显示正确的解密值。
我认为使用经典 asp 方法时的问题是必须将加密的字符串作为 varchar 插入到 varbinary 中?或者类似这样的东西。
此外,如果我不使用参数化查询,那么一切正常,但显然我想使用准备好的查询来确保安全。
请注意,这是一个遗留应用程序,所以我必须使用经典 asp。
感谢任何帮助。
目前 .CreateParameter()
调用没有意义,您需要对 ADODB.Command
进行一些重组。您应该始终通过参数传递值的基本数据类型,之后任何操作 (在本例中为 EncryptByKey()
) 应该在 CommandText
定义的查询中完成.
Dim sql: sql = ""
sql = sql & "OPEN SYMMETRIC KEY SymmetricKey DECRYPTION BY CERTIFICATE [Certificate]" & vbCrLf
sql = sql & "INSERT INTO thetable (thevalue) VALUES (EncryptByKey(Key_GUID('SymmetricKey'), ?))"
With chtest
.ActiveConnection = your_connection_string
.CommandText = sql
.CommandType = adCmdText
Call .Parameters.Append(.CreateParameter("@thevalue", adVarChar, adParamInput, 8000, "some text here")
'As you are doing an INSERT use adExecuteNoRecords to cut down on processing
'and improve performance.
Set rschtest = chtest.Execute(, , adExecuteNoRecords)
End With
你会发现我变了
.CreateParameter ("@thevalue", adVarBinary, adParamInput,8000, "EncryptByKey(Key_GUID('SymmetricKey'), 'some text here')" )
到
.CreateParameter("@thevalue", adVarChar, adParamInput, 8000, "some text here")
这是因为当您在这种情况下操作基值时,字符串 'some text here'
对它的任何操作都需要在传递基值之外完成。
这就是 .CommandText
现在包含 EncryptByKey(Key_GUID('SymmetricKey'), ?)
的原因,因此在执行期间只有基值通过 Parameter
集合传递。