Java uses messagedigest for MD5 encryption

Source: Internet
Author: User
Tags md5 encryption sha1 stringbuffer

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.