You are on page 1of 4

index > Computing > Programming > Java > Java md5 example with MessageDigest

Title: Java md5 example with MessageDigest Posted by: chris Date 2004-09-09 11:51:40 Views: 11490 Channel: Java Replies: 3 This is a quic tip for implementing md5 encryption in java. We use the MessageD igest class in the java.security pac age and some string manipulation to turn th e plain text into a byte array. The digest is then updated from the bytes from t he byte array and a hash computation is conducted upon them. To quote from the S un java api docs, The MessageDigest class provides applications the functionalit y of a message digest algorithm, such as MD5 or SHA. Message digests are secure one-way hash functions that ta e arbitrary-sized data and output a fixed-length hash value. Anyway the md5 example code below ta es a session id (this is just a string whic h I wanted to encrypt - It could just as easily be a document, say a lump of xml or any old bit of text). This session id is pulled into the bytes array, defaul tBytes and then the MessageDigest is instantiated as an instance of an md5 encry ption. Java developers who have come over from PERL or PHP often get frustrated with su ch a longwinded means of running what could simply be a single line of code, how ever the snippet below could be wrapped into an md5sum class which conducts the encryption and simply returns a string of cipher text. include java.security.*; ... etc sessionid="12345"; byte[] defaultBytes = sessionid.getBytes(); try{ MessageDigest algorithm = MessageDigest.getInstance("MD5"); algorithm.reset(); algorithm.update(defaultBytes); byte messageDigest[] = algorithm.digest(); StringBuffer hexString = new StringBuffer(); for (int i=0;i<messageDigest.length;i++) { hexString.append(Integer.toHexString(0xFF & messageDigest[i])); } String foo = messageDigest.toString(); System.out.println("sessionid "+sessionid+" md5 version is "+hexString.toSt ring()); sessionid=hexString+""; }catch(NoSuchAlgorithmException nsae){

} Remember to import the java.security pac age. The original plain text and cipher text strings are echod to the java console just to demonstrate what has happene d. Hope that's useful, christo -----------------------------Reply ' not exactly correct' posted by gipa on 2004-09-23 09:11:47 if you use Integer.toHexString(int a), if a>=0 and a<=15 you will get a String c onsisting of only one char!!!! you need to do something li e this: StringBuffer sb=new StringBuffer(); for (int i = 0; i < bytes.length; i++) { String hex=Integer.toHexString(0xff & bytes[i]); if(hex.length()==1) sb.append('0'); sb.append(hex); } Reply ' Than s!' posted by on 2006-11-28 16:43:09 These codes were very useful to me ! Than you! Reply ' Java md5 example with MessageDigest - - immer 32 z' posted by on 2006-12-18 16:59:38 Vielen Dan fr dieses Beispiel, ich bin mal so frei und hab den Code aufbereitet, das er passt. D.h. es ommt als Ergebnis immer ein gltiger 32 Zeichelnge MD5-String raus. //---------------------------

import java.security.*; ... etc String pass = "123456"; byte[] defaultBytes = pass.getBytes(); try{ MessageDigest algorithm = MessageDigest.getInstance("MD5"); algorithm.reset(); algorithm.update(defaultBytes); byte messageDigest[] = algorithm.digest(); StringBuffer hexString = new StringBuffer(); for (int i=0;i<messageDigest.length;i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if(hex.length()==1) hexString.append('0'); hexString.append(hex); } //*** Testausgabe System.out.println("pass "+pass+" ring()); pass = hexString+""; } catch(NoSuchAlgorithmException nsae){ } //--------------------------Dan e an Euch Zwei. ICh wre da selbst nicht drauf ge ommen. Java md5 example with MessageDigest You are logged in. Clic here to view your profile You are the channel owner. Clic here to loc this channel Clic here to unloc this channel and allow public posting Clic to delete the channel - but only after its contents have been removed Clic to add a new sub-channel to this screen Clic to post a message on this channel Clic here to find out more about the channel owner This is a private channel. Only the channel owner can post here This is a public channel. Login to add sub-channels and posts Use this tool to update your spiration profile Clic here to search the channels for posts on a specific topic Logout - you can then login as a different user Navigate up to parent channel Clic here to find out more about the person who submitted this post Clic here to delete this post - but only if you are sure ! Clic here to edit this post Reply to this post Invite a user into this channel - You will need to now their spiration nic name Change the channel label, title, descriptor, or exclusivity options Touch this post and send it to the top of the list (naughty) Subscribe to this channel and receive emails when new posts arrive Unsubscribe from this channel

md5 version is "+hexString.toSt

login: password:

clic

here to join

You might also like