MD5 encryption Tool Class

Source: Internet
Author: User

MD5 encryption Tool Class:

package cn.sniper.encrypt.util;import java.io.unsupportedencodingexception;import  java.security.messagedigest;import java.security.nosuchalgorithmexception;/** *  Cryptographic Tools Class  * The length of the &NBSP;&NBSP;*&NBSP;MD5 encryption is 32 bits  *  * sha the length of the encryption is 40 bits  *  *  @author  sniper *  */public class EncryptUtil {         /**     *  Encryption      *       *  @param  inputText     *  @return       */    public static string e (String inputtext)  {  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;MD5 (Inputtext);    }     /**     *  two times encryption, should not be able to crack it?      *      *  @param  inputtext     *  @return      */     Public static string md5andsha (String inputtext)  {         return sha (MD5 (inputtext));    }    /**  &NBSP;&NBSP;&NBSP;&NBSP;*&NBSP;MD5 Encryption      *      *  @param  inputText     *  @return      */   &NBSP;&NBSP;PUBLIC&NBSP;STATIC&NBSP;STRING&NBSP;MD5 (String inputtext)  {         return encrypt (inputtext,  "MD5");    }     /**     * sha Encryption      *       *  @param  inputText     *  @return       */   &nbsP;public static string sha (String inputtext)  {         return encrypt (inputtext,  "Sha-1");     }    /** &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;*&NBSP;MD5 or Sha-1 encryption      *       *  @param  inputText     *              content to encrypt      *  @param  algorithmName      *             Encryption Algorithm name: MD5 or SHA-1, Case insensitive      *  @return      */     Private static string encrypt (String inputtext, string algorithmname)  {         if  (inputtext == null | |   ". Equals (Inputtext.trim ())) &NBSP;{&NBsp;           throw new  IllegalArgumentException ("Please enter what you want to encrypt");        }         if  (algorithmname == null | |   ". Equals (Algorithmname.trim ()))  {             algorithmName =  "MD5";        }         String encryptText = null;         try {             Messagedigest m = messagedigest.getinstance (Algorithmname);             m.update (Inputtext.getbytes ("UTF8"));             byte s[] = m.digest ();    &Nbsp;        // m.digest (Inputtext.getbytes ("UTF8"));             return hex (s);         } catch  (nosuchalgorithmexception e)  {             e.printstacktrace ();         } catch  (unsupportedencodingexception e)  {             e.printstacktrace ();        }         return encrypttext;    }    / **     *  returns a hexadecimal string      *       *  @param  arr     *  @return      */     priVate static string hex (Byte[] arr)  {         Stringbuffer sb = new stringbuffer ();         for   (int i = 0; i < arr.length; ++i)  {             sb.append (integer.tohexstring (Arr[i] & 0xFF)  | 0x100). substring (1, 3));        }         return sb.tostring ();    }         /**     *  Testing      *       *  @param  args     */    public  static void main (String[] args)  {        // &NBSP;MD5 Encryption Test &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;STRING&NBSP;MD5_1&NBSP;=&NBSP;MD5 ("222222");      &NBSP;&NBSP;&NBSP;STRING&NBSP;MD5_2&NBSP;=&NBSP;MD5 ("Xi Big");         System.out.println (md5_1 +  "\ n"  + md5_2);                 // sha Encryption Test          string sha_1 = sha ("123456");        string  Sha_2 = sha ("Xi");         system.out.println (sha_1 +   "\ n"  + sha_2);             }}


MD5 encryption 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.