From 258d412b9adb98787f950fb2bfd708bb40f2242b Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Fri, 28 May 2021 10:59:27 +0200 Subject: [PATCH] Database: Add Encryption methods for connection string --- Modules.Database/Adapters/MSSQLServer.vb | 29 ++++++++++++++++++++++++ Modules.Database/Database.vbproj | 4 ++++ 2 files changed, 33 insertions(+) diff --git a/Modules.Database/Adapters/MSSQLServer.vb b/Modules.Database/Adapters/MSSQLServer.vb index a105e174..da5bca1b 100644 --- a/Modules.Database/Adapters/MSSQLServer.vb +++ b/Modules.Database/Adapters/MSSQLServer.vb @@ -1,6 +1,7 @@ Imports System.ComponentModel Imports System.Data.Common Imports System.Data.SqlClient +Imports DigitalData.Modules.Encryption Imports DigitalData.Modules.Logging Public Class MSSQLServer @@ -52,6 +53,34 @@ Public Class MSSQLServer End Try End Sub + ''' + ''' Encrypts a connection string password. + ''' + ''' A connection string with a plain-text password + ''' The connection string with the password encrypted. + Public Shared Function EncryptConnectionString(ConnectionString As String) As String + Dim oEncryption As New EncryptionLegacy() + Dim oBuilder As New SqlConnectionStringBuilder() With {.ConnectionString = ConnectionString} + Dim oEncryptedPassword = oEncryption.EncryptData(oBuilder.Password) + oBuilder.Password = oEncryptedPassword + + Return oBuilder.ToString() + End Function + + ''' + ''' Decrypts a connection string password. + ''' + ''' A connection string with a encrypted password + ''' The connection string with the password decrypted. + Public Shared Function DecryptConnectionString(ConnectionString As String) As String + Dim oEncryption As New EncryptionLegacy() + Dim oBuilder As New SqlConnectionStringBuilder() With {.ConnectionString = ConnectionString} + Dim oDecryptedPassword = oEncryption.DecryptData(oBuilder.Password) + oBuilder.Password = oDecryptedPassword + + Return oBuilder.ToString() + End Function + Public Function GetConnectionString(Server As String, Database As String, UserId As String, Password As String) As String Dim oConnectionStringBuilder As New SqlConnectionStringBuilder() With { .DataSource = Server, diff --git a/Modules.Database/Database.vbproj b/Modules.Database/Database.vbproj index 045a799c..d8b0b982 100644 --- a/Modules.Database/Database.vbproj +++ b/Modules.Database/Database.vbproj @@ -139,6 +139,10 @@ + + {8a8f20fc-c46e-41ac-bee7-218366cfff99} + Encryption + {903b2d7d-3b80-4be9-8713-7447b704e1b0} Logging