Example of MD5 encryption method in Java

Source: Internet
Author: User

You only need to write the class and call it in the background:

Import Java. security. messagedigest; public final class md5token {Private Static char hexdigits [] = {'0', '1', '2', '3', '4', '5 ', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E ', 'F'}; Private Static md5token instance = NULL; private md5token () {} public synchronized static md5token getinstance () {If (instance = NULL) {instance = new md5token ();} return instance;}/*** generate a 16-bit encryption Password
* @ Param arg0 * @ return */Public String getasktoken (string arg0) {return encoder (arg0). substring (8, 24);}/*** generates a 24-bit encryption Password
 * @param arg0 * @return */public String getLongToken(String arg0) {return encoder(arg0).toString();}private StringBuffer encoder(String arg){if(arg==null){arg="";}MessageDigest md5 = null;try {md5=MessageDigest.getInstance("MD5");md5.update(arg.getBytes("utf-8"));} catch (Exception e) {e.printStackTrace();}return toHex(md5.digest());}private StringBuffer toHex(byte[] bytes) {StringBuffer str = new StringBuffer(32);int length=bytes.length;for (int i = 0; i < length; i++) {str.append(hexDigits[(bytes[i] & 0xf0) >> 4]);str.append(hexDigits[bytes[i] & 0x0f]);}return str;}public static void main(String args[]){String password = "mysql";String p = Md5Token.getInstance().getLongToken(password);System.out.print(p);}}

 

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.