Solved

MD5 encryption

Posted on 2001-06-20
9
397 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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 6213042
<listening>
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count11 challenge 6 47
XML Paring  Error - Premature end of file. 7 56
pairs challenge 5 45
Java asynchronous logging 4 34
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

759 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now