Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. md5

md5

Scheduled Pinned Locked Moved Visual Basic
phpcryptographyquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ecentinela
    wrote on last edited by
    #1

    I'm having problems trying to hash a password in Visual Basic NET. The result of my code is different that the result of my php page. My VBNET code is this Dim md5 As MD5CryptoServiceProvider Dim bytValue() As Byte Dim bytHash() As Byte md5 = New MD5CryptoServiceProvider bytValue = System.Text.Encoding.UTF8.GetBytes(txtPassword.Text) bytHash = md5.ComputeHash(bytValue) md5.Clear() messagebox.show(Convert.ToBase64String(bytHash)) The result is "bXOF/9d64JOf/waR9/mEFQ==" In PHP with his simple code echo md5($password); shows "6d7385ffd77ae0939fff0691f7f98415" Maybe anyone can say me why is different? Wich is the good VB code to show the same that in PHP? Thanks in advance

    I 1 Reply Last reply
    0
    • E ecentinela

      I'm having problems trying to hash a password in Visual Basic NET. The result of my code is different that the result of my php page. My VBNET code is this Dim md5 As MD5CryptoServiceProvider Dim bytValue() As Byte Dim bytHash() As Byte md5 = New MD5CryptoServiceProvider bytValue = System.Text.Encoding.UTF8.GetBytes(txtPassword.Text) bytHash = md5.ComputeHash(bytValue) md5.Clear() messagebox.show(Convert.ToBase64String(bytHash)) The result is "bXOF/9d64JOf/waR9/mEFQ==" In PHP with his simple code echo md5($password); shows "6d7385ffd77ae0939fff0691f7f98415" Maybe anyone can say me why is different? Wich is the good VB code to show the same that in PHP? Thanks in advance

      I Offline
      I Offline
      Insincere Dave
      wrote on last edited by
      #2

      Md5 results in a 128-bit number, php's function displays this in hexadecimal this is not the same as using ToBase64String. I don't know if there is a better way but this seemed to work Dim md5 As MD5CryptoServiceProvider Dim bytValue() As Byte Dim bytHash() As Byte Dim sb As StringBuilder = New StringBuilder(32) Dim i As Integer md5 = New MD5CryptoServiceProvider bytValue = System.Text.Encoding.UTF8.GetBytes(txtPassword.Text) bytHash = md5.ComputeHash(bytValue) md5.Clear() For i = 0 To bytHash.Length - 1 sb.Append(bytHash(i).ToString("x")) Next MessageBox.Show(sb.ToString())

      E 1 Reply Last reply
      0
      • I Insincere Dave

        Md5 results in a 128-bit number, php's function displays this in hexadecimal this is not the same as using ToBase64String. I don't know if there is a better way but this seemed to work Dim md5 As MD5CryptoServiceProvider Dim bytValue() As Byte Dim bytHash() As Byte Dim sb As StringBuilder = New StringBuilder(32) Dim i As Integer md5 = New MD5CryptoServiceProvider bytValue = System.Text.Encoding.UTF8.GetBytes(txtPassword.Text) bytHash = md5.ComputeHash(bytValue) md5.Clear() For i = 0 To bytHash.Length - 1 sb.Append(bytHash(i).ToString("x")) Next MessageBox.Show(sb.ToString())

        E Offline
        E Offline
        ecentinela
        wrote on last edited by
        #3

        Thanks a lot!! The code not works completely, but it's very close. Every time you append the text, sb is 2 characters longer. The problem appears when the bythash(i).toString("x") returns 1 character. VBNET "6d 73 85 ff d7 7a e0 93 9f ff 6 91 f7 f9 84 15" PHP "6d 73 85 ff d7 7a e0 93 9f ff 06 91 f7 f9 84 15" How is the clever way to always return 2 characters? How can I format this? NOTE: My txtpassword.text is "pasa"

        E 1 Reply Last reply
        0
        • E ecentinela

          Thanks a lot!! The code not works completely, but it's very close. Every time you append the text, sb is 2 characters longer. The problem appears when the bythash(i).toString("x") returns 1 character. VBNET "6d 73 85 ff d7 7a e0 93 9f ff 6 91 f7 f9 84 15" PHP "6d 73 85 ff d7 7a e0 93 9f ff 06 91 f7 f9 84 15" How is the clever way to always return 2 characters? How can I format this? NOTE: My txtpassword.text is "pasa"

          E Offline
          E Offline
          ecentinela
          wrote on last edited by
          #4

          A workaround is this For i = 0 To bytHash.Length - 1 str = bytHash(i).ToString("x") If str.Length = 1 Then str = "0" & bytHash(i).ToString("x") End If sb.Append(str) Next But there is a clever way for sure. Anybody?

          I 1 Reply Last reply
          0
          • E ecentinela

            A workaround is this For i = 0 To bytHash.Length - 1 str = bytHash(i).ToString("x") If str.Length = 1 Then str = "0" & bytHash(i).ToString("x") End If sb.Append(str) Next But there is a clever way for sure. Anybody?

            I Offline
            I Offline
            Insincere Dave
            wrote on last edited by
            #5

            Try using ToString("x2") that should do the same as your workaround.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups