From 0e2ed68f9e22b77937900700cc50575729a42a0f Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Fri, 28 May 2021 10:57:50 +0200 Subject: [PATCH] Encryption: Return original value on error --- Encryption/EncryptionLegacy.vb | 88 +++++++++++++++++----------------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/Encryption/EncryptionLegacy.vb b/Encryption/EncryptionLegacy.vb index 784cab60..582ee55c 100644 --- a/Encryption/EncryptionLegacy.vb +++ b/Encryption/EncryptionLegacy.vb @@ -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,53 +32,52 @@ Public Class EncryptionLegacy End Function Public Function EncryptData(ByVal plaintext As String) As String - - ' Convert the plaintext string to a byte array. - Dim plaintextBytes() As Byte = - System.Text.Encoding.Unicode.GetBytes("!Didalog35452Heuchelheim=" & 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) + 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 - ' Convert the encrypted text string to a byte array. - Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext) - - ' Create the stream. - Dim ms As New System.IO.MemoryStream - ' Create the decoder to write to the stream. - Dim decStream As New CryptoStream(ms, - 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=", "") - ' 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() + 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