Solved

MD5 encryption

Posted on 2001-06-20
9
427 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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 50 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

691 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