Beginner MD5 Encryption

Source: Internet
Author: User

1. Create a tool class

Importjava.security.MessageDigest; /*** class Name: Cipherutil * Class Description: 16-bit MD5 encryption of data * created by: Jxufe hehaiyang* created: 2015-1-19 06:33:26 * Edit notes: *@version */ Public classcipherutil{//hexadecimal number-to-character mapping array    Private Final StaticString[] hexdigits = {"0", "1", "2", "3", "4",          "5", "6", "7", "8", "9", "a", "B", "C", "D", "E", "F"}; /*** Method Name: Generatepassword * Method Action: TODO encrypts String * Creator: Jxufe Hehaiyang * created: 2015-1-20 09:58:05 * @param @paraminputstring *@param @return* return value type: String *@throws     */     Public Staticstring Generatepassword (String inputstring) {returnencodeByMD5 (inputstring); }            /*** Method Name: ValidatePassword * Method Action: TODO verifies that the password entered is correct * created by: Jxufe Hehaiyang * Created: 2015-1-20 09:57: *@param @paramPassword *@param @paraminputstring *@param @return* Return value type: Boolean *@throws     */     Public Static BooleanValidatePassword (string password, string inputstring) {if(Password.equals (encodeByMD5 (inputstring))) {return true; } Else{              return false; }      }      /*** Method Name: EncodeByMD5 * Method Action: TODO MD5 string Encryption * created by: Jxufe Hehaiyang * created: 2015-1-20 09:57:38 * @param @paramoriginstring *@param @return* return value type: String *@throws     */    Private Staticstring encodeByMD5 (String originstring) {if(Originstring! =NULL){              Try{                  //creates a summary of information with the specified algorithm nameMessageDigest MD = messagedigest.getinstance ("MD5"); //The summary is last updated with the specified byte array, and the summary calculation is completed                byte[] results =md.digest (Originstring.getbytes ()); //turns the resulting byte array into a string returnString resultstring =bytearraytohexstring (results); returnresultstring.touppercase (); } Catch(Exception ex) {ex.printstacktrace (); }          }          return NULL; }            /*** Method Name: Bytearraytohexstring * Method function: TODO convert byte array to hexadecimal String * Creator: Jxufe Hehaiyang * created on: 2015-1-20 Afternoon 09:57:29 *@param @paramb *@param @return* return value type: String *@throws     */    Private StaticString bytearraytohexstring (byte[] b) {StringBuffer RESULTSB=NewStringBuffer ();  for(inti = 0; i < b.length; i++) {resultsb.append (bytetohexstring (b[i)); }          returnresultsb.tostring (); }            /*** Method Name: Bytetohexstring * Method Action: TODO converts a byte into a 16-binary string * Creator: Jxufe Hehaiyang * Created: 2015-1-20 pm 09 : 57:17 *@param @paramb *@param @return* return value type: String *@throws     */    Private StaticString bytetohexstring (byteb) {          intn =b; if(N < 0) n= 256 +N; intD1 = n/16; intD2 = n 16; returnHEXDIGITS[D1] +HEXDIGITS[D2]; }  }
MD5 16-bit cryptographic tool class

2. Call method

MD5 is a one-way encryption algorithm that can only be encrypted.

When used, the input string can be MD5 encrypted and then compared to the password.

    New cipherutil ();      

Beginner 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.