How to implement MD5 encryption method in JSP _javascript skills

Source: Internet
Author: User
Tags md5 md5 encryption odbc stmt stringbuffer


In the development of various application systems, often need to store user information, many places have to store user passwords, and the user password directly stored in the server is obviously unsafe, this article briefly describes how to achieve MD5 encryption in JSP method, hope to be a good point.



(i) Brief introduction to the message summary
A message digest is a digital fingerprint of a block of data. That is, a data block of any length is computed to produce a unique fingerprint (for SHA1 is to produce a binary array of 20 bytes). A message digest is a technique that is used in conjunction with message authentication codes to ensure message integrity. The main use of one-way hash function algorithm, can be used to verify the integrity of the message, and through the hash password directly in the form of text preservation, and so on, the current widely used algorithms are MD4, MD5, SHA-1.



The message digest has two basic properties:



Two different messages are difficult to generate the same summary



It is difficult to generate a message for the specified digest, but the specified summary can be inferred from the message.



Java source


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 (); 
} 
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 submission to the IETF
The MD5 message-digest algorithm in RFC1321.
*************************************************/
Public class MD5 {
/* The following S11-S44 are actually a 4*4 matrix, which is implemented in #define in the original C implementation.
Implementing them here as static final means that read-only, can be multiple in the same process space
Sharing between 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, 0, 0 };
/* The following three members are the three core data used in the MD5 calculation process, in the original C implementation.
Is 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, the latest calculation result
Hexadecimal ASCII representation.
*/
Public String digestHexStr;
  
/* digest, which is the binary internal representation of the latest calculation result, indicating the MD5 value of 128bit.
*/
Private byte[] digest = new byte[16];
  
/*
getMD5ofStr is the main public method of class MD5, the entry parameter is the string you want to transform MD5
The result of the transformation is returned. This result 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[i]);
}
Return digestHexStr;
}
// This is the standard constructor for the MD5 class. JavaBeans require a public constructor with no arguments.
Public MD5() {
md5Init();
Return;
}
/* md5Init is an initialization function that initializes the core variables 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 are 4 basic MD5 functions, in the original MD5 C implementation, since they are
Simple bit operations, which may be implemented as macros for efficiency reasons. In Java, we put them
The implementation becomes a private method, and the name remains 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 further 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 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 of MD5, inbuf is the byte string to be transformed, inputlen is the length, this
The function is called by getMD5ofStr. It needs to call md5init before calling, so design it as private.
*/
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 many 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 finishing and filling out the output
*/
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 for an internally used byte array, starting with the inpos of input and len length
Byte copy 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 transformation program, with md5Update call, block is the raw 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, 0xeaa127faL); /* 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] += a;
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 remove 32bit 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] >>> 16) & 0xffL);
Output[j + 3] = (byte)((input[i] >>> 24) & 0xffL);
}
}
/*Decode combines byte arrays into long arrays in order, because java's long type is 64bit.
Only synthesis low 32bit, high 32bit clear to adapt to 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[i] = b2iu(input[j]) |
(b2iu(input[j + 1]) << 8) |
(b2iu(input[j + 2]) << 16) |
(b2iu(input[j + 3]) << 24);
Return;
}
  
/*
B2iu is a "up" program that I wrote to follow the principle of not considering the sign, because java has no unsigned operation.
*/
Public static long b2iu(byte b) {
Return b < 0 ? b & 0x7F + 128 : b;
}
  
/*byteHEX(), used to convert a byte type number to a hexadecimal ASCII representation.
Because the toString of byte in java can't achieve this, we don't have C in 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.* '%> 


The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.


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.