Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

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);
}
 
Share this answer
 
v2
Comments
[no name] 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
[no name] 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900