加密算法MD5

package com.lenovo.lps.epub.eu.util;

import java.security.MessageDigest;
import java.util.Random;

import com.lenovo.lps.log.Log;

/**
 * @author zhangdz1
 *
 * @Date:2010-11-26 上午09:51:59
 * @Description:md5工具类,为给定的串生成32位的md5串
 * @version:
 *
 */
public class MD5Util {

	final static Log log = Log.getInstance(MD5Util.class);
	
	/**
	 *  将传入字符串做md5,返回md5后的串
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static String encryptToMD5(String str) throws Exception {
		MessageDigest md5 = MessageDigest.getInstance("MD5");
		md5.update(str.getBytes());
		return byte2hex(md5.digest());

	}

	/**
	 *  二行制转字符串
	 * @param b
	 * @return
	 */
	public static String byte2hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1)
				hs = hs + "0" + stmp;
			else
				hs = hs + stmp;
			if (n < b.length - 1)
				hs = hs + "";
		}
		return hs;
	}
	
	public static void main(String args[]) {
		try {
			String s="ZAQAHAAAAACrZSob_liEfa4C5InVDxJh3SuIBUM30T9bHfKaEmy7MJeAhfYNIl53XMMcv8GvRREISHUym_leRUyj4Mwm87S7gD8AH0z7qv4C4YyAd3pzyV-HT9ZSy3bj06e13kQ8yhPf7xYWALCtVr7iM5cKmAtXtR3WcFBIAM2Ri_JJgvRWSyaciJy3qZpmXIwJh8xDKW0F-3Ao0ytUtewMkxr_Q80g";
			System.out.println(s.length());
			Random r = new Random();
			 int x = r.nextInt(999999); 
			 System.out.println(x);
			String str = "meiyx89";
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			System.out.println(str);
//			System.out.println(encryptToMD5(str));
		    System.out.println(encryptToMD5((encryptToMD5(str)+x)));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

你可能感兴趣的:(算法,Security)