Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 6th, 2006, 4:26 PM   #1
emdiesse
Programmer
 
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 5 emdiesse is on a distinguished road
Md5

Hello.

I would like to MD5 a value of two text box's.
txtOne
txtTwo

So I would like to make the two textboxes into a variable.
Dim Text As String
String = txtOne + txtTwo
I believe that would be how

Then MD5 the variable.
I am not sure how I do this but I have found this, which to me, appears to be relative.
    Function getMd5Hash(ByVal input As String) As String
        ' Create a new instance of the MD5 object.
        Dim md5Hasher As MD5 = MD5.Create()

        ' Convert the input string to a byte array and compute the hash.
        Dim data As Byte() = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input))

        ' Create a new Stringbuilder to collect the bytes
        ' and create a string.
        Dim sBuilder As New StringBuilder()

        ' Loop through each byte of the hashed data 
        ' and format each one as a hexadecimal string.
        Dim i As Integer
        For i = 0 To data.Length - 1
            sBuilder.Append(data(i).ToString("x2"))
        Next i

        ' Return the hexadecimal string.
        Return sBuilder.ToString()

    End Function

Please can someone help me to make this work how I would like it to work
emdiesse is offline   Reply With Quote
Old Jan 6th, 2006, 5:41 PM   #2
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Please tell us what doesn't work, or what works wrong. We are quite good at programming, but we fail miserably at mind reading.
Polyphemus_ is offline   Reply With Quote
Old Jan 6th, 2006, 6:29 PM   #3
emdiesse
Programmer
 
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 5 emdiesse is on a distinguished road
Quote:
Originally Posted by Polyphemus_
Please tell us what doesn't work, or what works wrong. We are quite good at programming, but we fail miserably at mind reading.
Sorry. I thought I had but after reading it I have realised I missed out some vital Info.

The second bit of code is something I have found on the internet concerning strings and MD5. I am tring to convert this to something that will change a value of a variable (string variable) to MD5, but I am unsure as to how I would do this.

I got the code from here:
http://msdn2.microsoft.com/en-us/lib...aphy.md5.aspx#

Last edited by emdiesse; Jan 6th, 2006 at 6:52 PM.
emdiesse is offline   Reply With Quote
Old Jan 7th, 2006, 1:48 PM   #4
Rory
Expert Programmer
 
Rory's Avatar
 
Join Date: Jan 2005
Location: London
Posts: 542
Rep Power: 4 Rory is on a distinguished road
Send a message via MSN to Rory
OK, just a few things about that code: Firstly the data should be passed as a reference type to prevent needless duplication of memory, which is important if you're hashing a relatively large amount of data.

Secondly, however, if you're using this to hash entire files, you should use System.Security.Cryptography.MD5.ComputeHash with a filestream instead, and wrapper this in a callback thread so that your application does not "freeze" while the hash is being calculated, perhaps even giving user feedback.

Then again if the reliability and speed are important, you might want to look for an open source optimised ASM library like OpenSSL instead, as the .NET FW's routines are written in CLR and are appallingly slow.

Anyway here's your function:

Public Function getMd5Hash(ByRef Data As String) As String
        Dim md5Hasher As System.Security.Cryptography.MD5 = System.Security.Cryptography.MD5.Create()

        Dim Hash As Byte() = md5Hasher.ComputeHash(System.Text.Encoding.ASCII.GetBytes(Data))

        Dim sBuilder As System.Text.StringBuilder = New System.Text.StringBuilder(32)

        For I As Integer = 0 To Hash.Length - 1
            sBuilder.Append(Hash(I).ToString("x2"))
        Next I

        Return sBuilder.ToString()

    End Function
Rory is offline   Reply With Quote
Old Feb 9th, 2006, 11:45 AM   #5
emdiesse
Programmer
 
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 5 emdiesse is on a distinguished road
Quote:
Originally Posted by Rory
Anyway here's your function:

Public Function getMd5Hash(ByRef Data As String) As String
        Dim md5Hasher As System.Security.Cryptography.MD5 = System.Security.Cryptography.MD5.Create()

        Dim Hash As Byte() = md5Hasher.ComputeHash(System.Text.Encoding.ASCII.GetBytes(Data))

        Dim sBuilder As System.Text.StringBuilder = New System.Text.StringBuilder(32)

        For I As Integer = 0 To Hash.Length - 1
            sBuilder.Append(Hash(I).ToString("x2"))
        Next I

        Return sBuilder.ToString()

    End Function
Thanks for the hints and the code

I have made the amendments to my code and I have this as the code:
Imports System
Imports System.Security.Cryptography
Imports System.Text



Module Generate
    ' Hash an input string and return the hash as
    ' a 32 character hexadecimal string.
    Public Function getMd5Hash(ByRef Data As String) As String
        Dim md5Hasher As System.Security.Cryptography.MD5 = System.Security.Cryptography.MD5.Create()

        Dim Hash As Byte() = md5Hasher.ComputeHash(System.Text.Encoding.ASCII.GetBytes(Data))

        Dim sBuilder As System.Text.StringBuilder = New System.Text.StringBuilder(32)

        For I As Integer = 0 To Hash.Length - 1
            sBuilder.Append(Hash(I).ToString("x2"))
        Next I

        Return sBuilder.ToString()
    End Function



    Sub Main()
        Dim source As String
        Dim frmMd5 As New frmMd5
        source = frmMd5.txtOne.Text & frmMd5.txtApp.Text
        Dim hash As String = getMd5Hash(source)
        frmMd5.txtEncryptedText.Text = hash
    End Sub

End Module



Public Class frmMd5
    Inherits System.Windows.Forms.Form



    Private Sub btnGen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGen.Click
            Main()  'Run Sub Main(), Generates md5




I get this error when I click generate:
An unhandled exception of type 'System.InvalidOperationException' occurred in system.windows.forms.dll

Additional information: It is invalid to start a second message loop on a single thread. Use Application.RunDialog or Form.ShowDialog instead.
It also highlights the text "frmMd5" in green. I have also highlighted this bit in green in the code.

Please help
emdiesse is offline   Reply With Quote
Old Feb 11th, 2006, 3:48 PM   #6
emdiesse
Programmer
 
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 5 emdiesse is on a distinguished road
Ahah. Done it.

I would now like to know how to change the length of the the output of my Md5 hash. I would like it to be possible to make it longer or shorter by the use of another text box.

How do I do this?
Do the numbers have to be base 2 (2,4,8,16,32,64,etc), or can they be any integer?

I have changed the code to this:
Imports System
Imports System.Security.Cryptography
Imports System.Text
Module Generate
    Public Function getMd5Hash(ByRef Data As String) As String
        Dim md5Hasher As System.Security.Cryptography.MD5 = System.Security.Cryptography.MD5.Create()

        Dim Hash As Byte() = md5Hasher.ComputeHash(System.Text.Encoding.ASCII.GetBytes(Data))

        Dim sBuilder As System.Text.StringBuilder = New System.Text.StringBuilder(32)

        For I As Integer = 0 To Hash.Length - 1
            sBuilder.Append(Hash(I).ToString("x2"))
        Next I

        Return sBuilder.ToString()

    End Function
End Module



Public Class frmMd5
    Inherits System.Windows.Forms.Form

    Private Sub btnGen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGen.Click
            Dim source As String
            source = txtOne.Text & txtTwo.Text
            Dim hash As String = getMd5Hash(source)
            txtEnc.Text = hash
    End Sub
End Class

Thanks.
emdiesse is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:41 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC