?
Solved

MD5 encryption

Posted on 2001-06-20
9
Medium Priority
?
442 Views
Last Modified: 2010-04-16
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.. ");
     }

}



0
Comment
Question by:mehdi
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 3

Expert Comment

by:superschlonz
ID: 6211747
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
 
LVL 5

Expert Comment

by:Calron
ID: 6212978
Have a loock at www.cryptix.com there you can donload an opensource java package for encryption that includes the MD5 algorithm.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 6213041
<listening>
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 6213042
<listening>
0
 
LVL 1

Author Comment

by:mehdi
ID: 6213476
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
 
LVL 2

Expert Comment

by:_marko_
ID: 6213754
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
 
LVL 3

Accepted Solution

by:
superschlonz earned 200 total points
ID: 6213990
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
 
LVL 3

Expert Comment

by:superschlonz
ID: 6214022
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
 
LVL 1

Author Comment

by:mehdi
ID: 6214332
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month7 days, 19 hours left to enroll

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question