MD5 encryption Tool Class:
package com.lijianbo.test;import java.security.messagedigest;import java.security.nosuchalgorithmexception;import org.apache.commons.lang3.stringutils;/** * MD5 cryptographic algorithms tools * @author bbo * */public class MD5Utils {/* * For added security, we added a string of random strings */private static final string user_pwd_encp_prefix = "LDHDSFHWRERHESLM";/** * user password encryption * @param beforeEncp * @return */ Public static string encode (FINAL&NBSP;STRING&NBSP;BEFOREENCP) {if (StringUtils.isBlank ( BEFOREENCP)) {RETURN&NBSP;BEFOREENCP;} RETURN&NBSP;ENCRYPTMD5 (USER_PWD_ENCP_PREFIX+BEFOREENCP);} /**&NBSP;*&NBSP;MD5 encryption * @param strInput * @return */private static STRING&NBSP;ENCRYPTMD5 (final string strinput) {StringBuffer buf = null;try {// Get a MD5 converter (if you want to change the SHA1 parameter to "SHA1") messagedigest md = messagedigest.getinstance ("MD5"); // the input string into a byte array md.update ( Strinput.getbytes ()); // converts and returns the result, which is also a byte array containing 16 elements byte b[] = md.digest ();// new a stringbuffer, this is used to compose the result string (explanation: A byte is a eight-bit binary, that is, 2 hexadecimal characters (2 of 8 is equal to 16 of 2)) buf = new stringbuffer (b.length * 2);//Traverse for (int i = 0; i < b.length; i++) {if (((int) b[i] & 0xff) < 0x10) { //(int) b[i] & 0xff converted to unsigned integer buf.append ("0");} Long.tohexstring ( unsigned long integer hexadecimal string representation of Buf.append (long.tohexstring ((int) b[i] & 0xff)); }} catch (Nosuchalgorithmexception ex) {ex.printstacktrace ();} Return buf==null?null:buf.tostring (); //returns the result of after encryption}}
Call Mode:
String passwordmd5=md5utils.encode (password);
Generate a third-party secret key:
public static void Main (string[] args) {try {System.out.println (Md5utils.encode (" 168544234123234192.168.12.10disanfangmiyao123456789012345 "," GBK "). Length ());;} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}
This article is from the "Jianbo" blog, make sure to keep this source http://jianboli.blog.51cto.com/12075002/1886833
Java uses messagedigest for MD5 encryption