Good day to all,

I am creating a Class with MD5 encryption/decryption functions to encrypt/decrypt a user password which will be stored in a txt file.

https://www.youtube.com/watch?v=jhkCoeshpOA

Encrypt function seems to be working, but the decrypt function is having exceptions.

The program will first read the encrypted password from text file, use Decrypt function to decrypt with a keyword.


The Encryption Class
Code:
Imports System.Security.Cryptography
Imports System.Text

Public Class encryption

    Dim DES As New TripleDESCryptoServiceProvider
    Dim MD5 As New MD5CryptoServiceProvider

    Public Function MD5Hash(value As String) As Byte()

        Return MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(value))
    End Function

    Public Function Encrypt(StringInput As String, key As String) As String

        DES.Key = MD5Hash(key)
        DES.Mode = CipherMode.ECB

        Dim buffer As Byte() = ASCIIEncoding.ASCII.GetBytes(StringInput)

        Return Convert.ToBase64String(DES.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length))
    End Function

    Public Function Decrypt(EncryptedString As String, key As String) As String

        DES.Key = MD5Hash(key)
        DES.Mode = CipherMode.ECB

        Dim Buffer As Byte() = Convert.FromBase64String(EncryptedString)

        Return ASCIIEncoding.ASCII.GetString(DES.CreateDecryptor().TransformFinalBlock(Buffer, 0, Buffer.Length))
    End Function

End Class

The Login Form
Code:
Public Class AdminLoginForm

    Private Sub AdminLoginForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox1.Focus()

        'Read user.txt and load values into Settings
        Dim FileToString As String = ""     'grab 1 line from file
        Dim settingString(3) As String      'Store Substring from file to temp String()
        Dim i As Integer = 0                'Array index
        Dim filePath As String = ".\config\user.txt"
        Dim filenum As Integer

        filenum = FreeFile()

        'Read from File
        Try
            FileOpen(filenum, filePath, OpenMode.Input)
            While Not EOF(filenum)
                FileToString = LineInput(filenum)               
                settingString(i) = FileToString
                i += 1
            End While

            FileClose(filenum)
        Catch ex As Exception
            MessageBox.Show("File Error Occurred!" + vbCrLf + ex.ToString)
        End Try
        
        'Decrpyt Password
        Dim decryptedPW As String = ""
        Dim objDecrypt As encryption = New encryption
        decryptedPW = objDecrypt.Decrypt(settingString(1), "EVC")

        
        My.Settings.Username = settingString(0)
        My.Settings.Password = decryptedPW
        My.Settings.Save()

    End Sub
...

When the Form load, and Access Violation Error occurs.
Code:
A first chance exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll
The program '[13260] EVC_Kiosk_v1.1.vshost.exe: Managed (v4.0.30319)' has exited with code -1073741819 (0xc0000005) 'Access violation'.
I read somewhere that Access Violation might be due to bad pointer, but I am not sure if that's the case here.
Any help?