Imports System.IO
Public Class LoginForm1

    Dim password_guardada As String
    Dim user As String
    Dim pass As String
    Dim pass_desincriptada As String
    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        Dim erro As Boolean = False

        If UsernameTextBox.Text = Nothing Then
            MsgBox("Erro, não pode deixar o campo Username vazio!", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Erro")
            UsernameTextBox.Focus()
            erro = True
        ElseIf PasswordTextBox.Text = Nothing Then
            MsgBox("Erro, não pode deixar o campo Password vazio!", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Erro")
            PasswordTextBox.Focus()
            erro = True
        ElseIf UsernameTextBox.Text <> user Then
            MsgBox("Erro, Username Errado!", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Erro")
            UsernameTextBox.Focus()
            erro = True
        ElseIf PasswordTextBox.Text <> pass_desincriptada Then
            MsgBox("Erro, Password Errada!", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Erro")
            PasswordTextBox.Focus()
            erro = True
        End If
        If erro = False Then
            MsgBox("Bem Vindo " & user, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Bem Vindo")
            Me.Hide()
            Form1.Show()
        End If
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub

    Private Sub LoginForm1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        MsgBox("Tem certeza que deseja sair?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Info")
        If MsgBoxResult.No = True Then
            e.Cancel = True
        End If
    End Sub

    Private Sub LoginForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim altamente As Boolean

        If IO.Directory.Exists("Z:\") Then ' Procurar Por drive Z:
            altamente = True
        Else
            altamente = False
        End If

        If altamente = False Then
            If File.Exists("C:\biblioteca.txt") Then
                Dim ioFile As New StreamReader("C:\biblioteca.txt")

                password_guardada = ioFile.ReadLine ' ler estado da checkbox
                user = ioFile.ReadLine 'ler username do ficheiro
                pass = ioFile.ReadLine 'ler password do ficheiro

                pass_desincriptada = desincriptar(pass) ' Executar Função desincriptar
                If password_guardada = 1 Then
                    UsernameTextBox.AppendText(user)
                    PasswordTextBox.Text = pass_desincriptada
                    'TextBox1.Text = pass_desincriptada
                End If

            End If
        Else

        End If


    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
    End Sub

    Public Function desincriptar(ByRef pass As String) As String
        Dim a, d, b As Integer
        Dim pass_desincriptada2 As String = Nothing
        a = Len(pass) ' Obter tamanho da Password
        Dim holder(a) As Char
        For d = a - 1 To 0 Step -1
            b = AscW(pass(d)) 'Converter caracter na posição d para valor numerico
            b = b - d ' Retirar Valor de d a caracter
            holder(d) = ChrW(b) ' Converter para caracter novamente
        Next

        For d = 0 To a
            pass_desincriptada2 = pass_desincriptada2 + holder(d) ' Passar Caracteres Para uma String
        Next
        desincriptar = pass_desincriptada2
        Return desincriptar 'Returnar Password
    End Function

End Class
ElseIf PasswordTextBox.Text <> pass_desincriptada Then
            MsgBox("Erro, Password Errada!", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Erro")
            PasswordTextBox.Focus()
            erro = True

This line here is allways returning true. I've tried a bunch of different solutions, like if it's numeric comparison, i do this

Cint(pass_desincriptada)

, a second validation for chars and it works out fine. Problem is if the password contains ints and chars.
To solve this i tried:
Comparing Passwordtextbox.text to another textbox, but if the text box as the the propertie visible = false it doesn't work.
If i do this:

dim something as textbox
something = pass_desincriptada
something.visible = true
if passwordtextbox.text <> something.text

That also doesn't work.
And the most odd thing about all this, is that the passwordtextbox is getting is value from pass_desincriptada.

Can someone help me out with my code, or tell me what im doing wrong?

Thanks, NtheN

Found the asnwer. The len of passwordtextbox.text didin't match the len of pass_desincriptar.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.