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,53 +32,52 @@ 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.
Dim plaintextBytes() As Byte =
System.Text.Encoding.Unicode.GetBytes(SALT_VALUE & plaintext)
' Convert the plaintext string to a byte array. ' Create the stream.
Dim plaintextBytes() As Byte = Dim ms As New System.IO.MemoryStream
System.Text.Encoding.Unicode.GetBytes("!Didalog35452Heuchelheim=" & plaintext) ' Create the encoder to write to the stream.
Dim encStream As New CryptoStream(ms,
TripleDes.CreateEncryptor(),
System.Security.Cryptography.CryptoStreamMode.Write)
' Create the stream. ' Use the crypto stream to write the byte array to the stream.
Dim ms As New System.IO.MemoryStream encStream.Write(plaintextBytes, 0, plaintextBytes.Length)
' Create the encoder to write to the stream. encStream.FlushFinalBlock()
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. ' Convert the encrypted stream to a printable string.
encStream.Write(plaintextBytes, 0, plaintextBytes.Length) Return Convert.ToBase64String(ms.ToArray)
encStream.FlushFinalBlock() Catch ex As Exception
Return plaintext
' Convert the encrypted stream to a printable string. End Try
Return Convert.ToBase64String(ms.ToArray)
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
' Convert the encrypted text string to a byte array. Try
Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext) ' Convert the encrypted text string to a byte array.
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