Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encrypting to SHA1 visual basic - VB 2010

Tags:

vb.net

I have an online SMF forum and when a user registers the password is encrypted with SHA1 in the database. I need to create a vb program with a login feature where only members of the forum can login. Now the part I'm stuck in is how do I encrypt the password into SHA1 in visual basic? I included some code which I don't know is correct or not so please help me out.

Imports System.Security.Cryptography
Public Class Form2

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ' declare those variables
    Dim password As String
    Dim passwordSHA As String

    password = txtPassword.Text ' give password the value of the password textbox

    Call passwordEncryptSHA(password) ' Lets call the first password encryption function for SHA1

    passwordSHA = passwordEncryptSHA(password) ' give the variable the returned SHA value

    ' finally we will display both values in the corresponding textboxes
    txtSHA1.Text = passwordSHA


End Sub
Public Function passwordEncryptSHA(ByVal password As String) As String
    Dim sha As New SHA1CryptoServiceProvider ' declare sha as a new SHA1CryptoServiceProvider
    Dim bytesToHash() As Byte ' and here is a byte variable

    bytesToHash = System.Text.Encoding.ASCII.GetBytes(password) ' covert the password into ASCII code

    bytesToHash = sha.ComputeHash(bytesToHash) ' this is where the magic starts and the encryption begins

    Dim encPassword As String = ""

    For Each b As Byte In bytesToHash
        encPassword += b.ToString("x2")
    Next

    Return encPassword ' boom there goes the encrypted password!

End Function

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
End Class

Thank you and please don't be mean because I am still learning (I'm 15)!

like image 728
Milad Shkoh Avatar asked Jun 07 '12 00:06

Milad Shkoh


1 Answers

Imports System.IO
Public Class Form1

Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    If e.KeyValue = Keys.Enter Then
        TextBox2.Text = getSHA1Hash(TextBox1.Text)
        Label3.Text = TextBox2.Text.Length
    End If
End Sub

Function getSHA1Hash(ByVal strToHash As String) As String

    Dim sha1Obj As New Security.Cryptography.SHA1CryptoServiceProvider
    Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(strToHash)

    bytesToHash = sha1Obj.ComputeHash(bytesToHash)

    Dim strResult As String = ""

    For Each b As Byte In bytesToHash
        strResult += b.ToString("x2")
    Next

    Return strResult

End Function

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    Dim fs As New FileStream("location.txt", FileMode.OpenOrCreate, FileAccess.Write)
    Dim sr As New StreamWriter(fs)
    fs.SetLength(0)
    sr.WriteLine(Me.Location.X)
    sr.WriteLine(Me.Location.Y)
    sr.Close()
    fs.Close()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If File.Exists("location.txt") Then
        Dim fs As New FileStream("location.txt", FileMode.Open, FileAccess.Read)
        Dim sr As New StreamReader(fs)

        Me.Location = New Point(sr.ReadLine, sr.ReadLine)
        sr.Close()
        fs.Close()
    End If
End Sub

Private Sub TextBox1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
    If e.KeyValue = Keys.Escape Then
        Application.Exit()
    End If
End Sub
End Class

enter image description here
so :-) its a small program I've created for you, hope it'll be useful, happy learning.
tip: ignore the extra positioning code, its nothing but an old habit of a lazy programmer..., by the way hashing is one way, encryption is two way (you can enc and then decrypt to get the same data back, but you cant un-hash data).

like image 82
Tawfik Khalifeh Avatar answered Nov 19 '22 13:11

Tawfik Khalifeh