7 Commits

Author SHA1 Message Date
Jonathan Jenne
824f5cfc10 Merge branch 'master' of http://dd-vmp07-com04:3000/AppStd/Monorepo 2021-06-07 13:40:27 +02:00
Jonathan Jenne
33cfb257b1 use new Encryption methods 2021-05-28 11:01:59 +02:00
Jonathan Jenne
3d30ab7309 Config: Remove DecryptConnectionStrings 2021-05-28 11:01:06 +02:00
Jonathan Jenne
5c9eb4bf1b Database: Version 2.2.0 2021-05-28 10:59:42 +02:00
Jonathan Jenne
258d412b9a Database: Add Encryption methods for connection string 2021-05-28 10:59:34 +02:00
Jonathan Jenne
aa6e211957 Encryption: Version 1.1.0 2021-05-28 10:58:40 +02:00
Jonathan Jenne
0e2ed68f9e Encryption: Return original value on error 2021-05-28 10:57:50 +02:00
9 changed files with 84 additions and 78 deletions

View File

@@ -5,6 +5,7 @@ Imports System.Data.SqlClient
Public Class EncryptionLegacy Public Class EncryptionLegacy
Private TripleDes As New TripleDESCryptoServiceProvider Private TripleDes As New TripleDESCryptoServiceProvider
Private DEFAULT_KEY As String = "!35452didalog=" Private DEFAULT_KEY As String = "!35452didalog="
Private SALT_VALUE As String = "!Didalog35452Heuchelheim="
Sub New() Sub New()
TripleDes.Key = TruncateHash(DEFAULT_KEY, TripleDes.KeySize \ 8) TripleDes.Key = TruncateHash(DEFAULT_KEY, TripleDes.KeySize \ 8)
@@ -31,53 +32,52 @@ Public Class EncryptionLegacy
End Function End Function
Public Function EncryptData(ByVal plaintext As String) As String Public Function EncryptData(ByVal plaintext As String) As String
Try
' Convert the plaintext string to a byte array.
Dim plaintextBytes() As Byte =
System.Text.Encoding.Unicode.GetBytes(SALT_VALUE & plaintext)
' Convert the plaintext string to a byte array. ' Create the stream.
Dim plaintextBytes() As Byte = Dim ms As New System.IO.MemoryStream
System.Text.Encoding.Unicode.GetBytes("!Didalog35452Heuchelheim=" & plaintext) ' Create the encoder to write to the stream.
Dim encStream As New CryptoStream(ms,
TripleDes.CreateEncryptor(),
System.Security.Cryptography.CryptoStreamMode.Write)
' Create the stream. ' Use the crypto stream to write the byte array to the stream.
Dim ms As New System.IO.MemoryStream encStream.Write(plaintextBytes, 0, plaintextBytes.Length)
' Create the encoder to write to the stream. encStream.FlushFinalBlock()
Dim encStream As New CryptoStream(ms,
TripleDes.CreateEncryptor(),
System.Security.Cryptography.CryptoStreamMode.Write)
' Use the crypto stream to write the byte array to the stream. ' Convert the encrypted stream to a printable string.
encStream.Write(plaintextBytes, 0, plaintextBytes.Length) Return Convert.ToBase64String(ms.ToArray)
encStream.FlushFinalBlock() Catch ex As Exception
Return plaintext
' Convert the encrypted stream to a printable string. End Try
Return Convert.ToBase64String(ms.ToArray)
End Function End Function
'Entschlüsselt die Zeichenfolge 'Entschlüsselt die Zeichenfolge
Public Function DecryptData(ByVal encryptedtext As String) As String Public Function DecryptData(ByVal EncryptedText As String) As String
' Convert the encrypted text string to a byte array. Try
Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext) ' Convert the encrypted text string to a byte array.
Dim oEncryptedBytes() As Byte = Convert.FromBase64String(EncryptedText)
' Create the stream. ' Create the stream.
Dim ms As New System.IO.MemoryStream Dim oMemoryStream As New System.IO.MemoryStream
' Create the decoder to write to the stream. ' Create the decoder to write to the stream.
Dim decStream As New CryptoStream(ms, Dim oCryptoStream As New CryptoStream(oMemoryStream,
TripleDes.CreateDecryptor(), TripleDes.CreateDecryptor(),
System.Security.Cryptography.CryptoStreamMode.Write) System.Security.Cryptography.CryptoStreamMode.Write)
' Use the crypto stream to write the byte array to the stream. ' Use the crypto stream to write the byte array to the stream.
decStream.Write(encryptedBytes, 0, encryptedBytes.Length) oCryptoStream.Write(oEncryptedBytes, 0, oEncryptedBytes.Length)
decStream.FlushFinalBlock() oCryptoStream.FlushFinalBlock()
Dim result = System.Text.Encoding.Unicode.GetString(ms.ToArray) Dim oResult = System.Text.Encoding.Unicode.GetString(oMemoryStream.ToArray)
result = result.Replace("!Didalog35452Heuchelheim=", "") oResult = oResult.Replace(SALT_VALUE, "")
' Convert the plaintext stream to a string. ' Convert the plaintext stream to a string.
Return result Return oResult
End Function Catch ex As Exception
Return EncryptedText
Public Function DecryptConnectionString(ConnectionString As String) As String End Try
Dim oBuilder As New SqlConnectionStringBuilder() With {.ConnectionString = ConnectionString}
Dim oDecryptedPassword = DecryptData(oBuilder.Password)
oBuilder.Password = oDecryptedPassword
Return oBuilder.ToString()
End Function End Function
End Class End Class

View File

@@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices
' indem Sie "*" wie unten gezeigt eingeben: ' indem Sie "*" wie unten gezeigt eingeben:
' <Assembly: AssemblyVersion("1.0.*")> ' <Assembly: AssemblyVersion("1.0.*")>
<Assembly: AssemblyVersion("1.0.0.0")> <Assembly: AssemblyVersion("1.1.0.0")>
<Assembly: AssemblyFileVersion("1.0.0.0")> <Assembly: AssemblyFileVersion("1.1.0.0")>

View File

@@ -74,7 +74,6 @@ Public Class frmMonitor
Try Try
LogConfig = New LogConfig(LogConfig.PathType.AppData, Nothing, Nothing, "Digital Data", "Monitor") LogConfig = New LogConfig(LogConfig.PathType.AppData, Nothing, Nothing, "Digital Data", "Monitor")
ConfigManager = New ConfigManager(Of Config)(LogConfig, Application.UserAppDataPath, Application.UserAppDataPath, Application.StartupPath) ConfigManager = New ConfigManager(Of Config)(LogConfig, Application.UserAppDataPath, Application.UserAppDataPath, Application.StartupPath)
ConfigManager.DecryptConnectionStrings()
Init(LogConfig) Init(LogConfig)
If ConfigManager.Config.ConnectionString = String.Empty Then If ConfigManager.Config.ConnectionString = String.Empty Then
@@ -82,6 +81,7 @@ Public Class frmMonitor
If oSQLConfig.ShowDialog() = DialogResult.OK Then If oSQLConfig.ShowDialog() = DialogResult.OK Then
ConfigManager.Config.ConnectionString = oSQLConfig.ConnectionString ConfigManager.Config.ConnectionString = oSQLConfig.ConnectionString
ConfigManager.Save() ConfigManager.Save()
Application.Restart()
Else Else
ShowErrorMessage("No Database configured. Application will close!") ShowErrorMessage("No Database configured. Application will close!")
Application.Exit() Application.Exit()
@@ -89,7 +89,8 @@ Public Class frmMonitor
End If End If
End If End If
Database = New MSSQLServer(LogConfig, ConfigManager.Config.ConnectionString) Dim oConnectionString = MSSQLServer.DecryptConnectionString(ConfigManager.Config.ConnectionString)
Database = New MSSQLServer(LogConfig, oConnectionString)
GridBuilder = New GridBuilder(New List(Of GridView) From {GridView1, GridView2, GridView3, GridView4}) GridBuilder = New GridBuilder(New List(Of GridView) From {GridView1, GridView2, GridView3, GridView4})
GridBuilder. GridBuilder.
WithDefaults(). WithDefaults().
@@ -133,7 +134,7 @@ Public Class frmMonitor
Private Function LoadGDPicture() As String Private Function LoadGDPicture() As String
Dim oSQL = "SELECT LICENSE FROM TBDD_3RD_PARTY_MODULES WHERE NAME = 'GDPICTURE'" Dim oSQL = "SELECT LICENSE FROM TBDD_3RD_PARTY_MODULES WHERE NAME = 'GDPICTURE'"
Return Database.GetScalarValue(oSQL).ToString() Return Database.GetScalarValue(oSQL)
End Function End Function
Private Sub buttonSearch_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles buttonSearch.ItemClick Private Sub buttonSearch_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles buttonSearch.ItemClick

View File

@@ -116,12 +116,8 @@ Public Class ClassInit
End Sub End Sub
Private Sub CheckConnectivity(MyApplication As My.MyApplication) Private Sub CheckConnectivity(MyApplication As My.MyApplication)
Dim oCrypt As New EncryptionLegacy() Dim oConnectionString = MSSQLServer.DecryptConnectionString(My.SystemConfig.ConnectionString)
Dim oBuilder = My.SystemConfig.GetConnectionStringBuilder(My.SystemConfig.ConnectionString) My.Database = New MSSQLServer(My.LogConfig, oConnectionString)
oBuilder.Password = oCrypt.DecryptData(oBuilder.Password)
Dim oDecryptedConnectionString = oBuilder.ToString
My.Database = New MSSQLServer(My.LogConfig, oDecryptedConnectionString)
If My.Database.DBInitialized = False Then If My.Database.DBInitialized = False Then
_Logger.Warn("Could not initialize DD_ECM-Database!") _Logger.Warn("Could not initialize DD_ECM-Database!")

View File

@@ -166,27 +166,6 @@ Public Class ConfigManager(Of T)
End Try End Try
End Function End Function
Public Function DecryptConnectionStrings() As Boolean
Try
Dim oType As Type = GetType(T)
Dim oProperties = oType.GetProperties()
Dim oEncryption = New EncryptionLegacy()
For Each oProperty In oProperties
If Attribute.IsDefined(oProperty, GetType(ConnectionStringAttribute)) Then
Dim oValue = oProperty.GetValue(_Config, Nothing)
Dim oDecryptedValue = oEncryption.DecryptConnectionString(oValue)
oProperty.SetValue(_Config, oDecryptedValue, Nothing)
End If
Next
Return True
Catch ex As Exception
_Logger.Error(ex)
Return False
End Try
End Function
''' <summary> ''' <summary>
''' Copies all properties from Source to Target, except those who have an attribute ''' Copies all properties from Source to Target, except those who have an attribute
''' listed in ExcludedAttributeTypes ''' listed in ExcludedAttributeTypes

View File

@@ -1,6 +1,7 @@
Imports System.ComponentModel Imports System.ComponentModel
Imports System.Data.Common Imports System.Data.Common
Imports System.Data.SqlClient Imports System.Data.SqlClient
Imports DigitalData.Modules.Encryption
Imports DigitalData.Modules.Logging Imports DigitalData.Modules.Logging
Public Class MSSQLServer Public Class MSSQLServer
@@ -52,6 +53,34 @@ Public Class MSSQLServer
End Try End Try
End Sub 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 Public Function GetConnectionString(Server As String, Database As String, UserId As String, Password As String) As String
Dim oConnectionStringBuilder As New SqlConnectionStringBuilder() With { Dim oConnectionStringBuilder As New SqlConnectionStringBuilder() With {
.DataSource = Server, .DataSource = Server,

View File

@@ -139,6 +139,10 @@
<None Include="packages.config" /> <None Include="packages.config" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Encryption\Encryption.vbproj">
<Project>{8a8f20fc-c46e-41ac-bee7-218366cfff99}</Project>
<Name>Encryption</Name>
</ProjectReference>
<ProjectReference Include="..\Modules.Logging\Logging.vbproj"> <ProjectReference Include="..\Modules.Logging\Logging.vbproj">
<Project>{903b2d7d-3b80-4be9-8713-7447b704e1b0}</Project> <Project>{903b2d7d-3b80-4be9-8713-7447b704e1b0}</Project>
<Name>Logging</Name> <Name>Logging</Name>

View File

@@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices
' übernehmen, indem Sie "*" eingeben: ' übernehmen, indem Sie "*" eingeben:
' <Assembly: AssemblyVersion("1.0.*")> ' <Assembly: AssemblyVersion("1.0.*")>
<Assembly: AssemblyVersion("2.1.4.0")> <Assembly: AssemblyVersion("2.2.0.0")>
<Assembly: AssemblyFileVersion("2.1.4.0")> <Assembly: AssemblyFileVersion("2.2.0.0")>

View File

@@ -129,11 +129,8 @@ Public Class frmSQLConfig
Dim oResult = MessageBox.Show(STRING_CONNECTION_SUCCESSFUL, Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) Dim oResult = MessageBox.Show(STRING_CONNECTION_SUCCESSFUL, Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If oResult = DialogResult.Yes Then If oResult = DialogResult.Yes Then
Dim oCrypt As New EncryptionLegacy() Dim oPlainTextConnectionString = $"Server={txtServerName.Text};Database={cmbDatabase.Text};User Id={txtUserName.Text};Password={txtPassword.Text};"
Dim oEncryptedPassword = oCrypt.EncryptData(txtPassword.Text) ConnectionString = MSSQLServer.EncryptConnectionString(oPlainTextConnectionString)
Dim oEncryptedConnectionString = $"Server={txtServerName.Text};Database={cmbDatabase.Text};User Id={txtUserName.Text};Password={oEncryptedPassword};"
ConnectionString = oEncryptedConnectionString
Close() Close()
End If End If
Catch ex As Exception Catch ex As Exception