2 Steps to create 32-bit MD5 class to encrypt String in java



MD5 is an encryption algorithm and is be used in many applications. It also have some types, for example, 16-bit,32-bit or 128-bit, In java, if you want to use 32-bit md5 to encrypt string, you can do like this:

Step 1: Create a 32-bit MD5 class in java

In java, a sample source code of  32-bit MD5 class is :


import java.security.MessageDigest;
public class MD5 {
 public static String encryptStr(String s){
 char hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
 try {
 byte[] bytes = s.getBytes();
 MessageDigest md = MessageDigest.getInstance("MD5");
 md.update(bytes);
 bytes = md.digest();
 int j = bytes.length;
 char[] chars = new char[j * 2];
 int k = 0;
 for (int i = 0; i < bytes.length; i++) {
 byte b = bytes[i];
 chars[k++] = hexChars[b >>> 4 & 0xf];
 chars[k++] = hexChars[b & 0xf];
 }
 return new String(chars);
 }
 catch (Exception e) {
 return null;
 }
 }

}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>