MD5 Cryptographic Algorithm Tool class

Source: Internet
Author: User
Tags md5 encryption

Many companies use MD5 to store user passwords, but when the digest MD5 value leaks out, it is still possible to obtain the original text in a different way. \

It is easy to get the original text via the following website
http://www.cmd5.com/
http://pmd5.com/

 PackageCom.bootdo.common.demo;Importjava.security.MessageDigest;Importjava.security.NoSuchAlgorithmException;ImportJava.util.Random;ImportOrg.apache.shiro.codec.Hex; Public classMd5util {/*** Normal MD5 encryption <br> * *@paramInput *@return      */     Public Staticstring MD5 (String input) {messagedigest MD5=NULL; Try{MD5= Messagedigest.getinstance ("MD5"); } Catch(nosuchalgorithmexception e) {return"Check JDK"; } Catch(Exception e) {e.printstacktrace (); return""; }          Char[] Chararray =Input.tochararray (); byte[] ByteArray =New byte[Chararray.length];  for(inti = 0; i < chararray.length; i++) Bytearray[i]= (byte) Chararray[i]; byte[] Md5bytes =md5.digest (ByteArray); StringBuffer Hexvalue=NewStringBuffer ();  for(inti = 0; i < md5bytes.length; i++) {              intval = ((int) Md5bytes[i]) & 0xFF; if(Val < 16) Hexvalue.append ("0");          Hexvalue.append (Integer.tohexstring (Val)); }          returnhexvalue.tostring (); }                                  /*** Add salt MD5 Encryption <br> * *@paramPassword *@return         */         Public Staticstring Generate (String password) {Random R=NewRandom (); StringBuilder SB=NewStringBuilder (16); Sb.append (R.nextint (99999999). Append (R.nextint (99999999)); intLen =sb.length (); if(Len < 16) {                   for(inti = 0; i < 16-len; i++) {sb.append ("0"); }} String Salt=sb.tostring (); Password= Md5hex (password +salt); Char[] cs =New Char[48];  for(inti = 0; I < 48; i + = 3) {Cs[i]= Password.charat (I/3 * 2); Charc = Salt.charat (I/3); Cs[i+ 1] =C; Cs[i+ 2] = Password.charat (I/3 * 2 + 1); }              return NewString (CS); }            /*** Check if salt is consistent with original text <br> * *@paramPassword *@paramMD5 *@return         */         Public Static BooleanVerify (string password, string md5) {Char[] CS1 =New Char[32]; Char[] CS2 =New Char[16];  for(inti = 0; I < 48; i + = 3) {Cs1[i/3 * 2] =Md5.charat (i); Cs1[i/3 * 2 + 1] = Md5.charat (i + 2); Cs2[i/3] = Md5.charat (i + 1); } String Salt=NewString (CS2); returnMd5hex (password + salt). Equals (NewString (CS1)); }            /*** Get MD5 abstract in hexadecimal string form*/          Private Staticstring Md5hex (string src) {Try{messagedigest MD5= Messagedigest.getinstance ("MD5"); byte[] bs =md5.digest (Src.getbytes ()); return NewString (NewHex (). Encode (BS)); } Catch(Exception e) {return NULL; }          }                Public Static voidMain (string[] args) {//OriginalString plaintext = "Dingsai"; //plaintext = "123456"; System.out.println ("Original:" +plaintext); System.out.println ("Normal MD5 after:" +md5util.md5 (plaintext)); //get the MD5 value after adding saltString ciphertext =md5util.generate (plaintext); System.out.println ("MD5 after adding salt:" +ciphertext); System.out.println ("Is the same string:" +md5util.verify (plaintext, ciphertext)); /*** MD5 value of one of the Dingsai strings*/string[] Tempsalt= {"C4d980d6905a646d27c0c437b1f046d4207aa2396df6af86","66db82d9da2e35c95416471a147d12e46925d38e1185c043","61A718E4C15D914504A41D95230087A51816632183732B5A" };  for(String temp:tempsalt) {System.out.println ("Is the same string:" +md5util.verify (plaintext, temp)); }      }                     }

Operation Result:

MD5 Cryptographic Algorithm Tool class

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.