JSP standard MD5 encryption code

About Md5 encryption:

 MD5 encryption algorithm, namely "Message-Digest Algorithm 5", it is a one-way function algorithm (HASH algorithm) developed from MD2, MD3, and MD4. It is an internationally famous public key R. Rivest, the first designer of the encryption algorithm standard RSA, developed it in the early 1990s. The biggest function of MD5 is to compress large-capacity file information of different formats into a confidential format. The key point is that this "compression" is irreversible . JAVA JDK already has its own MD5 implementation, as long as you simply call it. 

Note : java.security.MessageDigest must be introduced

<%@ page import="java.security.MessageDigest"%>
<%!
	/**
	 * MD5加密
	 * @param plainText 要加密的字符串
	 * @return
	 */
	public String MD5(String plainText){
        try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byte b[] = md.digest();
 
			int i;
 
			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			//32位加密
			return buf.toString();
			// 16位的加密
			//return buf.toString().substring(8, 24);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
    }
%>

Call example:

<%
String md5=MD5("123456");
out.println(md5);
%>

Output result:

e10adc3949ba59abbe56e057f20f883e

 

Guess you like

Origin blog.csdn.net/qq15577969/article/details/112843177