Results 1 to 2 of 2

Thread: md5 encryption

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    md5 encryption

    Basically, i've imported this:

    import java.security.*;

    Then,
    MessageDigest md = MessageDigest.getInstance("MD5");

    creates the new instance of it. But im a bit unsure on the rest, i.e, the updating and digest etc? can someone show me how i can get a md5 encrypt of 'hello'

    which would be

    5d41402abc4b2a76b9719d911017c592

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: md5 encryption

    MD5 encrypts binary data and results in a 128-bit binary number. So first you need to convert the string to binary, then you digest it, and finally, if you really need to, you convert it back to a string representation. (E.g. hexadecimal in your case.)
    The thing about getting binary from a string is that you have to specify the encoding. I tend to use UTF-8.

    Code:
    String input = "hello";
    byte[] data = input.getBytes("UTF-8");
    byte[] digest = md.digest(data);
    // Somehow convert back.
    Now the thing is, this conversion takes another bit of code. All in all, it's a hassle. It's easier to download the Apache Jakarta Commons Codec library and just write
    Code:
    String hex = org.apache.commons.codec.digest.DigestUtils.md5Hex("hello");
    The only shortcoming is that it doesn't let you specify the encoding.
    Last edited by CornedBee; Feb 1st, 2007 at 05:22 PM.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width