Encryption: Return original value on error
This commit is contained in:
parent
b1b4868010
commit
0e2ed68f9e
@ -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
|
||||
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.
|
||||
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)
|
||||
|
||||
' 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()
|
||||
|
||||
' 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)
|
||||
' 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)
|
||||
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 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)
|
||||
' 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.
|
||||
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()
|
||||
' 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
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user