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