Imports System.Security.Cryptography
Imports System.IO
Imports System.Text
Public Class PWM
    Private testfile As String = Application.StartupPath & "\bin\data.txt"
    Dim pool As String = ""
    Dim lock As String
    Dim password As String = "test"


    Private Sub PWM_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ColumnDomain.Width = My.Settings.ColumnWidthDomain
        ColumnUsername.Width = My.Settings.ColumnWidthUsername
        ColumnPassword.Width = My.Settings.ColumnWidthPassword
        ColumnEMail.Width = My.Settings.ColumnWidthEMail
        ColumnToken.Width = My.Settings.ColumnWidthToken
        LabelLastTimeEditedValue.Text = My.Settings.LastTimeEditedValue

        Me.Text = "Password Manager - ZKDev | Currect user: " & System.Environment.MachineName

        ListView1.Items.Clear()
        Dim myCoolFileLines() As String = IO.File.ReadAllLines(testfile)
        For Each line As String In myCoolFileLines
            Dim lineArray() As String = line.Split("#")
            Dim newItem As New ListViewItem(lineArray(0))
            newItem.SubItems.Add(lineArray(1))
            newItem.SubItems.Add(lineArray(2))
            newItem.SubItems.Add(lineArray(3))
            newItem.SubItems.Add(lineArray(4))
            ListView1.Items.Add(newItem)
        Next
    End Sub

    Private Sub ButtonAddData_Click(sender As Object, e As EventArgs) Handles ButtonAddData.Click
        AddData.ShowDialog(Me)
    End Sub

    Private Sub ButtonRemoveSelected_Click(sender As Object, e As EventArgs) Handles ButtonRemoveSelected.Click
        If ListView1.SelectedItems.Count = 0 Then
            MsgBox("You have to choose an item", MsgBoxStyle.Critical)
        Else
            For Each item As ListViewItem In ListView1.SelectedItems
                ListView1.Items.Remove(item)
                My.Settings.LastTimeEditedValue = System.DateTime.Now
                My.Settings.Save()
                My.Settings.Reload()
                MsgBox("Your data has been successfully removed")
            Next
        End If
    End Sub

    Private Sub ButtonSaveData_Click(sender As Object, e As EventArgs) Handles ButtonSaveData.Click
        My.Settings.ColumnWidthDomain = ColumnDomain.Width
        My.Settings.ColumnWidthUsername = ColumnUsername.Width
        My.Settings.ColumnWidthPassword = ColumnPassword.Width
        My.Settings.ColumnWidthEMail = ColumnEMail.Width
        My.Settings.ColumnWidthToken = ColumnToken.Width
        My.Settings.Save()
        My.Settings.Reload()

        Dim myWriter As New IO.StreamWriter(testfile)
        For Each myItem As ListViewItem In ListView1.Items
            myWriter.WriteLine(lock = myItem.Text & "#" & myItem.SubItems(1).Text & "#" & myItem.SubItems(2).Text & "#" & myItem.SubItems(3).Text & "#" & myItem.SubItems(4).Text)
            lock = TripleDESEncryptMessage(lock, Password)
        Next
        myWriter.Close()
        My.Settings.LastTimeEditedValue = System.DateTime.Now
        My.Settings.Save()
        My.Settings.Reload()
        MsgBox("Your data has been successfully saved")
    End Sub

    Private Sub ButtonShutdown_Click(sender As Object, e As EventArgs) Handles ButtonShutdown.Click
        Me.Close()
        authpanel.Enabled = True
    End Sub

    Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
        ListView1.Visible = False
    End Sub

    Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
        ListView1.Visible = True
    End Sub

    Private Sub ButtonGeneratePW_Click(sender As Object, e As EventArgs) Handles ButtonGeneratePW.Click
        pool = ""

        If CheckBoxNumbers.Checked = True Then
            pool = pool & "0123456789"
        End If
        If CheckBoxLetters.Checked = True Then
            pool = pool & "abcdefghijklmnopqrstuvwxyz"
        End If
        If CheckBoxCapitalLetters.Checked = True Then
            pool = pool & "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        End If
        If CheckBoxUltraSafeMode.Checked = True Then
            pool = pool & "!$%&!$%&?#-_+-/()?#-!$%&?#-_+-/()_!$%&?#-_+-/()+-/()"
        End If
        Dim count = 0
        TextBoxGeneratedPW.Text = ""

        Dim cc As New Random
        Dim strpos = ""
        While count <= TextBoxLength.Text
            strpos = cc.Next(0, pool.Length)

            TextBoxGeneratedPW.Text = TextBoxGeneratedPW.Text & pool(strpos)
            count = count + 1
        End While
    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBoxNumbers.CheckedChanged

    End Sub

    Private Sub CheckBoxUltraSafeMode_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBoxUltraSafeMode.CheckedChanged

    End Sub

    Private Sub ButtonClearPW_Click(sender As Object, e As EventArgs) Handles ButtonClearPW.Click
        TextBoxGeneratedPW.Clear()
    End Sub

    Private Sub ButtonCopyPW_Click(sender As Object, e As EventArgs) Handles ButtonCopyPW.Click
        My.Computer.Clipboard.SetText(TextBoxGeneratedPW.Text)
    End Sub

    Private Sub ButtonEditData_Click(sender As Object, e As EventArgs) Handles ButtonEditData.Click
        If ListView1.SelectedItems.Count = 0 Then
            MsgBox("You have to choose an item", MsgBoxStyle.Critical)
        Else
            EditData.ShowDialog(Me)
        End If
    End Sub

    Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged

    End Sub

    '   Public Function StringToMD5(ByVal StringToEncrypt As String) As String
    '       Dim CSP As New MD5CryptoServiceProvider
    '       Dim Data As Byte()
    '       Dim Result As Byte()
    '       Dim Res As String = ""
    '       Dim Tmp As String = ""
    '   
    '       Data = Encoding.ASCII.GetBytes(StringToEncrypt)
    '       Result = CSP.ComputeHash(Data)
    '
    '   For i As Integer = 0 To Result.Length - 1
    '       Tmp = Hex(Result(i))
    '   If Len(Tmp) = 1 Then Tmp = "0" & Tmp
    '       Res &= Tmp
    '   Next
    '
    '   Return Res.ToLower
    'End Function
    Public Function TripleDESEncryptMessage(ByVal plainMessage As String, ByVal password As String) As String
        Dim des As New TripleDESCryptoServiceProvider()
        des.IV = New Byte(7) {}
        Dim pdb As New PasswordDeriveBytes(password, New Byte(-1) {})
        des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, New Byte(7) {})
        Dim ms As New MemoryStream(plainMessage.Length * 2)
        Dim encStream As New CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write)
        Dim plainBytes As Byte() = Encoding.UTF8.GetBytes(plainMessage)
        encStream.Write(plainBytes, 0, plainBytes.Length)
        encStream.FlushFinalBlock()
        Dim encryptedBytes As Byte() = New Byte(ms.Length - 1) {}
        ms.Position = 0
        ms.Read(encryptedBytes, 0, CInt(ms.Length))
        encStream.Close()
        Return (Convert.ToBase64String(encryptedBytes))
    End Function
    Public Function TripleDESDecryptMessage(ByVal encryptedBase64 As String, ByVal password As String) As String
        Dim des As New TripleDESCryptoServiceProvider()
        des.IV = New Byte(7) {}
        Dim pdb As New PasswordDeriveBytes(password, New Byte(-1) {})
        des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, New Byte(7) {})
        Dim encryptedBytes As Byte() = Convert.FromBase64String(encryptedBase64)
        Dim ms As New MemoryStream(encryptedBase64.Length)
        Dim decStream As New CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write)
        decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
        decStream.FlushFinalBlock()
        Dim plainBytes As Byte() = New Byte(ms.Length - 1) {}
        ms.Position = 0
        ms.Read(plainBytes, 0, CInt(ms.Length))
        decStream.Close()
        Return (Encoding.UTF8.GetString(plainBytes))
    End Function
End Class