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. C#
  4. Attempting to implement Keyed MD-5 Hash

Attempting to implement Keyed MD-5 Hash

Scheduled Pinned Locked Moved C#
csharpcryptographyhelpquestionhtml
6 Posts 3 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.
  • S Offline
    S Offline
    selil
    wrote on last edited by
    #1

    Code project C# guru’s :confused:, I am attempting to implement RFC 2095 ( http://www.ietf.org/rfc/rfc2095.txt ) I’ve found what I believe is an error in the crypto library for C#. Now be kind I’ve been coding C# for exactly 1 week :omg:, and it has been about 7 years since I coded in production :eek:. The last time I coded was in VI on Unix. This is just one of the methods I’m attempting to implement in this project. The rest of the project includes an SMTP server, and verification schema. I’m implementing the procedures in a C# program found in RFC 2095, and using the CRAM algorithm found in http://www.ietf.org/rfc/rfc2104.txt . The test data strings found in RFC 2095 include a share key string “tanstaaftanstaaf” and the challenge “1896.697170952@postoffice.reston.mci.net” these test values should produce according to the test in the RFC the hashed hex output “b9 13 a6 02 c7 ed a7 a4 95 b4 e6 e7 33 4d 38 90” . In the following program from the MSDN code library (ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconextendingkeyedhashalgorithmclass.htm) I get the following output from the output string B9 13 A6 2 C7 ED A7 A4 95 B4 E6 E7 33 4D 38 90 on the test case. And, of course the fourth hex pair has dropped the leading zero :doh:. That would be wrong according to the Keyed MD-5 hash algorithm :mad:. The leading zero should be left in place. From my feeble attempts at debugging it looks like it is either the output of the HEX according to Microsoft or internal to the crypto library. Either way how do I make sure that it does not drop that leading zero X| ? Thanks in advance, Sam From the MSDN Code Library with RFC 2095 Edits //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// using System; using System.Security.Cryptography; public class TestHMACMD5 { static private void PrintByteArray(Byte[] arr) { int i; Console.WriteLine("Length: " + arr.Length); for (i=0; i

    H D 2 Replies Last reply
    0
    • S selil

      Code project C# guru’s :confused:, I am attempting to implement RFC 2095 ( http://www.ietf.org/rfc/rfc2095.txt ) I’ve found what I believe is an error in the crypto library for C#. Now be kind I’ve been coding C# for exactly 1 week :omg:, and it has been about 7 years since I coded in production :eek:. The last time I coded was in VI on Unix. This is just one of the methods I’m attempting to implement in this project. The rest of the project includes an SMTP server, and verification schema. I’m implementing the procedures in a C# program found in RFC 2095, and using the CRAM algorithm found in http://www.ietf.org/rfc/rfc2104.txt . The test data strings found in RFC 2095 include a share key string “tanstaaftanstaaf” and the challenge “1896.697170952@postoffice.reston.mci.net” these test values should produce according to the test in the RFC the hashed hex output “b9 13 a6 02 c7 ed a7 a4 95 b4 e6 e7 33 4d 38 90” . In the following program from the MSDN code library (ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconextendingkeyedhashalgorithmclass.htm) I get the following output from the output string B9 13 A6 2 C7 ED A7 A4 95 B4 E6 E7 33 4D 38 90 on the test case. And, of course the fourth hex pair has dropped the leading zero :doh:. That would be wrong according to the Keyed MD-5 hash algorithm :mad:. The leading zero should be left in place. From my feeble attempts at debugging it looks like it is either the output of the HEX according to Microsoft or internal to the crypto library. Either way how do I make sure that it does not drop that leading zero X| ? Thanks in advance, Sam From the MSDN Code Library with RFC 2095 Edits //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// using System; using System.Security.Cryptography; public class TestHMACMD5 { static private void PrintByteArray(Byte[] arr) { int i; Console.WriteLine("Length: " + arr.Length); for (i=0; i

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      The bug is in your code. If MD5 didn't work once, it wouldn't work for any plain text. Use Console.Write("{0:X2}", arr[i]) instead of just using "{0:X}" for the format specifier. This makes sure that 2 chars are used per bit. Also, it's faster to use arr[i].ToString("X2") in this case, and this is typically true when you need to format only one value. Why? Because using Console.Write (or any of the formatting methods) instantiates a StringBuilder and does some having parsing to eventually call Byte.ToString(string, IFormatProvider), which you could just do directly here (though you don't need to pass an IFormatProvider in this case). It's best you don't claim bugs, especially when you've only been programming C# for 1 week. Check out the documentation, search the 'net for similar findings, and ask someone to verify (like on this forum), then declare you've found a bug and report it to the proper channels.

      Microsoft MVP, Visual C# My Articles

      S 1 Reply Last reply
      0
      • S selil

        Code project C# guru’s :confused:, I am attempting to implement RFC 2095 ( http://www.ietf.org/rfc/rfc2095.txt ) I’ve found what I believe is an error in the crypto library for C#. Now be kind I’ve been coding C# for exactly 1 week :omg:, and it has been about 7 years since I coded in production :eek:. The last time I coded was in VI on Unix. This is just one of the methods I’m attempting to implement in this project. The rest of the project includes an SMTP server, and verification schema. I’m implementing the procedures in a C# program found in RFC 2095, and using the CRAM algorithm found in http://www.ietf.org/rfc/rfc2104.txt . The test data strings found in RFC 2095 include a share key string “tanstaaftanstaaf” and the challenge “1896.697170952@postoffice.reston.mci.net” these test values should produce according to the test in the RFC the hashed hex output “b9 13 a6 02 c7 ed a7 a4 95 b4 e6 e7 33 4d 38 90” . In the following program from the MSDN code library (ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconextendingkeyedhashalgorithmclass.htm) I get the following output from the output string B9 13 A6 2 C7 ED A7 A4 95 B4 E6 E7 33 4D 38 90 on the test case. And, of course the fourth hex pair has dropped the leading zero :doh:. That would be wrong according to the Keyed MD-5 hash algorithm :mad:. The leading zero should be left in place. From my feeble attempts at debugging it looks like it is either the output of the HEX according to Microsoft or internal to the crypto library. Either way how do I make sure that it does not drop that leading zero X| ? Thanks in advance, Sam From the MSDN Code Library with RFC 2095 Edits //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// using System; using System.Security.Cryptography; public class TestHMACMD5 { static private void PrintByteArray(Byte[] arr) { int i; Console.WriteLine("Length: " + arr.Length); for (i=0; i

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        selil wrote: static private void PrintByteArray(Byte[] arr) { int i; Console.WriteLine("Length: " + arr.Length); for (i=0; i { Console.Write("{0:X}", arr[i]); Console.Write(" "); if ( (i+9)%8 == 0 ) Console.WriteLine(); } if (i%8 != 0) Console.WriteLine(); } This is happening because the format specifier in {0:X} is dropping the leading zero. The value is represented with the leading zero (obviously, since the value is 2), but the leading zero is just not printed. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        S 1 Reply Last reply
        0
        • H Heath Stewart

          The bug is in your code. If MD5 didn't work once, it wouldn't work for any plain text. Use Console.Write("{0:X2}", arr[i]) instead of just using "{0:X}" for the format specifier. This makes sure that 2 chars are used per bit. Also, it's faster to use arr[i].ToString("X2") in this case, and this is typically true when you need to format only one value. Why? Because using Console.Write (or any of the formatting methods) instantiates a StringBuilder and does some having parsing to eventually call Byte.ToString(string, IFormatProvider), which you could just do directly here (though you don't need to pass an IFormatProvider in this case). It's best you don't claim bugs, especially when you've only been programming C# for 1 week. Check out the documentation, search the 'net for similar findings, and ask someone to verify (like on this forum), then declare you've found a bug and report it to the proper channels.

          Microsoft MVP, Visual C# My Articles

          S Offline
          S Offline
          selil
          wrote on last edited by
          #4

          Heath Stewart wrote: It's best you don't claim bugs, especially when you've only been programming C# for 1 week. Check out the documentation, search the 'net for similar findings, and ask someone to verify (like on this forum), then declare you've found a bug and report it to the proper ch First. Thank you very much. Second. I was using the example code from Microsoft on their implemenatation of the correct code (which is still obviously wrong according to the RFC). I did check the net for this very topic (very thin and obscure task). I did ask somebody to verify but they were looking at the entire code and easily missed the problem. Third. It was an obvious small error on my part that I should have caught. I just never figured that Microsoft would provide a sample of an RFC that wasn't compliant. That is exactly why I thought to ask the kind folks here at Code Project for help. Fourth I wasn't claiming a bug and was looking for the exact validation you provided in that the Microsoft code as supplied was wrong and not something deeper. They should change that one item to make it RFC compliant. Fifth. Thanks again. Sorry if you thought I was calling your baby ugly. Haven't you ever beat your head on the keyboard looking at the obvious to somebody else while completely missing the EASY! answer? Again thank you very much for the help.... Now to finish the project. And, I think C# is pretty darn nifty......Thanks again, and again.... Really... -------------------- Sam

          H 1 Reply Last reply
          0
          • D Dave Kreskowiak

            selil wrote: static private void PrintByteArray(Byte[] arr) { int i; Console.WriteLine("Length: " + arr.Length); for (i=0; i { Console.Write("{0:X}", arr[i]); Console.Write(" "); if ( (i+9)%8 == 0 ) Console.WriteLine(); } if (i%8 != 0) Console.WriteLine(); } This is happening because the format specifier in {0:X} is dropping the leading zero. The value is represented with the leading zero (obviously, since the value is 2), but the leading zero is just not printed. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            S Offline
            S Offline
            selil
            wrote on last edited by
            #5

            THANK YOU! Too! I'm working on a coding project for a class (500 level course) to implement a secure email (schema) login procedure. I was looking to learn C# since I had heard about how easy it is to utilize for fast development. The hardest thing to learn has been where certain libraries are located. Doing the windows programming was a breeze, but I beat my head trying to figure out where this particular issue was (I implicitly trusted the MS code). I used to do a lot of programming in VC++, and Java and everybody seems to think C# is the wave of the future (RFC 2095 and RFC 2104) so to increase the level of difficulty I switched. So I'm writing an IMAP server and IMAP mail client for my first C# project. I have three days to complete the assignment in a new language, against sketchy RFC's, and it must be fully functional by tomorrow morning... Yee haw. Thanks again! -------------------- Sam

            1 Reply Last reply
            0
            • S selil

              Heath Stewart wrote: It's best you don't claim bugs, especially when you've only been programming C# for 1 week. Check out the documentation, search the 'net for similar findings, and ask someone to verify (like on this forum), then declare you've found a bug and report it to the proper ch First. Thank you very much. Second. I was using the example code from Microsoft on their implemenatation of the correct code (which is still obviously wrong according to the RFC). I did check the net for this very topic (very thin and obscure task). I did ask somebody to verify but they were looking at the entire code and easily missed the problem. Third. It was an obvious small error on my part that I should have caught. I just never figured that Microsoft would provide a sample of an RFC that wasn't compliant. That is exactly why I thought to ask the kind folks here at Code Project for help. Fourth I wasn't claiming a bug and was looking for the exact validation you provided in that the Microsoft code as supplied was wrong and not something deeper. They should change that one item to make it RFC compliant. Fifth. Thanks again. Sorry if you thought I was calling your baby ugly. Haven't you ever beat your head on the keyboard looking at the obvious to somebody else while completely missing the EASY! answer? Again thank you very much for the help.... Now to finish the project. And, I think C# is pretty darn nifty......Thanks again, and again.... Really... -------------------- Sam

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              The samples in the documentation is riddled with bugs. Look at it - hopefully it helps you to understand if the class documentation doesn't - but never copy it.

              Microsoft MVP, Visual C# My Articles

              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