MD5加密java代码

package com.test;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import sun.misc.BASE64Encoder;

public class MD5 {
	public String EncoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
		MessageDigest md5 = MessageDigest.getInstance("MD5");
		BASE64Encoder base64e = new BASE64Encoder();
		String newstr = base64e.encode(md5.digest(str.getBytes("utf-8")));
		return newstr;
	}

	public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {
		String str = "lpp";
		String newstr = new MD5().EncoderByMd5(str);
		System.out.println(str + "==" + newstr);
		Boolean flag = new MD5().checkpassword("lp", newstr);
		System.out.println("密码是否正确:" + flag);
	}

	public boolean checkpassword(String newpasswd, String oldpasswd)
			throws NoSuchAlgorithmException, UnsupportedEncodingException {
		if (EncoderByMd5(newpasswd).equals(oldpasswd))
			return true;
		else
			return false;
	}
}
以上是MD5加密和字符串验证代码。

猜你喜欢

转载自blog.csdn.net/lp15203883326/article/details/80481896