Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In my Login form the user needs to input the username and password in the relative textboxes and then press the button LOGIN. The username and password are already saved into a text file, every time the user tries to enter the username and password entered by him will be matched with the ones saved into the text file and if they do match, only then, the user would be allowed access. I now want to be able to encrypt the password that's been entered in the textbox, I have tried looking online but I've not found any help. Does anyone have any idea? Thanks in advance.

What I have tried:

Dim intnum1 As Integer

    Dim str2 As String

    Dim icounter As Integer

 

 

    Private Sub cmdCipher_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCipher.Click

        For icounter = 1 To Len(txtCTC.Text)

            intnum1 = Asc(Mid(txtCTC.Text, icounter, 1)) 'Converts the letter to a ANSI number

            intnum1 = intnum1 + 3 'Adds three to the ANSI number

            str2 = Chr(intnum1)   'Converts the ANSI number back to a letter

 

            txtCipher.Text = txtCipher.Text & str2 'Adds the ciphered letter to the end of whatever was previously in the textbox

        Next

    End Sub

 

 

    Private Sub cmdDecipher_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDecipher.Click

        For icounter = 1 To Len(txtCipher.Text)

            intnum1 = Asc(Mid(txtCipher.Text, icounter, 1)) 'Converts the letter to a ANSI number

            intnum1 = intnum1 - 3 'Adds three to the ANSI number

            str2 = Chr(intnum1)   'Converts the ANSI number back to a letter

 

            lbldecrypt.Text = lbldecrypt.Text & str2 'Adds the ciphered letter to the end of whatever was previously in the textbox

        Next

    End Sub

End Class
Posted
Updated 1-Mar-18 8:36am
v2

1 solution

Don't encrypt it; hash it.

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

Generate a random salt for each password; append (or prepend) it to the password bytes; then generate a one-way hash of the password. Store the salt and the hash in your file.

When you need to validate the password, load the salt and the hash from the file for the user. Combine the salt with the bytes of the entered password in exactly the same way as before. Generate the one-way hash of the combined value, and compare it to the hash loaded from the file.
 
Share this answer
 
Comments
faiqaa 1-Mar-18 14:42pm    
Hi, thank you for your reply, do you mean something like this?

Option Strict On
Imports System.Text
Imports System.Security.Cryptography

Public Class Form1
Function CreateSalt(MinimumLen As Integer, MaximumLength As Integer) As String
Dim StrReturn As String = String.Empty
Dim CharsToUse As String = "ACBDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmmopqrstuvwxyz0123456789,.<>/?';:[]{}`~!@#$%^&*()-_=+"
Dim GetRandom As New Random
Dim Index As Integer = 0
For Index = 1 To GetRandom.Next(MinimumLen, MaximumLength + 1)
StrReturn &= CharsToUse.Substring(GetRandom.Next(0, CharsToUse.Length), 1)
Next
Return StrReturn
End Function
Function GetSHA1Hash(ByVal Txt As String) As String
Dim sha As New SHA1Managed()
Dim ae As New ASCIIEncoding()
Dim Hash() As Byte = sha.ComputeHash(ae.GetBytes(Txt))
Dim sb As New StringBuilder(Hash.Length * 2)
Dim ndx As Integer
For ndx = 0 To Hash.Length - 1
sb.Append(Hash(ndx).ToString("X2"))
Next
Return sb.ToString
End Function

Private Sub BtnProcess_Click(sender As System.Object, e As System.EventArgs) Handles BtnProcess.Click
If NUMSaltMin.Value >= NUMSaltMax.Value Then
TXTSalt.Text = "Minimum must be at least equal to or greater than Maximum"
Exit Sub
End If
TXTSalt.Text = CreateSalt(CInt(NUMSaltMin.Value), CInt(NUMSaltMax.Value))
TXTHash.Text = GetSHA1Hash(TXTSalt.Text & txtPassword.Text)
End Sub
End Class
Richard Deeming 1-Mar-18 14:45pm    
Yes, something like that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900