如何保护 Vb6 应用程序和 mssql 服务器之间的连接?

How to secure connection between Vb6 application and mssql server?

我正在开发一个遗留应用程序,它使用 sqloledb 提供程序和 activex 数据对象连接到 mssql 数据库。现在我需要加密应用程序和服务器之间的连接,而不需要 sql 服务器中的强制加密选项。

我在 sql 服务器实例中安装了自签名证书,并尝试将 Encrypt=trueTrustservercertificate=true 放入连接字符串中。但是连接没有加密。

我已尝试将 ODBC 提供程序与 ADO 一起使用,并且在使用 encrypt=truetrustservercertificate=true 时,我遇到了打开连接的 SSL 安全错误。

请告诉我如何使用 ADO 2.8 库建立安全连接。

Private Sub Command1_Click()
    Dim sConnectionString As String
    Dim strSQLStmt As String

     '-- Build the connection string
     'sConnectionString = "UID=userid;PWD=password;Initial Catalog=EHSC_SYM_Kings_Development;Server=EHILP-257\MIB14;Provider=MSOLEDBSQL;Encrypt=YES;trustServerCertificate=YES"
     'sConnectionString = "Provider=sqloledb;Data Source=192.168.27.91\MIB14;Initial Catalog=EHSC_SYM_Kings_Development;User Id=userid;Password=password;Encrypt=YES;trustServerCertificate=YES"
     'sConnectionString = "driver={SQL Server};server=192.168.27.91\MIB14;user id=userid;password=password;Initial Catalog=EHSC_SYM_Kings_Development;Encrypt=Yes;trustServerCertificate=True"
     sConnectionString = "Provider=SQLNCLI11;Server=192.168.27.91\MIB14;Database=EHSC_SYM_Kings_Development;Uid=userid;Pwd=password;Encrypt=yes;trustServerCertificate=True"

     strSQLStmt = "select * from dbo.patient where pat_pid = '1001'"

    'DB WORK
    Dim db As New ADODB.Connection
    Dim cmd As New ADODB.Command
    Dim rs As New ADODB.Recordset
    Dim result As String

    db.ConnectionString = sConnectionString
    db.Open 'open connection

    With cmd
      .ActiveConnection = db
      .CommandText = strSQLStmt
      .CommandType = adCmdText
    End With

    With rs
      .CursorType = adOpenStatic
      .CursorLocation = adUseClient
      .LockType = adLockOptimistic
      .Open cmd
    End With

    If rs.EOF = False Then
        rs.MoveFirst
        Let result = rs.Fields(0)
    End If
    'close conns
    rs.Close
    db.Close
    Set db = Nothing
    Set cmd = Nothing
    Set rs = Nothing
End Sub

谢谢大家的建议,我终于通过将驱动程序更改为 sqlserver native client 11(ODBC) 来确保连接安全。看起来 sqloledb 不支持 tls。将驱动程序更改为 ODBC 似乎并没有太大改变行为。我的其余代码无需任何更改即可正常工作

sConnectionString = "Driver={SQL Server Native Client 11.0};Server=192.168.27.91\MIB14;Database=xxxxxxxx;user id=xxxxxxx;password=xxxxxxxxx;Encrypt=yes;TrustServerCertificate=yes"

我自己刚刚经历过这个。

对于您的原始连接字符串,您要查找的关键字是 而不是 "Encrypt=true" 或 "Encrypt=yes" 就像大多数互联网会让您相信的那样.这是 "Use Encryption for Data=true".

因此您的连接字符串变为:

sConnectionString = "Provider=SQLNCLI11;Server=192.168.27.91\MIB14;Database=EHSC_SYM_Kings_Development;Uid=userid;Pwd=password;Use Encryption for Data=true;trustServerCertificate=True"

trustservercertificate=true 仅用于使用自签名证书进行测试...并使您自己暴露于中间人攻击;)

作为参考,这是经过以下测试的: VB6 通过 ADODB 使用 SQLNCLI11 提供程序连接到 SQL Server 2016。