fix potential memory leak with converting byte arrays to bitmaps

This commit is contained in:
Jonathan Jenne
2020-11-06 11:12:45 +01:00
parent 18482491ab
commit cf7a55203b
4 changed files with 33 additions and 15 deletions

View File

@@ -14,16 +14,14 @@ Public Class ClassEncryption
Dim sha1 As New SHA1CryptoServiceProvider
' Hash the key.
Dim keyBytes() As Byte =
System.Text.Encoding.Unicode.GetBytes(key)
Dim keyBytes() As Byte = Text.Encoding.Unicode.GetBytes(key)
Dim hash() As Byte = sha1.ComputeHash(keyBytes)
' Truncate or pad the hash.
ReDim Preserve hash(length - 1)
Return hash
End Function
Public Function EncryptData(
ByVal plaintext As String) As String
Public Function EncryptData(ByVal plaintext As String) As String
' Convert the plaintext string to a byte array.
Dim plaintextBytes() As Byte =
@@ -40,12 +38,19 @@ Public Class ClassEncryption
encStream.Write(plaintextBytes, 0, plaintextBytes.Length)
encStream.FlushFinalBlock()
Dim oPrintableString = Convert.ToBase64String(ms.ToArray)
Try
ms.Dispose()
Catch ex As Exception
LOGGER.Error(ex)
End Try
' Convert the encrypted stream to a printable string.
Return Convert.ToBase64String(ms.ToArray)
Return oPrintableString
End Function
'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.
Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)
@@ -60,9 +65,17 @@ Public Class ClassEncryption
' Use the crypto stream to write the byte array to the stream.
decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
decStream.FlushFinalBlock()
' Convert the plaintext stream to a string.
Dim result = System.Text.Encoding.Unicode.GetString(ms.ToArray)
result = result.Replace("!Didalog35452Heuchelheim=", "")
' Convert the plaintext stream to a string.
Try
ms.Dispose()
Catch ex As Exception
LOGGER.Error(ex)
End Try
Return result
End Function
End Class