Database: Add Encryption methods for connection string

This commit is contained in:
Jonathan Jenne
2021-05-28 10:59:27 +02:00
parent aa6e211957
commit 258d412b9a
2 changed files with 33 additions and 0 deletions

View File

@@ -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
''' <summary>
''' Encrypts a connection string password.
''' </summary>
''' <param name="ConnectionString">A connection string with a plain-text password</param>
''' <returns>The connection string with the password encrypted.</returns>
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
''' <summary>
''' Decrypts a connection string password.
''' </summary>
''' <param name="ConnectionString">A connection string with a encrypted password</param>
''' <returns>The connection string with the password decrypted.</returns>
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,