How to implement MD5 encryption in JSP

Source: Internet
Author: User

Java source Code

    /** * Class Name:md5digest<br> * Description: MD5 common parameters used for password encryption <br> * Date written:2001/03/05<br> * Modifier:<br> * Modify information:  <br> * @authoredgarlo [email protected] * @version1 .0<br> */import java.security.MessageDigest;  Import java.security.NoSuchAlgorithmException;  public class Md5digest {private messagedigest __md5 = null;  Private StringBuffer __digestbuffer = null;  Public Md5digest () throws nosuchalgorithmexception {__md5 = messagedigest.getinstance ("MD5");  __digestbuffer = new StringBuffer ();  } Public String Md5crypt (string s) {__digestbuffer.setlength (0);  byte abyte0[] = __md5.digest (S.getbytes ());  for (int i = 0; i < abyte0.length; i++) __digestbuffer.append (Tohex (Abyte0[i]));  return __digestbuffer.tostring ();  The public String Tohex (byte one) {String hex= "0123456789ABCDEF";  Char[] Result=new char[2];  Result[0]=hex.charat ((One & 0xf0) >> 4);  Result[1]=hex.charat (one & 0x0f);  String Mm=new string (result);  return mm; }  } 

-------------------------------------------------------------------------------

Java Bean for/************************************************ MD5 algorithm @author: TopCat tuppin Last modified:10,mar,2001 * * *  /package Beartool;  Import java.lang.reflect.*; The/************************************************* MD5 class implements RSA Data Security, Inc. in the RFC1321 submitted to the IETF MD5  Message-digest algorithm. /public class MD5 {/* below these s11-s44 are actually a 4*4 matrix, in the original C implementation is a # define  Implemented, where they are implemented as static final is a read-only, tangent can be shared between multiple instance within the same process space */static final int S11 = 7;  static final int S12 = 12;  static final int S13 = 17;  static final int S14 = 22;  static final int S21 = 5;  static final int S22 = 9;  static final int S23 = 14;  static final int S24 = 20;  static final int S31 = 4;  static final int S32 = 11;  static final int S33 = 16;  static final int S34 = 23;  static final int S41 = 6;  static final int S42 = 10;  static final int S43 = 15;  static final int S44 = 21; Static final byte[] PADDING = {-128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; /* The following three members are the 3 core data used in the MD5 calculation process and are defined in the original C implementation in the MD5_CTX structure */private long[] state = new long[4];//State (ABCD) Private lon G[] Count = new long[2];//number of bits, modulo 2^64 (LSB first) private byte[] buffer = new BYTE[64];  The input buffer/* DIGESTHEXSTR is the only public member of MD5, which is the 16-in-one ASCII representation of the latest calculation result.  */public String digesthexstr;  /* Digest, which is the 2-binary internal representation of the latest calculation, representing the MD5 value of 128bit.  */private byte[] Digest = new BYTE[16];  /* GETMD5OFSTR is the primary public method of class MD5, and the entry parameter is the result of the conversion of the string you want to MD5 transform, which is obtained from the public member Digesthexstr.  */public string Getmd5ofstr (string inbuf) {md5init ();  Md5update (Inbuf.getbytes (), inbuf.length ());  Md5final ();  Digesthexstr = "";  for (int i = 0; i < i++) {digesthexstr + = Bytehex (Digest[i]);  } return DIGESTHEXSTR; }//This is the standard constructor for this class of MD5, and JavaBean requires a public and no parameter constructCreate function Public MD5 () {md5init ();  Return  }/* Md5init is an initialization function that initializes the core variable and loads the standard magic number */private void Md5init () {count[0] = 0L;  COUNT[1] = 0L;  * Load Magic Initialization constants.  State[0] = 0x67452301l;  STATE[1] = 0xefcdab89l;  STATE[2] = 0x98badcfel;  STATE[3] = 0x10325476l;  Return }/* F, G, H, I is 4 basic MD5 functions, in the original MD5 C implementation, because they are simple bit operations, may be for the sake of efficiency to implement them into a macro, in Java, we implemented them into the private method, the name maintained in the original C. */Private long F (long x, long y, long Z) {return (x & y) |  ((~x) & Z); } Private Long G (long x, long y, long Z) {return (X & Z) |  (Y & (~z));  } Private Long H (long x, long y, long z) {return x ^ y ^ z; } Private Long I (long x, long y, long z) {return y ^ (x |  (~z));  }/* Ff,gg,hh and II will call F,g,h,i for a near-step transform FF, GG, HH, and II Transformations for Rounds 1, 2, 3, and 4.  Rotation is separate from addition to prevent recomputation.  */Private long FF (long A, long B, long C, long D, long X, long s, long AC) {a + = F (b, C, D) + x + ac; A = ((int) a <&Lt s) |  ((int) a >>> (32-s));  A + = B;  return A;  } Private Long GG (long A, long B, long C, long D, long X, long s, long AC) {a + = G (b, C, D) + x + ac; A = ((int) a << s) |  ((int) a >>> (32-s));  A + = B;  return A;  } Private Long HH (long A, long B, long C, long D, long X, long s, long AC) {a + = H (b, C, D) + x + ac; A = ((int) a << s) |  ((int) a >>> (32-s));  A + = B;  return A;  } Private Long II (long A, long B, long C, long D, long X, long s, long AC) {a + = I (b, C, D) + x + ac; A = ((int) a << s) |  ((int) a >>> (32-s));  A + = B;  return A; }/* Md5update is the main calculation process for MD5, Inbuf is the byte string to transform, Inputlen is the length, this function is called by GETMD5OFSTR, call Md5init before calling, so it is designed to be private */private VO  ID md5update (byte[] inbuf, int inputlen) {int I, index, Partlen;  byte[] block = new BYTE[64];  index = (int) (Count[0] >>> 3) & 0x3F; /* Update Number of bits */if ((count[0] + = (Inputlen << 3)) < (Inputlen << 3))  count[1]++;  COUNT[1] + = (inputlen >>> 29);  Partlen = 64-index;  Transform as many times as possible.  if (Inputlen >= partlen) {md5memcpy (buffer, INBUF, index, 0, Partlen);  Md5transform (buffer);  for (i = Partlen; i + < Inputlen; i + = +) {md5memcpy (block, inbuf, 0, I, 64);  Md5transform (block);  } index = 0;  } else i = 0;  * Buffer remaining input */md5memcpy (buffer, INBUF, index, I, inputlen-i);  }/* Md5final collate and fill out the results */private void md5final () {byte[] bits = new BYTE[8];  int index, Padlen;  * Save Number of bits */Encode (BITS, count, 8);  * Pad out to 64 mod.  index = (int) (Count[0] >>> 3) & 0x3f; Padlen = (Index < 56)?  (56-index): (120-index);  Md5update (PADDING, Padlen);  * Append Length (before padding) */md5update (bits, 8);  * Store State in Digest */Encode (Digest, State, 16); }/* md5memcpy is a block copy function of an internally used byte array, starting from the inpos of input to copy len length bytes to the outpos position of output */private VoiD md5memcpy (byte[] output, byte[] input, int outpos, int inpos, int len) {int i;  for (i = 0; i < len; i++) Output[outpos + i] = Input[inpos + i]; }/* Md5transform is the MD5 Core transform program, with Md5update called, Block is the raw byte of chunked */private void Md5transform (byte block[]) {Long A = State[0  ], B = state[1], c = state[2], d = state[3];  long[] x = new LONG[16];  Decode (x, block, 64); /* Round 1 */a = FF (A, B, C, D, X[0], S11, 0xd76aa478l); /* 1 */d = FF (d, a, B, C, x[1], S12, 0xe8c7b756l); /* 2 */c = FF (c, D, a, B, x[2], S13, 0X242070DBL); /* 3 */b = FF (b, C, D, A, x[3], S14, 0xc1bdceeel); /* 4 */a = FF (A, B, C, D, X[4], S11, 0XF57C0FAFL); /* 5 */d = FF (d, a, B, C, x[5], S12, 0x4787c62al); /* 6 */c = FF (c, D, a, B, x[6], S13, 0xa8304613l); /* 7 */b = FF (b, C, D, A, x[7], S14, 0xfd469501l); /* 8 */a = FF (A, B, C, D, X[8], S11, 0x698098d8l); /* 9 */d = FF (d, a, B, C, x[9], S12, 0X8B44F7AFL); /* Ten */c = FF (c, D, a, B, x[10], S13, 0xffff5bb1l); /* each */b = FF (b, C, D, A, x[11], S14, 0x895cd7bel); /* */A = FF (A, B, C, D, X[12], S11, 0x6b901122l); /* */d = FF (d, a, B, C, x[13], S12, 0xfd987193l); /* */C = FF (c, D, a, B, x[14], S13, 0xa679438el); /* */b = FF (b, C, D, A, x[15], S14, 0x49b40821l); /* * */* Round 2 */a = GG (A, B, C, D, X[1], S21, 0xf61e2562l); /* * +/d = GG (d, a, B, C, x[6], S22, 0xc040b340l); /* */C = GG (c, D, a, B, x[11], S23, 0x265e5a51l); /* * +/b = GG (b, C, D, A, x[0], S24, 0xe9b6c7aal); /* */A = GG (A, B, C, D, X[5], S21, 0XD62F105DL); /* */d = GG (d, a, B, C, x[10], S22, 0x2441453l); /* */C = GG (c, D, a, B, x[15], S23, 0xd8a1e681l); /* * */b = GG (b, C, D, A, x[4], S24, 0xe7d3fbc8l); /* + * a = GG (A, B, C, D, X[9], S21, 0x21e1cde6l); /* */d = GG (d, a, B, C, x[14], S22, 0xc33707d6l); /* + */c = GG (c, D, a, B, x[3], S23, 0xf4d50d87l); /* */b = GG (b, C, D, A, x[8], S24, 0x455a14edl); /* * * a = GG (A, B, C, D, X[13], S21, 0xa9e3e905l); /* 29 */ D = GG (d, a, B, C, x[2], S22, 0xfcefa3f8l); /* * +/C = GG (c, D, a, B, x[7], S23, 0x676f02d9l); /* */b = GG (b, C, D, A, x[12], S24, 0x8d2a4c8al); /* * */* Round 3 */a = HH (A, B, C, D, X[5], S31, 0xfffa3942l); /* */d = HH (d, a, B, C, x[8], S32, 0x8771f681l); /* */C = HH (c, D, a, B, x[11], S33, 0x6d9d6122l); /* * +/b = HH (b, C, D, A, x[14], S34, 0xfde5380cl); /* */A = HH (A, B, C, D, X[1], S31, 0xa4beea44l); /* PNS */d = HH (d, a, B, C, x[4], S32, 0x4bdecfa9l); /* + */c = HH (c, D, a, B, x[7], S33, 0xf6bb4b60l); /* * All-in-B = HH (b, C, D, A, x[10], S34, 0xbebfbc70l); /* + * a = HH (A, B, C, D, X[13], S31, 0x289b7ec6l); /* */d = HH (d, a, B, C, x[0], S32, 0xeaa127fal); /* * */C = HH (c, D, a, B, x[3], S33, 0xd4ef3085l); /* + */b = HH (b, C, D, A, x[6], S34, 0x4881d05l); /* */A = HH (A, B, C, D, X[9], S31, 0xd9d4d039l); /* */d = HH (d, a, B, C, x[12], S32, 0xe6db99e5l); /* * +/C = HH (c, D, a, B, x[15], S33, 0x1fa27cf8l); /* */b = HH (b, C, D, A, x[2], S34, 0xc4ac5665l); /* * */* Round 4 */a = II (A, B, C, D, X[0], S41, 0xf4292244l); /* */d = II (d, a, B, C, x[7], S42, 0x432aff97l); /* */C = II (c, D, a, B, x[14], S43, 0xab9423a7l); /* Wuyi */b = II (b, C, D, A, x[5], S44, 0xfc93a039l); /* */A = II (A, B, C, D, X[12], S41, 0x655b59c3l); /* +/d = II (d, a, B, C, x[3], S42, 0x8f0ccc92l); /* + */c = II (c, D, a, B, x[10], S43, 0XFFEFF47DL); /* * */b = II (b, C, D, A, x[1], S44, 0x85845dd1l); /* * * * a = II (A, B, C, D, X[8], S41, 0X6FA87E4FL); /* (+/-)/d = II (d, a, B, C, x[15], S42, 0xfe2ce6e0l); /* + +/c = II (c, D, a, B, x[6], S43, 0xa3014314l); /* *//b = II (b, C, D, A, x[13], S44, 0x4e0811a1l); /* * a = II (A, B, C, D, X[4], S41, 0xf7537e82l); /*----*/d = II (d, a, B, C, x[11], S42, 0xbd3af235l); /* * +/C = II (c, D, a, B, x[2], S43, 0X2AD7D2BBL); /* N/b = II (b, C, D, A, x[9], S44, 0xeb86d391l);  /* * */state[0] + = A;  STATE[1] + = b;STATE[2] + = C;  STATE[3] + = D; /*encode the long array into a byte array in order, because the long type of Java is 64bit, and only the lower 32bit is removed to accommodate the original C implementation. */private void Encode (byte[] output, long[]  input, int len) {int I, J;  for (i = 0, j = 0; j < Len; i++, J + = 4) {Output[j] = (byte) (Input[i] & 0xffL);  Output[j + 1] = (byte) ((Input[i] >>> 8) & 0xffL);  Output[j + 2] = (byte) ((Input[i] >>>) & 0xffL);  Output[j + 3] = (byte) ((Input[i] >>>) & 0xffL); }}/*decode the byte array into a long array, because the long type of Java is 64bit, only synthetic low 32bit, high 32bit zeroing, to accommodate the original C implementation of the use of */private void Decode (long[] O  Utput, byte[] input, int len) {int I, J;  for (i = 0, j = 0; j < Len; i++, J + + 4) output[i] = B2iu (Input[j]) |  (B2iu (input[j + 1]) << 8) |  (B2iu (Input[j + 2]) << 16) |  (B2iu (Input[j + 3]) << 24);  Return }/* B2iu is a "liter" program I wrote that writes byte in accordance with the principle of no sign, because Java has no unsigned operation */public static long B2iu (byte b) {return B < 0? b &A mp  0x7F + 128:b; }/*bytehex (), used to convert a number of byte types to 16The binary ASCII representation, because the ToString of byte in Java cannot achieve this, we do not have C language sprintf (Outbuf, "%02x", IB) */public static String Bytehex (Byte IB  {char[] Digit = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '};  char [] ob = new char[2];  Ob[0] = digit[(ib >>> 4) & 0X0F];  OB[1] = Digit[ib & 0X0F];  string s = new string (OB);  return s;  } public static void Main (String args[]) {MD5 m = new MD5 ();  if (array.getlength (args) = = 0) {//If there are no parameters, execute the standard test suite System.out.println ("MD5 test suite:");  System.out.println ("MD5 (\" \ "):" +m.getmd5ofstr (""));  System.out.println ("MD5 (\" A\ "):" +m.getmd5ofstr ("a"));  System.out.println ("MD5 (\" Abc\ "):" +m.getmd5ofstr ("abc"));  System.out.println ("MD5 (\" Message digest\ "):" +m.getmd5ofstr ("Message digest"));  System.out.println ("MD5 (\" abcdefghijklmnopqrstuvwxyz\ "):" + m.getmd5ofstr ("abcdefghijklmnopqrstuvwxyz")); System.out.println ("MD5 (\" abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789\ "):" + m.getmd5ofstr (" AbcdefghijkLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 "));  } else System.out.println ("MD5 (" + args[0] + ") =" + M.getmd5ofstr (Args[0])); }  }

How to use in JSP

-------------------------------------------------------------------------------

    <%@ page language= ' java '%> <jsp:usebean id= ' oMD5 ' scope= ' request ' class= ' Beartool.  MD5 '/> <%@ page import= ' java.util.* '%> <%@ page import= ' java.sql.* '%> 

How to implement MD5 encryption in JSP

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.