Thread: Implementing the MD5 encryption library in C

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    12

    Implementing the MD5 encryption library in C

    Hey fellas

    I'm trying to modify a slightly complex C program, and all I need to do is convert a short character string into the hex representation of its MD5. The only problem is, I've never implemented MD5 in C. I'm very familiar with PHP, which has a built in MD5 function, but C programmers arent so fortunate (or restricted). If anyone can show me how to perform this simple task, or point me to a site with some particularly useful information, I'd greatly appreciate it.

    Thanks
    If at first you don't succeed, punch your monitor, kick your chair, strangle your boss, and toss your computer out of the nearest window. Then, once you've relieved a portion of your frustration, try again.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    http://userpages.umbc.edu/~mabzug1/cs/md5/md5.html

    It's amazing what three little words (One of them being a single lette!) and a search engine can do for you.

    1) Go to www.google.com
    2) Enter: MD5 C Programming
    3) Click some links.
    4) Enjoy.

    This is your first and only lesson in the class "Search Engines 101".

    Quzah.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    12

    Thanks, but no thanks

    Perhaps I should be a little more clear. I'm not looking for the actual MD5 code. I can find all of that I need. I have searched, but I cannot find the simple answer I am looking for.

    The post I wrote contained enough information for anyone who's reasonably familiar with the process to give some simple instructions. If you're not familiar, you need not answer. I did not come here to get smart a-- answers or to be insulted.

    Thanks
    If at first you don't succeed, punch your monitor, kick your chair, strangle your boss, and toss your computer out of the nearest window. Then, once you've relieved a portion of your frustration, try again.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > and all I need to do is convert a short character string into the
    > hex representation of its MD5.

    So what exactly is complex? You're given a MD5 encrypted string, correct? You want to display it in hex, correct?

    for(x=0;x<strlen(s);x++) printf("0x%x ",string[x]);

    Surely you didn't need my help for THAT. If not, by all means, expound.

    Quzah.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    12

    Not Quite...

    I asked how to convert a character string into the hex representation of its MD5.

    Let me break this down for you.

    Start with a short character string.

    Pass this to an MD5 string encryption algorithm (or a series of algorithms as the case in C seems to be).

    Take the hex result that is returned and convert it back to a character string (don't bother, I can take care of this much).


    All I need help with is importing the MD5 library and using it in the appropriate and simplistic manner. Since there are so many varied uses for MD5, I have found myself unable to find the simplistic answer I've been looking for.

    It's not as easy as:
    $string = MD5("$anotherString");

    There is a lot more involved when doing something like this in C instead of PHP, and that is why I've come here - to ask if anyone more experienced with C than myself can give me a simple answer to this problem.

    Thanks
    If at first you don't succeed, punch your monitor, kick your chair, strangle your boss, and toss your computer out of the nearest window. Then, once you've relieved a portion of your frustration, try again.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Uh, actually, the very first link I gave you has a way to do this in it. Additionally, there's an MD5 library in C you can use and basicly do exactly what you needed. You don't want an implementation, but you want a way to do it? That's the same thing. A way to do something, is an implementation.

    Additionally, you canno get a "hex value". There is no data type for "hex value". You only have basic types: char, int, long, which can be displayed any way you want. If you need the "hex value", being an encoded block of data, then you'll have to represent it in a structure, or an unsigned char block, or something similar. There is no such thing as a "hex result".

    Again, the very first link I gave you has such an implementation:

    encode( tothisarray, fromthisarray, length );

    Like I said, it took me all of 30 seconds with a search engine to find EXACTLY what you were looking for, 1 click deep into the first search result in the list.

    Quzah.

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    12
    By hex I meant binary. I wasnt sure exactly what gets returned by MD5. Pardon the oversight.

    I will try your suggestion. That's all you had to say, there was never any need for the bashing. All of the material I've read never said anything about encode.

    Thanks anyway

    ps, if this doesn't work, I'll be back!
    If at first you don't succeed, punch your monitor, kick your chair, strangle your boss, and toss your computer out of the nearest window. Then, once you've relieved a portion of your frustration, try again.

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    12

    Ok

    Alright.

    I've searched some more, and I still cannot find the answer I'm looking for. There is no mention of "encode()" in that "md5 unoffficial homepage", the link to which you gave in your first reply.

    Although my explanation of what I needed may have had some fundamental verbalization errors, there shouldnt be any confusion among knowledgeable people as to what I need.

    You say you've found the answer to what I was looking for in "30 seconds" and with "one click". You gave me a prototype for a function that allegedly does exactly what I need. You also mentioned that it is on the site that I previously mentioned. I might be blind, but I did not see that function anywhwere under the C section, nor in any of the source code linked to therein.

    It's like this. I don't have time for the run-around, and I don't have time to spend digging through more poorly constructed and maintained websites for what I need, which is an answer, straightforward and simple. So enough of this "go get it yourself, you stupid moron" attitude and just provide me with what I requested (with detail, not just an allusion) if you have it. Like I said before, if you don't know, you don't have to answer. There is no need to become upset because you are somehow able to find the answers and I seem to be missing something. This message board, I assume, is here for people like myself, who lack knowledge in certain areas, to come and post questions for people who have that knowledge to help them out in any way they in which they can and are willing.

    If little things like my overlooking something and asking a question that's easy to answer or has been asked before upsets you that much, then you need to take a long, hard look at what's going on in your own life, because I believe you have problems beyond that of having to deal with some ignorant people in a message board that means little compared to the reality of life from both our perspectives. I dont need to get bashed because somebody is having marital problems, or because their father never said he loved him. This is just the INTERNET, it DOESNT MATTER. There are millions of ignorant people on it, and it affords you nothing but your own misery to get upset every time you run into one of them.

    Now, it is obvious that I'm no guru when it comes to C. If you claim to know that there is a library that contains what I need, don't tell me about it, tell me what it is. What do I need to include, what library do I need to link to, etc. I'm not looking for answers like "yeah, there is one, but you have to go and look for it". Thats why I came here to begin with. If anyone had simply given me the details I needed, I'd have been satisified and left with gratitude.

    Now, please, are there any decent and compassionate people out there who can help me out by giving me the information I need IN DETAIL? I am pressed for time and don't have the patience for this stupidity. If I am the stupid one, then just recognize the fact that I am an idiot and give me a helping hand. That's all I ask.
    Last edited by Weed4Me; 11-08-2001 at 02:05 PM.
    If at first you don't succeed, punch your monitor, kick your chair, strangle your boss, and toss your computer out of the nearest window. Then, once you've relieved a portion of your frustration, try again.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    From here:
    http://userpages.umbc.edu/~mabzug1/cs/md5/md5.html
    Click here:
    "more easily extracted (tarred, gzipped)"

    Download this source code, and view the "MD5C.C" file. This is
    what contains the encode function I was talking about.

    Code:
    /* Encodes input (UINT4) into output (unsigned char). Assumes len is
      a multiple of 4.
     */
    static void Encode (output, input, len)
    unsigned char *output;
    UINT4 *input;
    unsigned int len;
    {
      unsigned int i, j;
    
      for (i = 0, j = 0; j < len; i++, j += 4) {
     output[j] = (unsigned char)(input[i] & 0xff);
     output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
     output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
     output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
      }
    }
    Again, this was the first code link I tried, and quickly browsed it. I guess I just got lucky.

    While I haven't personally done it, you should be able to customize their source code and make it a library or something you can link to. Anyway, that's where the 'encode' function is that I was talking about.

    You'll see above that they use the 'old style' prototypes, so you could easily rewrite it to:

    static void Encode ( unsidned char *output, UINT4 *input, unsigned int len)

    It looks as though you'll have to cram your text string into their structure 'UINT4'. They probably have functions to do it, I haven't read the code in its entirety.

    Again, I haven't tried it, but that should do the trick. There is also an MD5 library for linux that someone I used to code with kept wanting to use for the project, that I personally never did anything with. You should be able to find something on it.

    Quzah.

  10. #10
    Registered User
    Join Date
    Nov 2001
    Posts
    12
    Ok,

    Well, the answer has been right in front of me the whole time. As a matter of fact, the "encode" function you gave me is not what I needed, but it is one small piece of a much larger, more complex, and a harder to understand puzzle. I was hoping there would be a more simple solution, but I went ahead, gnashed my teeth, and figured out a way to use all of this this ugly code to get my string into an alphanumeric binary number. When printed out in hex, like in your for loop example, the hex number displayed matches what I'm looking for.

    Now I just have one more simple question. What I need to do is convert that alphanumeric binary string into its hex representation, storing it in another string. I know you're probably thinking "he should know that...", but remember, I have very little C experience. All you have to do is help me out with this one last thing and I'll be out of your hair forever (or at least until I am forced to work with C again ).

    Thanks for answering, though. I can sortof relate to why you get so P.O.'d at dumb people. I used to do technical support for an ISP. It was my duty to answer the stupid questions of stupid people over and over and over and over by telephone. I almost let that get to me, I became extremely irritable all the time and had pretty much lost most of the joy in my life because of it. Now, I allowed a job to almost completely destroy my peace of mind, but why should you let a dumb messageboard on the internet get to you? If you just ignore the dumb people, you make them look like the idiots because everyone else will see that the people who have a lot of expertise don't even bother. But, if you harp on them constantly, you only make yourself look like a jerk. The way I see it, life is short. We simply don't have time to sit around and be p_ssed all the time.

    Anyway, thanks again.

    ** Update: Nevermind about that conversion question. I found a solution.
    Last edited by Weed4Me; 11-08-2001 at 05:25 PM.
    If at first you don't succeed, punch your monitor, kick your chair, strangle your boss, and toss your computer out of the nearest window. Then, once you've relieved a portion of your frustration, try again.

  11. #11
    Unregistered
    Guest
    What I need to do is convert that alphanumeric binary string into its hex representation, storing it in another string. I know you're probably thinking "he should know that...", but remember, I have very little C experience. All you have to do is help me out with this one last thing and I'll be out of your hair forever (or at least until I am forced to work with C again ).
    I'm not sure I get your meaning here. Do you have a string of 0101010111010101 ... or do you just mean the string that the encode() function (or whatever) retuns? Additionally, do you just want the string of text to read: "0xe20xee0x140x..." or?

    To display something in hex, you can just use the %x format specifier for printf(). You can use this like I did with the for() loop earlier, if that's what you're looking for.

    Quzah.

  12. #12
    Registered User
    Join Date
    Nov 2001
    Posts
    12
    Basically I just need an ASCII to Hex conversion function. I found one on the web, but I'm wondering if there is a built-in function in C that will do it (I don't think the one I found is converting properly).

    Your for loop with a "%x" specifier in printf printed the result I was looking for, but I'm trying to store that result in another string instead of printing it out.

    Example:

    Start with "Your Mom"

    Run that string through the MD5 sequence.

    MD5 returns: "´æ~Ét±KpUlä1àßf"

    Run this through a slightly modified version of your for loop, and it prints: b4e617ec974b14b70556ce431e0df66

    The latter is the result I'm looking for. So I was slightly off by saying that I had an "alphanumeric binary string". In reality, I've realized that I'm given an ASCII string full of garbage, and the hex representation of that ASCII string's binary code is what I need.

    This is the code I found to perform this task, but for some reason it doesn't work.
    PHP Code:
    char     get_digitchar c )
    {
            if ( ( 
    >= '0' ) && ( <= '9' ) )
                    return( 
    '0' );
            if ( ( 
    >= 'a' ) && ( <= 'f' ) )
                    return( 
    'a' 10 );
            if ( ( 
    >= 'A' ) && ( <= 'F' ) )
                    return( 
    'A' 10 );
            return -
    1;
    }

    int atohchar *string 
    {
            
    int     digitresult;

            
    result 0;
            while( ( 
    digit get_digit( *string++ ) ) != -)
                    
    result result 16 digit;
            return( 
    result );

    Last edited by Weed4Me; 11-09-2001 at 02:32 PM.
    If at first you don't succeed, punch your monitor, kick your chair, strangle your boss, and toss your computer out of the nearest window. Then, once you've relieved a portion of your frustration, try again.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could always try 'sprintf()'

    Not the most graceful way to do it, but this should work:
    for(x = 0; s[x]; x++){sprintf( buf, "%x", s[x] );strcat( buf2, buf );}

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implementing Library Algorithms
    By jewelz in forum C++ Programming
    Replies: 9
    Last Post: 04-06-2009, 12:44 AM
  2. Email Encryption Library in C
    By saadahmed42 in forum C Programming
    Replies: 1
    Last Post: 08-22-2008, 11:15 AM
  3. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  4. MD5 encryption.
    By Maz in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2005, 02:46 AM
  5. MD5 encryption
    By marker5a in forum C Programming
    Replies: 5
    Last Post: 10-27-2005, 07:19 PM