Simple MD5 Encryption Class (Md5utils) in Java

Source: Internet
Author: User
Tags md5 md5 encryption

MD5 Encryption Analysis: JDK API: Get Object API: Encrypted API:
1  Packagecn.utils;2 3 Importjava.security.MessageDigest;4 Importjava.security.NoSuchAlgorithmException;5 6 /**7  * @authorCQY13 MD5 Encryption Tool class8  */9  Public classMd5utils {Ten  One     /** A * Get MD5 encryption -      *  -      * @parampwd the * strings that need to be encrypted -      * @returnstring string after strings are encrypted -      */ -      Public Staticstring Getpwd (string pwd) { +         Try { -             //Create a Cryptographic object +MessageDigest digest = messagedigest.getinstance ("MD5"); A  at             //method of invoking the cryptographic object, the encrypted action has been completed -             byte[] bs =digest.digest (Pwd.getbytes ()); -             //Next, we will optimize the results after encryption and follow the MySQL optimization approach . -             //MySQL's optimization ideas: -             //The first step is to convert all the data into positive numbers: -String hexstring = ""; in              for(byteb:bs) { -                 //The first step is to convert all the data into positive numbers: to                 //Explanation: Why use b&255 +                 /* - * B: It would have been a byte type of data (1 bytes) 255: is an int type of data (4 bytes) the * Byte type data is operated with data of type int, automatic type promotion to int type eg:b: 1001 1100 (raw data) * * operation: b:0000 0000 0000 0000 0000 0000 1001 1100 255:0000 $ * 0000 0000 0000 0000 0000 1111 1111 results: 0000 0000 0000 0000Panax Notoginseng * 0000 0000 1001 1100 The temp at this time is an integer of type int -                  */ the                 inttemp = B & 255; +                 //The second step is to convert all the data into a 16 binary form A                 //Note: When converting, note if positive >=0&&<16, then if you use Integer.tohexstring (), it may cause a missing bit number the                 //Therefore, the temp needs to be judged +                 if(Temp < && temp >= 0) { -                     //manually fill in a "0" $hexstring = hexstring + "0" +integer.tohexstring (temp); $}Else { -hexstring = hexstring +integer.tohexstring (temp); -                 } the             } -             returnhexstring;Wuyi}Catch(nosuchalgorithmexception e) { the             //TODO auto-generated Catch block - e.printstacktrace (); Wu         } -         return""; About     } $  -     /** -      * @paramargs -      */ A      Public Static voidMain (string[] args) { +String pwd = md5utils.getpwd ("abc"); the System.out.println (PWD); -     } $  the}

Simple MD5 Encryption Class (Md5utils) in Java

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.