MD5 Encrypted Java edition

Source: Internet
Author: User
Tags md5 digest

MD5 is a secure hashing algorithm, input two different plaintext will not get the same output value, according to the output value, can not get the original plaintext, that is, its process is irreversible; So to decrypt MD5 there is no ready-made algorithm, only with the poor lifting method, the possible clear text, with the MD5 algorithm hash, The resulting hash value and the original data form a one-to-one mapping table, through the comparison in the table than the crack cipher MD5 algorithm hash value, by matching from the mapping table to find out the original plaintext corresponding to the decryption password.

Importjava.security.MessageDigest; Public classPassword {Private Final StaticString[] hexdigits = {"0", "1", "2", "3", "4", "5",   "6", "7", "8", "9", "a", "B", "C", "D", "E", "F" }; //hexadecimal number-to-character mapping array   /**encrypt the inputstring .*/  Public Staticstring Createpassword (String inputstring) {returnencodeByMD5 (inputstring);} /*** Verify that the password entered is correct * *@paramPassword * Real password (encrypted true password) *@paraminputstring * Input String *@returnvalidation result, Boolean type*/  Public Static BooleanAuthenticatepassword (string password, string inputstring) {if(Password.equals (encodeByMD5 (inputstring))) {return true; } Else {   return false; } }    /**MD5 encoding A string*/ 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; } /*** Rotate byte array to hexadecimal string * *@paramb * byte array *@returnhexadecimal string*/ Private StaticString bytearraytohexstring (byte[] b) {StringBuffer RESULTSB=NewStringBuffer ();  for(inti = 0; i < b.length; i++) {resultsb.append (bytetohexstring (b[i)); }  returnresultsb.tostring ();} /*** Converts a byte into a string of 16 binary form*/ Private StaticString bytetohexstring (byteb) {intn =b; if(N < 0) n= 256 +N; intD1 = n/16; intD2 = n 16; returnHEXDIGITS[D1] +HEXDIGITS[D2];}  Public Static voidMain (string[] args) {String password= Password.createpassword ("Huangcheng"); System.out.println ("String for Huangcheng with MD5 digest:" +password); String inputstring= "Huangchenghuangcheng"; System.out.println ("Does the Huangchenghuangcheng match the password?" "    +Password.authenticatepassword (Password, inputstring)); InputString= "Huangcheng"; System.out.println ("Does the Huangcheng match the password?" "    +Password.authenticatepassword (Password, inputstring)); }    }

MD5 Encrypted Java edition

Related Article

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.