Implement MD5 encryption in JSP

Source: Internet
Author: User
**
* Class Name: MD5Digest <br>
* Description: md5 public parameters used for password encryption <br>
* Written on: 2001/03/05 <br>
* Modifier: <br>
* Modify information: <br>
* @ Author edgarlo edgarlo@china.com
* @ Version 1.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));

Return _ digestBuffer. toString ();
}
Public String toHex (byte one ){
String HEX = "0123456789 ABCDEF ";
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 Of the MD5 Algorithm
@ Author: Topcat Tuppin
Last Modified: 10, Mar, 2001
**************************************** *********/
Package beartool;
Import java. lang. reflect .*;
/*************************************** **********
The md5 class implements RSA Data Security, Inc. is submitted to the IETF
The MD5 message-digest algorithm in RFC1321.
**************************************** *********/

Public class MD5 {
/* The following S11-S44 is actually a 4*4 matrix, which is implemented with # define in the original C implementation,
Here, implementing them as static final indicates read-only and can be multiple in the same process space.
Share instances */
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 };
/* The following three members are the three core data used in the MD5 calculation process. In the original C implementation
Defined in the MD5_CTX Structure

*/
Private long [] state = new long [4]; // state (ABCD)
Private long [] count = new long [2]; // number of bits, modulo 2 ^ 64 (lsb first)
Private byte [] buffer = new byte [64]; // input buffer

/* DigestHexStr is the only public member of MD5 and is the latest calculation result
Hexadecimal ASCII representation.
*/
Public String digesthexstr;

/* Digest, which is a binary representation of the latest computation result, indicating the MD5 value of bits.
*/
Private byte [] digest = new byte [16];

/*
Getmd5ofstr is the main public method of MD5 type. The entry parameter is the string you want to perform MD5 conversion.
The returned result is the transformed result, 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 <16; I ++ ){
DigestHexStr + = byteHEX (digest);
}
Return digestHexStr;

}
// This is the standard constructor of the MD5 class. JavaBean requires a public constructor without parameters.
Public MD5 (){
Md5Init ();

Return;
}

/* Md5Init is an initialization function that initializes core variables and loads Standard magic numbers */
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, and I are four basic MD5 functions. In the original MD5 C implementation, because they are
Simple bitwise operations may implement them into macros for efficiency reasons. In java, we use them
The private method is implemented, and the name is kept 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 the next step transformation
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 <s) | (INT) A >>> (32-S ));
A + = B;
Return;
}

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;
}
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;
}
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;
}
/*
Md5Update is the main calculation process of MD5, inbuf is the byte string to be transformed, inputlen is the length, this
The function is called by getMD5ofStr. before calling the function, you need to call md5init.
*/
Private void 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 same times as possible.
If (inputlen> = partlen ){
Md5Memcpy (buffer, inbuf, index, 0, partLen );
Md5Transform (buffer );

For (I = partLen; I + 63 <inputLen; I ++ = 64 ){

Md5Memcpy (block, inbuf, 0, I, 64 );
Md5Transform (block );
}
Index = 0;

} Else

I = 0;

/// * Buffer remaining input */
Md5Memcpy (buffer, inbuf, index, I, inputLen-I );

}

/*
Md5Final sorting and entering output results
*/
Private void md5Final (){
Byte [] bits = new byte [8];
Int index, padLen;

/// * Save number of bits */
Encode (bits, count, 8 );

/// * Pad out to 56 mod 64.
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 the byte array used internally. The len Length is
Copy 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 conversion program called by md5Update, and block is the original byte of the block.
*/
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);/* 10 */
C = FF (c, d, a, B, x [10], S13, 0xffff5bb1L);/* 11 */
B = FF (B, c, d, a, x [11], S14, 0x895cd7beL);/* 12 */
A = ff (a, B, c, d, X [12], S11, 0x6b901122l);/* 13 */
D = ff (D, a, B, c, X [13], S12, 0xfd987193l);/* 14 */
C = ff (c, d, A, B, X [14], S13, 0xa679438el);/* 15 */
B = ff (B, c, d, A, X [15], S14, 0x49b40821l);/* 16 */

/* Round 2 */
A = Gg (a, B, c, d, X [1], S21, 0xf61e2562l);/* 17 */
D = Gg (D, a, B, c, X [6], s22, 0xc040b340l);/* 18 */
C = GG (c, d, a, B, x [11], S23, 0x265e5a51L);/* 19 */
B = GG (B, c, d, a, x [0], S24, 0xe9b6c7aaL);/* 20 */
A = GG (a, B, c, d, x [5], S21, 0xd62f105dL);/* 21 */
D = GG (d, a, B, c, x [10], S22, 0x2441453L);/* 22 */
C = GG (c, d, a, B, x [15], S23, 0xd8a1e681L);/* 23 */
B = GG (B, c, d, a, x [4], S24, 0xe7d3fbc8L);/* 24 */
A = GG (a, B, c, d, x [9], S21, 0x21e1cde6L);/* 25 */
D = Gg (D, a, B, c, X [14], s22, 0xc33707d6l);/* 26 */
C = Gg (c, d, A, B, X [3], S23, 0xf4d50d87l);/* 27 */
B = Gg (B, c, d, A, X [8], S24, 0x455a14edl);/* 28 */
A = Gg (a, B, c, d, X [13], S21, 0xa9e3e905l);/* 29 */
D = Gg (D, a, B, c, X [2], s22, 0xfcefa3f8l);/* 30 */
C = Gg (c, d, A, B, X [7], S23, 0x676f02d9l);/* 31 */
B = Gg (B, c, d, A, X [12], S24, 0x8d2a4c8al);/* 32 */

/* Round 3 */
A = HH (a, B, c, d, x [5], S31, 0xfffa3942L);/* 33 */
D = HH (d, a, B, c, x [8], S32, 0x8771f681L);/* 34 */
C = HH (c, d, a, B, x [11], S33, 0x6d9d6122L);/* 35 */
B = HH (B, c, d, a, x [14], S34, 0xfde5380cL);/* 36 */
A = HH (a, B, c, d, x [1], S31, 0xa4beea44L);/* 37 */
D = HH (d, a, B, c, x [4], S32, 0x4bdecfa9L);/* 38 */
C = HH (c, d, A, B, X [7], s33, 0xf6bb4b60l);/* 39 */
B = HH (B, c, d, A, X [10], s34, 0xbebfbc70l);/* 40 */
A = HH (a, B, c, d, X [13], s31, 0x289b7ec6l);/* 41 */
D = HH (D, a, B, c, X [0], s32, 0xeaa1_fal);/* 42 */
C = HH (c, d, A, B, X [3], s33, 0xd4ef3085l);/* 43 */
B = HH (B, c, d, A, X [6], s34, 0x4881d05l);/* 44 */
A = HH (a, B, c, d, X [9], s31, 0xd9d4d039l);/* 45 */
D = HH (d, a, B, c, x [12], S32, 0xe6db99e5L);/* 46 */
C = HH (c, d, a, B, x [15], S33, 0x1fa27cf8L);/* 47 */
B = HH (B, c, d, a, x [2], S34, 0xc4ac5665L);/* 48 */

/* Round 4 */
A = II (a, B, c, d, x [0], S41, 0xf4292244L);/* 49 */
D = II (d, a, B, c, x [7], S42, 0x432aff97L);/* 50 */
C = II (c, d, a, B, x [14], S43, 0xab9423a7L);/* 51 */
B = II (B, c, d, a, x [5], S44, 0xfc93a039L);/* 52 */
A = II (a, B, c, d, x [12], S41, 0x655b59c3L);/* 53 */
D = II (d, a, B, c, x [3], S42, 0x8f0ccc92L);/* 54 */
C = II (c, d, a, B, x [10], S43, 0xffeff47dL);/* 55 */
B = II (B, c, d, a, x [1], S44, 0x85845dd1L);/* 56 */
A = II (a, B, c, d, x [8], S41, 0x6fa87e4fL);/* 57 */
D = II (d, a, B, c, x [15], S42, 0xfe2ce6e0L);/* 58 */
C = II (c, d, A, B, X [6], s43, 0xa3014314l);/* 59 */
B = II (B, c, d, A, X [13], s44, 0x4e0811a1l);/* 60 */
A = II (a, B, c, d, X [4], s41, 0xf7537e82l);/* 61 */
D = II (D, a, B, c, X [11], S42, 0xbd3af235l);/* 62 */
C = II (c, d, A, B, X [2], s43, 0x2ad7d2bbl);/* 63 */
B = II (B, c, d, A, X [9], s44, 0xeb86d391l);/* 64 */

State [0] + =;
State [1] + = B;
State [2] + = c;
State [3] + = d;

}

/* Encode splits the long array into byte arrays in order, because java's long type is 64bit,
Only 32 bits are removed to adapt to the use of 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& 0 xffL );
Output [j + 1] = (byte) (input>>> 8) & 0 xffl );
Output [J + 2] = (byte) (input>>> 16) & 0 xffL );
Output [j + 3] = (byte) (input>>> 24) & 0 xffL );
}
}

/* Decode combines byte arrays into long arrays in order, because java's long type is 64bit,
Only low 32bit is synthesized, and high 32bit is cleared to adapt to the use of the original C implementation.
*/
Private void Decode (long [] output, byte [] input, int len ){
Int I, j;

For (I = 0, j = 0; j <len; I ++, j + = 4)
Output= B2iu (input [J]) |
(B2iu (input [J + 1]) <8) |
(B2iu (input [j + 2]) <16) |
(B2iu (input [j + 3]) <24 );

Return;
}

/*
B2iu is a "Upgrade" program I wrote that does not consider positive and negative numbers for byte, Because Java does not have unsigned operations.
*/
Public static long b2iu (byte B ){
Return B <0? B & amp; 0x7f + 128: B;
}

/* Bytehex () is used to convert the number of bytes to the hexadecimal ASCII representation,
Because the tostring of byte in Java cannot be implemented, we do not have
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', '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 no parameter exists, run the standard Test Suite

System. out. println ("MD5 Test suite :");
System. out. println ("MD5 (/"/"):" + m. getMD5ofStr (""));
System. out. println ("MD5 (/" a/"):" + m. getMD5ofStr (""));
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]);


}

}

JSP usage

-------------------------------------------------------------------------------
<% @ Page language = 'java' %>
<Jsp: useBean id = 'omd5' scope = 'request' class = 'beartool. md5'/>

<% @ Page import = 'java. util. * '%>
<% @ Page import = 'java. SQL. * '%>
<Html>
<Body>
<%
String userid = request. getparameter ("userid"); // obtain the userid entered by the user
String Password = request. getparameter ("password"); // obtain the password entered by the user

String pwdmd5 = omd5.getmd5ofstr (password); // calculate the MD5 value.

Printwriter Rp = response. getwriter ();

Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver ");

Connection con = drivermanager. getconnection ("JDBC: ODBC: community ","","");

Statement stmt = con. createstatement ();

Resultset rs = stmt.exe cutequery ("select * from users where userid = '" + userid + "' and pwdmd5 = '" + pwdmd5 + "'");

If (Rs. Next ())
{
RP. Print ("Login OK ");

}
Else
{
RP. Print ("Login fail ");
}

Stmt. Close ();
Con. Close ();

%>

</Body>

</Html>
 

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.