Wednesday, March 14, 2012

How to encrypt using MD5 with a Java code example

Using the code bellow it is possible to generate a MD5 Hash related to strings:


    public static String encrypt(String sign) {   
    
        try {   
           java.security.MessageDigest md =   
              java.security.MessageDigest.getInstance("MD5");   
           md.update(sign.getBytes());   
           byte[] hash = md.digest();   
           StringBuffer hexString = new StringBuffer();   
           for (int i = 0; i < hash.length; i++) {   
              if ((0xff & hash[i]) < 0x10)   
                 hexString.append(   
                    "0" + Integer.toHexString((0xFF & hash[i])));   
              else   
                 hexString.append(Integer.toHexString(0xFF & hash[i]));   
           }   
           sign = hexString.toString();   
        }   
        catch (Exception nsae) {   
           nsae.printStackTrace();   
        }   
        return sign;   
     }  


Here you have a MD5 Hash Generator online:
http://www.miraclesalad.com/webtools/md5.php