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
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