MD5 encryption

Greetings to all you good people!

Im trying to do something very simple.. in theory.  I have a String, and i want to get another String.. (the MD5'ed version of the string i started with...

As you will see from the code, the string 'mehdi' is what i want to apply MD5 encryption to!.. the result (varified by 2 other programs) should be 7f7d49795dcf0a82605fb1103ed20d28

the result I get is a sloppy [B@62eec8

:(

I cant use apache.org .. bla bla.. must be sun.. im using JDK 1.3.1 on Win2000 and im running this program from the command prompt.

heres the code I have so far:

-------------------------------

import java.security.*;

public class MD5Encrypt {

     public static void main (String args[])     {
          System.out.println("here");

          try {

               MessageDigest alg = MessageDigest.getInstance("MD5");

               String myVar = "mehdi";

               byte bs[] = myVar.getBytes();

               // a simple loop to print out my array ! - shows everything to be ok
               //for (int i=0; i<=bs.length-1; i++) {
               //     System.out.println (i + " : " + bs[i]);
               //}

               alg.update(bs);

               // for christmas, all i want is a new bike.. and this to give me an MD5ized String !!!
               System.out.println ("its.." + alg.digest(bs).toString());

          }

          catch (Exception e) {
               System.out.println("there appears to have been an error" + e);
               return;
          }

          System.out.println("well, nothing went too wrong.. ");
     }

}



LVL 1
mehdiAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
superschlonzConnect With a Mentor Commented:
I'm very sorry, it's my fault. The conversion to hex is wrong.
This line should work:

System.out.print( Integer.toHexString( 0x0100 + (digest[i]&0x00FF) ).substring(1) );
0
 
superschlonzCommented:
What you get from the digest() method is a byte array.
You have to convert the result to a hex string:

byte digest[] = alg.digest(bs);
System.out.print( "its.." );
for( int i=0; i<digest.length; ++i )
{
  System.out.print( Integer.toHexString( 0x0100 + digest[i] ).substring(1) );
}
System.out.println( "" );
0
 
CalronCommented:
Have a loock at www.cryptix.com there you can donload an opensource java package for encryption that includes the MD5 algorithm.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
sudhakar_koundinyaCommented:
<listening>
0
 
sudhakar_koundinyaCommented:
<listening>
0
 
mehdiAuthor Commented:
SuperSch..

thanks for your answer.  It certainly seems to be getting there.  Using the extra piece of code you sent i get the following:

7f7d49795df0a2605f1103e20d28


It looks like the answer i get for the string "mehdi" from other encryption sources, but it is not.

the code on sites..

http://www.cs.hut.fi/~santtu/java/Hasher.html
..and
http://pajhome.org.uk/crypt/md5/

*both* give me

7f7d49795dcf0a82605fb1103ed20d28 .. [looks the same at first glace but is different].

cheers,

Mehdi
0
 
_marko_Commented:
Hello.

I'm no expert on Java, but here is a solution which works. I'm sure you can streamline the code better than I did :)



import java.security.*;

public class MD5Encrypt {

    public static void main (String args[])     {
         try {

          MessageDigest alg = MessageDigest.getInstance("MD5");

          String myVar = "mehdi";

          alg.update ( myVar.getBytes() );
          byte digest[] = alg.digest();
          System.out.print( "its.." );
         
          StringBuffer hexString = new StringBuffer();
          for (int i=0;i<digest.length;i++) {
                 myVar = Integer.toHexString(0xFF & digest[i]);
                 if (myVar.length() < 2) myVar = "0" + myVar;
                 hexString.append ( myVar );
          }
          System.out.println(hexString.toString());



         }

         catch (Exception e) {
              System.out.println("there appears to have been an error" + e);
              return;
         }

         System.out.println("well, nothing went too wrong.. ");
    }
}



Marko
0
 
superschlonzCommented:
I forgot that negative bytes are expanded to negative ints by filling up all
bits with ones, for example:

byte b = 0x80;
int i = b; // i now contains 0xFFFFFF80
// then i added 0x0100;
int i2 = i + 0x0100; // i2 contains now 0x00000080;
// now convert to hex string
String s = Integer.toHexString( i2 ); // s now contains "80"
// then I removed the first character of the string
String s2 = s.substring(1); // s2 contains "0"

But s2 should always contain 2 characters but in my firsat version it
contains only one character which is wrong. But when you change the
conversion to int as follows it works:

int i = b & 0x00FF; // i now contains 0x0080;
-> i2 = 0x1800
-> s = "180"
-> s2 = "80"

0
 
mehdiAuthor Commented:
all this Hex stuff is over my head.. I have to read on some of this stuff.

I did get the same answer as other sources from what this answer.

cheers!

Mehdi
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.