From a4a3dc4536e54c27e825f9610be81b891ca6af26 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Fri, 1 Sep 2023 13:41:40 +0200 Subject: [PATCH] Restructure Base --- Base/Base.vbproj | 29 +++--- Base/{ => Base}/BaseClass.vb | 0 Base/{ => Base}/BaseUtils.vb | 0 Base/ECM/ECM.vb | 7 -- Base/Encryption/Compression.vb | 70 +++++++++++++ Base/Encryption/Encryption.vb | 148 ++++++++++++++++++++++++++++ Base/Encryption/EncryptionLegacy.vb | 85 ++++++++++++++++ Base/IDB/Attributes.vb | 15 --- Base/IDB/Database.vb | 11 --- Base/IDB/FileStore.vb | 20 ---- Base/My Project/AssemblyInfo.vb | 6 +- Base/{ => Static}/DatabaseEx.vb | 0 Base/{ => Static}/FileEx.vb | 0 Base/{ => Static}/GraphicsEx.vb | 0 Base/{ => Static}/LanguageEx.vb | 0 Base/{ => Static}/MimeEx.vb | 0 Base/{ => Static}/ObjectEx.vb | 0 Base/{ => Static}/ScreenEx.vb | 0 Base/{ => Static}/StringEx.vb | 4 +- Base/{ => Static}/WindowsEx.vb | 0 20 files changed, 322 insertions(+), 73 deletions(-) rename Base/{ => Base}/BaseClass.vb (100%) rename Base/{ => Base}/BaseUtils.vb (100%) delete mode 100644 Base/ECM/ECM.vb create mode 100644 Base/Encryption/Compression.vb create mode 100644 Base/Encryption/Encryption.vb create mode 100644 Base/Encryption/EncryptionLegacy.vb delete mode 100644 Base/IDB/Attributes.vb delete mode 100644 Base/IDB/Database.vb delete mode 100644 Base/IDB/FileStore.vb rename Base/{ => Static}/DatabaseEx.vb (100%) rename Base/{ => Static}/FileEx.vb (100%) rename Base/{ => Static}/GraphicsEx.vb (100%) rename Base/{ => Static}/LanguageEx.vb (100%) rename Base/{ => Static}/MimeEx.vb (100%) rename Base/{ => Static}/ObjectEx.vb (100%) rename Base/{ => Static}/ScreenEx.vb (100%) rename Base/{ => Static}/StringEx.vb (99%) rename Base/{ => Static}/WindowsEx.vb (100%) diff --git a/Base/Base.vbproj b/Base/Base.vbproj index 0271f4ea..99819b74 100644 --- a/Base/Base.vbproj +++ b/Base/Base.vbproj @@ -77,21 +77,20 @@ - - - - - - + + + + + + + + - + - - - - - - + + + True @@ -108,8 +107,8 @@ Settings.settings True - - + + diff --git a/Base/BaseClass.vb b/Base/Base/BaseClass.vb similarity index 100% rename from Base/BaseClass.vb rename to Base/Base/BaseClass.vb diff --git a/Base/BaseUtils.vb b/Base/Base/BaseUtils.vb similarity index 100% rename from Base/BaseUtils.vb rename to Base/Base/BaseUtils.vb diff --git a/Base/ECM/ECM.vb b/Base/ECM/ECM.vb deleted file mode 100644 index bf04d74b..00000000 --- a/Base/ECM/ECM.vb +++ /dev/null @@ -1,7 +0,0 @@ -Public Class ECM - Public Enum Product - ProcessManager - GlobalIndexer - ClipboardWatcher - End Enum -End Class diff --git a/Base/Encryption/Compression.vb b/Base/Encryption/Compression.vb new file mode 100644 index 00000000..a1d5027e --- /dev/null +++ b/Base/Encryption/Compression.vb @@ -0,0 +1,70 @@ +Imports System.IO +Imports System.IO.Compression +Imports DigitalData.Modules.Logging + +Public Class Compression + Private ReadOnly _logger As Logger + + Public Sub New(LogConfig As LogConfig) + _logger = LogConfig.GetLogger() + End Sub + + Public Async Function CompressAsync(data As Byte()) As Task(Of Byte()) + Return Await Task.Run(Function() As Byte() + Return Compress(data) + End Function) + End Function + + Public Function Compress(data As Byte()) As Byte() + Try + ' ByteArray in Stream umwandeln + Using originalStream As New MemoryStream(data) + ' Ziel Stream erstellen + Using compressedStream As New MemoryStream() + ' Gzip-Stream erstellen, der alle Daten komprimiert und zu compressedStream durchleitet + ' + ' > MemoryStream > GzipStream > MemoryStream + ' originalStream --> compressionStream --> compressedFileStream + ' + Using compressionStream As New GZipStream(compressedStream, CompressionMode.Compress) + originalStream.CopyTo(compressionStream) + compressionStream.Close() + Return compressedStream.ToArray() + End Using + End Using + End Using + Catch ex As Exception + _logger.Error(ex) + Throw ex + End Try + End Function + + Public Async Function DecompressAsync(data As Byte()) As Task(Of Byte()) + Return Await Task.Run(Function() As Byte() + Return Decompress(data) + End Function) + End Function + + Public Function Decompress(data As Byte()) As Byte() + Try + ' ByteArray in Stream umwandeln + Using compressedStream As New MemoryStream(data) + ' Ziel Stream erstellen + Using decompressedStream As New MemoryStream() + ' Gzip-Stream erstellen, der alle Daten komprimiert und zu compressedStream durchleitet + ' + ' > MemoryStream > GzipStream > MemoryStream + ' compressedStream --> decompressionStream --> decompressedStream + ' + Using decompressionStream As New GZipStream(compressedStream, CompressionMode.Decompress) + decompressionStream.CopyTo(decompressedStream) + Return decompressedStream.ToArray() + End Using + End Using + End Using + Catch ex As Exception + _logger.Error(ex) + Throw ex + End Try + End Function +End Class diff --git a/Base/Encryption/Encryption.vb b/Base/Encryption/Encryption.vb new file mode 100644 index 00000000..1ec620de --- /dev/null +++ b/Base/Encryption/Encryption.vb @@ -0,0 +1,148 @@ +Imports System.IO +Imports System.Security.Cryptography +Imports System.Text.Encoding +Imports DigitalData.Modules.Logging + +''' +''' https://stackoverflow.com/questions/10168240/encrypting-decrypting-a-string-in-c-sharp +''' +Public Class Encryption + ' This constant is used to determine the keysize of the encryption algorithm in bits. + ' We divide this by 8 within the code below to get the equivalent number of bytes. + Private Const KEY_SIZE As Integer = 256 + ' This constant determines the number of iterations for the password bytes generation function. + Private Const DERIVATION_ITERATIONS As Integer = 1000 + Private Const BLOCK_SIZE As Integer = 256 + + Private _paddingMode As PaddingMode = PaddingMode.Zeros + Private _cipherMode As CipherMode = CipherMode.CBC + + Private ReadOnly _password As String + Private _logger As Logger + + Public Sub New(LogConfig As LogConfig, Password As String) + _logger = LogConfig.GetLogger() + + If IsNothing(Password) Then + Throw New ArgumentNullException("Password") + End If + + _password = Password + End Sub + + Public Async Function EncryptAsync(PlainTextBytes As Byte()) As Task(Of Byte()) + Return Await Task.Run(Function() As Byte() + Return Encrypt(PlainTextBytes) + End Function) + End Function + + Public Function Encrypt(PlainText As String) As String + Try + Dim oBytes As Byte() = UTF8.GetBytes(PlainText) + Dim oEncrypted As Byte() = Encrypt(oBytes) + Return UTF8.GetString(oEncrypted) + Catch ex As Exception + _logger.Error(ex) + Throw ex + End Try + End Function + + Public Function Encrypt(PlainTextBytes As Byte()) As Byte() + Try + ' Salt and IV is randomly generated each time, but is preprended to encrypted cipher text + ' so that the same Salt and IV values can be used when decrypting. + Dim oSaltStringBytes = Generate256BitsOfRandomEntropy() + Dim oIvStringBytes = Generate256BitsOfRandomEntropy() + Using oPassword = New Rfc2898DeriveBytes(_password, oSaltStringBytes, DERIVATION_ITERATIONS) + Dim oKeyBytes = oPassword.GetBytes(KEY_SIZE / 8) + Using oSymmetricKey = New RijndaelManaged() + oSymmetricKey.BlockSize = BLOCK_SIZE + oSymmetricKey.Mode = _cipherMode + oSymmetricKey.Padding = _paddingMode + + Using oEncryptor = oSymmetricKey.CreateEncryptor(oKeyBytes, oIvStringBytes) + Using oMemoryStream = New MemoryStream() + Using oCryptoStream = New CryptoStream(oMemoryStream, oEncryptor, CryptoStreamMode.Write) + oCryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length) + oCryptoStream.FlushFinalBlock() + ' Create the final bytes as a concatenation of the random salt bytes, the random iv bytes and the cipher bytes. + Dim oCipherTextBytes = oSaltStringBytes + oCipherTextBytes = oCipherTextBytes.Concat(oIvStringBytes).ToArray() + oCipherTextBytes = oCipherTextBytes.Concat(oMemoryStream.ToArray()).ToArray() + oMemoryStream.Close() + oCryptoStream.Close() + Return oCipherTextBytes + End Using + End Using + End Using + End Using + End Using + Catch ex As Exception + _logger.Error(ex) + Throw ex + End Try + End Function + + Public Async Function DecryptAsync(CipherTextBytesWithSaltAndIv As Byte()) As Task(Of Byte()) + Return Await Task.Run(Function() As Byte() + Return Decrypt(CipherTextBytesWithSaltAndIv) + End Function) + End Function + + Public Function Decrypt(CipherTextPlainWithSaltAndIv As String) As String + Try + Dim oBytes As Byte() = UTF8.GetBytes(CipherTextPlainWithSaltAndIv) + Dim oDecrypted As Byte() = Decrypt(oBytes) + Return UTF8.GetString(oDecrypted) + Catch ex As Exception + _logger.Error(ex) + Throw ex + End Try + End Function + + Public Function Decrypt(CipherTextBytesWithSaltAndIv As Byte()) As Byte() + Try + ' Get the complete stream of bytes that represent: + ' [32 bytes of Salt] + [32 bytes of IV] + [n bytes of CipherText] + ' Get the saltbytes by extracting the first 32 bytes from the supplied cipherText bytes. + Dim oSaltStringBytes = CipherTextBytesWithSaltAndIv.Take(KEY_SIZE / 8).ToArray() + ' Get the IV bytes by extracting the next 32 bytes from the supplied cipherText bytes. + Dim oIvStringBytes = CipherTextBytesWithSaltAndIv.Skip(KEY_SIZE / 8).Take(KEY_SIZE / 8).ToArray() + ' Get the actual cipher text bytes by removing the first 64 bytes from the cipherText string. + Dim oCipherTextBytes = CipherTextBytesWithSaltAndIv.Skip((KEY_SIZE / 8) * 2).Take(CipherTextBytesWithSaltAndIv.Length - ((KEY_SIZE / 8) * 2)).ToArray() + + Using oPassword = New Rfc2898DeriveBytes(_password, oSaltStringBytes, DERIVATION_ITERATIONS) + Dim oKeyBytes = oPassword.GetBytes(KEY_SIZE / 8) + Using oSymmetricKey = New RijndaelManaged() + oSymmetricKey.BlockSize = BLOCK_SIZE + oSymmetricKey.Mode = _cipherMode + oSymmetricKey.Padding = _paddingMode + Using oDecryptor = oSymmetricKey.CreateDecryptor(oKeyBytes, oIvStringBytes) + Using oMemoryStream = New MemoryStream(oCipherTextBytes) + Using oCryptoStream = New CryptoStream(oMemoryStream, oDecryptor, CryptoStreamMode.Read) + Dim oPlainTextBytes = New Byte(oCipherTextBytes.Length - 1) {} + Dim oDecryptedByteCount = oCryptoStream.Read(oPlainTextBytes, 0, oPlainTextBytes.Length) + oMemoryStream.Close() + oCryptoStream.Close() + Return oPlainTextBytes + End Using + End Using + End Using + End Using + End Using + Catch ex As Exception + _logger.Error(ex) + Throw ex + End Try + End Function + + Private Shared Function Generate256BitsOfRandomEntropy() As Byte() + Dim oRandomBytes = New Byte(31) {} + ' 32 Bytes will give us 256 bits. + Using oRNGCsp = New RNGCryptoServiceProvider() + ' Fill the array with cryptographically secure random bytes. + oRNGCsp.GetBytes(oRandomBytes) + End Using + Return oRandomBytes + End Function +End Class diff --git a/Base/Encryption/EncryptionLegacy.vb b/Base/Encryption/EncryptionLegacy.vb new file mode 100644 index 00000000..bb7640a4 --- /dev/null +++ b/Base/Encryption/EncryptionLegacy.vb @@ -0,0 +1,85 @@ +Imports System.Security.Cryptography +Imports System.Data +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) + TripleDes.IV = TruncateHash("", TripleDes.BlockSize \ 8) + End Sub + + Sub New(key As String) + ' Initialize the crypto provider. + TripleDes.Key = TruncateHash(key, TripleDes.KeySize \ 8) + TripleDes.IV = TruncateHash("", TripleDes.BlockSize \ 8) + End Sub + + Private Function TruncateHash(ByVal key As String, ByVal length As Integer) As Byte() + Dim sha1 As New SHA1CryptoServiceProvider + + ' Hash the key. + Dim keyBytes() As Byte = + System.Text.Encoding.Unicode.GetBytes(key) + Dim hash() As Byte = sha1.ComputeHash(keyBytes) + + ' Truncate or pad the hash. + ReDim Preserve hash(length - 1) + Return hash + 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(SALT_VALUE & plaintext) + + ' Create the stream. + Dim ms As New System.IO.MemoryStream + ' Create the encoder to write to the stream. + 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. + encStream.Write(plaintextBytes, 0, plaintextBytes.Length) + encStream.FlushFinalBlock() + + ' 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 + Try + ' Convert the encrypted text string to a byte array. + Dim oEncryptedBytes() As Byte = Convert.FromBase64String(EncryptedText) + + ' Create the stream. + Dim oMemoryStream As New System.IO.MemoryStream + ' Create the decoder to write to the stream. + 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. + 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 oResult + Catch ex As Exception + Return EncryptedText + End Try + End Function +End Class + diff --git a/Base/IDB/Attributes.vb b/Base/IDB/Attributes.vb deleted file mode 100644 index bf7bd740..00000000 --- a/Base/IDB/Attributes.vb +++ /dev/null @@ -1,15 +0,0 @@ -Namespace IDB - Public Class Attributes - Public Const ATTRIBUTE_DOCTYPE = "Doctype" - Public Const ATTRIBUTE_DYNAMIC_FOLDER = "Dynamic Folder" - - Public Const ATTRIBUTE_ORIGIN_FILENAME = "OriginFileName" - Public Const ATTRIBUTE_ORIGIN_CHANGED = "OriginChangedDatetime" - Public Const ATTRIBUTE_ORIGIN_CREATED = "OriginCreationDatetime" - - Public Const ATTRIBUTE_DISPLAY_FILENAME = "DisplayFileName" - Public Const ATTRIBUTE_DISPLAY_FILENAME1 = "DisplayFileName1" - - End Class - -End Namespace \ No newline at end of file diff --git a/Base/IDB/Database.vb b/Base/IDB/Database.vb deleted file mode 100644 index 8afd0a7e..00000000 --- a/Base/IDB/Database.vb +++ /dev/null @@ -1,11 +0,0 @@ - -Namespace IDB - Public Class Database - Public Enum NamedDatabase - ECM - IDB - End Enum - End Class - -End Namespace - diff --git a/Base/IDB/FileStore.vb b/Base/IDB/FileStore.vb deleted file mode 100644 index f1dc29e0..00000000 --- a/Base/IDB/FileStore.vb +++ /dev/null @@ -1,20 +0,0 @@ -Namespace IDB - Public Class FileStore - Public Const FILE_STORE_INVALID_OBEJCT_ID = 0 - - Public Const FILE_CHANGED_QUESTION = "QUESTION VERSION" - Public Const FILE_CHANGED_OVERWRITE = "AUTO REPLACE" - Public Const FILE_CHANGED_VERSION = "AUTO VERSION" - - Public Const OBJECT_STATE_FILE_ADDED = "File added" - Public Const OBJECT_STATE_FILE_VERSIONED = "File versioned" - Public Const OBJECT_STATE_FILE_CHANGED = "File changed" - Public Const OBJECT_STATE_FILE_OPENED = "File opened" - Public Const OBJECT_STATE_FILE_DELETED = "File deleted" - Public Const OBJECT_STATE_METADATA_CHANGED = "Metadata changed" - Public Const OBJECT_STATE_ATTRIBUTEVALUE_DELETED = "Attributevalue deleted" - Public Const OBJECT_STATE_FILE_CHECKED_OUT = "File Checked Out" - Public Const OBJECT_STATE_FILE_CHECKED_IN = "File Checked In" - End Class - -End Namespace \ No newline at end of file diff --git a/Base/My Project/AssemblyInfo.vb b/Base/My Project/AssemblyInfo.vb index 8acce9cb..7e120f51 100644 --- a/Base/My Project/AssemblyInfo.vb +++ b/Base/My Project/AssemblyInfo.vb @@ -13,7 +13,7 @@ Imports System.Runtime.InteropServices - + @@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices ' indem Sie "*" wie unten gezeigt eingeben: ' - - + + diff --git a/Base/DatabaseEx.vb b/Base/Static/DatabaseEx.vb similarity index 100% rename from Base/DatabaseEx.vb rename to Base/Static/DatabaseEx.vb diff --git a/Base/FileEx.vb b/Base/Static/FileEx.vb similarity index 100% rename from Base/FileEx.vb rename to Base/Static/FileEx.vb diff --git a/Base/GraphicsEx.vb b/Base/Static/GraphicsEx.vb similarity index 100% rename from Base/GraphicsEx.vb rename to Base/Static/GraphicsEx.vb diff --git a/Base/LanguageEx.vb b/Base/Static/LanguageEx.vb similarity index 100% rename from Base/LanguageEx.vb rename to Base/Static/LanguageEx.vb diff --git a/Base/MimeEx.vb b/Base/Static/MimeEx.vb similarity index 100% rename from Base/MimeEx.vb rename to Base/Static/MimeEx.vb diff --git a/Base/ObjectEx.vb b/Base/Static/ObjectEx.vb similarity index 100% rename from Base/ObjectEx.vb rename to Base/Static/ObjectEx.vb diff --git a/Base/ScreenEx.vb b/Base/Static/ScreenEx.vb similarity index 100% rename from Base/ScreenEx.vb rename to Base/Static/ScreenEx.vb diff --git a/Base/StringEx.vb b/Base/Static/StringEx.vb similarity index 99% rename from Base/StringEx.vb rename to Base/Static/StringEx.vb index 4f7a6e56..d10e422a 100644 --- a/Base/StringEx.vb +++ b/Base/Static/StringEx.vb @@ -12,7 +12,7 @@ Public Class StringEx ''' And consecutive hyphens. ''' ''' The string to convert - Public Shared Function ConvertTextToSlug(ByVal s As String) As String + Public Shared Function ConvertTextToSlug(s As String) As String Dim oBuilder As New StringBuilder() Dim oWasHyphen As Boolean = True @@ -27,7 +27,7 @@ Public Class StringEx oWasHyphen = True ElseIf Char.IsWhiteSpace(oChar) AndAlso Not oWasHyphen Then - oBuilder.Append("-"c) + oBuilder.Append("_"c) oWasHyphen = True End If Next diff --git a/Base/WindowsEx.vb b/Base/Static/WindowsEx.vb similarity index 100% rename from Base/WindowsEx.vb rename to Base/Static/WindowsEx.vb