Java对字符串实现MD5加密

 
  
 
  
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import sun.misc.BASE64Encoder;

public class testMd5 {

	public static void main(String[] args) {
		String str = "123456";
		String strMd5 = md5(str);
		System.out.println("经md5加密过后为:"+strMd5);
	}

	// 使用MD5加密密码
	public static String md5(String message) {
		try {
			MessageDigest md;

			md = MessageDigest.getInstance("md5");
			byte m5[] = md.digest(message.getBytes());
			BASE64Encoder encoder = new BASE64Encoder();
			return encoder.encode(m5);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}
}

结果:

经md5加密过后为:4QrcOUm6Wau+VuBX8g+IPg==


你可能感兴趣的:(Java)