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

View File

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

View File

@@ -74,7 +74,6 @@ Public Class frmMonitor
Try
LogConfig = New LogConfig(LogConfig.PathType.AppData, Nothing, Nothing, "Digital Data", "Monitor")
ConfigManager = New ConfigManager(Of Config)(LogConfig, Application.UserAppDataPath, Application.UserAppDataPath, Application.StartupPath)
ConfigManager.DecryptConnectionStrings()
Init(LogConfig)
If ConfigManager.Config.ConnectionString = String.Empty Then
@@ -82,6 +81,7 @@ Public Class frmMonitor
If oSQLConfig.ShowDialog() = DialogResult.OK Then
ConfigManager.Config.ConnectionString = oSQLConfig.ConnectionString
ConfigManager.Save()
Application.Restart()
Else
ShowErrorMessage("No Database configured. Application will close!")
Application.Exit()
@@ -89,7 +89,8 @@ Public Class frmMonitor
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.
WithDefaults().
@@ -133,7 +134,7 @@ Public Class frmMonitor
Private Function LoadGDPicture() As String
Dim oSQL = "SELECT LICENSE FROM TBDD_3RD_PARTY_MODULES WHERE NAME = 'GDPICTURE'"
Return Database.GetScalarValue(oSQL).ToString()
Return Database.GetScalarValue(oSQL)
End Function
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
Private Sub CheckConnectivity(MyApplication As My.MyApplication)
Dim oCrypt As New EncryptionLegacy()
Dim oBuilder = My.SystemConfig.GetConnectionStringBuilder(My.SystemConfig.ConnectionString)
oBuilder.Password = oCrypt.DecryptData(oBuilder.Password)
Dim oDecryptedConnectionString = oBuilder.ToString
My.Database = New MSSQLServer(My.LogConfig, oDecryptedConnectionString)
Dim oConnectionString = MSSQLServer.DecryptConnectionString(My.SystemConfig.ConnectionString)
My.Database = New MSSQLServer(My.LogConfig, oConnectionString)
If My.Database.DBInitialized = False Then
_Logger.Warn("Could not initialize DD_ECM-Database!")

View File

@@ -166,27 +166,6 @@ Public Class ConfigManager(Of T)
End Try
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>
''' Copies all properties from Source to Target, except those who have an attribute
''' listed in ExcludedAttributeTypes

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,

View File

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

View File

@@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices
' übernehmen, indem Sie "*" eingeben:
' <Assembly: AssemblyVersion("1.0.*")>
<Assembly: AssemblyVersion("2.1.4.0")>
<Assembly: AssemblyFileVersion("2.1.4.0")>
<Assembly: AssemblyVersion("2.2.0.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)
If oResult = DialogResult.Yes Then
Dim oCrypt As New EncryptionLegacy()
Dim oEncryptedPassword = oCrypt.EncryptData(txtPassword.Text)
Dim oEncryptedConnectionString = $"Server={txtServerName.Text};Database={cmbDatabase.Text};User Id={txtUserName.Text};Password={oEncryptedPassword};"
ConnectionString = oEncryptedConnectionString
Dim oPlainTextConnectionString = $"Server={txtServerName.Text};Database={cmbDatabase.Text};User Id={txtUserName.Text};Password={txtPassword.Text};"
ConnectionString = MSSQLServer.EncryptConnectionString(oPlainTextConnectionString)
Close()
End If
Catch ex As Exception