Encryption: Return original value on error

This commit is contained in:
Jonathan Jenne 2021-05-28 10:57:50 +02:00
parent b1b4868010
commit 0e2ed68f9e

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,10 +32,10 @@ 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. ' Convert the plaintext string to a byte array.
Dim plaintextBytes() As Byte = Dim plaintextBytes() As Byte =
System.Text.Encoding.Unicode.GetBytes("!Didalog35452Heuchelheim=" & plaintext) System.Text.Encoding.Unicode.GetBytes(SALT_VALUE & plaintext)
' Create the stream. ' Create the stream.
Dim ms As New System.IO.MemoryStream Dim ms As New System.IO.MemoryStream
@ -49,35 +50,34 @@ Public Class EncryptionLegacy
' Convert the encrypted stream to a printable string. ' Convert the encrypted stream to a printable string.
Return Convert.ToBase64String(ms.ToArray) Return Convert.ToBase64String(ms.ToArray)
Catch ex As Exception
Return plaintext
End Try
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
Try
' Convert the encrypted text string to a byte array. ' 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. ' 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