Click here to Skip to main content
14,095,566 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
String senc = "String to encode";
java.security.MessageDigest digest =
java.security.MessageDigest.getInstance("MD5");


What to do next to get the String with MD5?
EDIT: Is this possible at all? (using built in md5-support)
I can't found working examples:(
Please, help.
EDIT 2:

byte msgDigest[] = algorithm.digest();//==> error occurs

required: byte[], int, int.
found: no arguments.
Ok, I wrote:
byte msgDigest[] = algorithm.digest(sencBytes, 0, sencBytes.length);

and i got an error again:
required: byte[], int, int.
found: int
I don't know why found int if first argument is byte[] :(

Project properties:
Device Configuration: CLDC-1.1
Device Profile: MIDP-2.1
Posted
Updated 10-Nov-10 2:17am
v7

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

It shows you haven't opened the Java Documentation.
Anyways the following is the way to implement MD5 Message Digest. Hope it helps.
import java.security.MessageDigest;
....etc.

String senc = "String to encode";
byte[] sencBytes = senc.getBytes();
try{
	MessageDigest algorithm = MessageDigest.getInstance("MD5");
	algorithm.reset();
	algorithm.update(sencBytes);
	byte msgDigest[] = algorithm.digest();
            
	StringBuffer md5String = new StringBuffer();
	for (int i=0;i<msgDigest.length;i++) {
		md5String.append(Integer.toHexString(0xFF & msgDigest[i]));
	}
	String foo = msgDigest.toString();
	System.out.println("MD5 of "+senc+" is "+md5String.toString());
}catch(NoSuchAlgorithmException nsae){
            JOptionPane.showMessageDialog(null, "Warning!!", "No Such Alogorithm Available", JOptionPane.ERROR_MESSAGE);
}
   
v2
Comments
halabella 10-Nov-10 8:06am
   
Thanks, but I don't know why your code not works for me..
Indivara 10-Nov-10 8:36am
   
You need to say _what_ doesn't work
halabella 10-Nov-10 8:44am
   
I tested GPUToaster's code and edited my first post to show what doesn't work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190518.1 | Last Updated 10 Nov 2010
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100