Sunday, September 5, 2010

In the JSP in how MD5 encryption

Source

/ ** 
* Class name: MD5Digest
 
* Note: the password used to encrypt the md5 utility parameters
 
* Date written: 2001/03/05
 
* Modified By:
 
* Modify the information:
 
* @ Author edgarlo edgarlo@china.com 
* @ Version 1.0
 
* / 


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
__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; 
)

-------------------------------------------------- ------------------------------ 
/************************************************ 
MD5 algorithm for Java Bean 
@ Author: Topcat Tuppin 
Last Modified: 10, Mar, 2001 
*************************************************/ 
package beartool; 
import java.lang.reflect .*; 
/************************************************* 
md5 class implements the RSA Data Security, Inc. submitted to the IETF 
Of RFC1321 in the MD5 message-digest algorithm. 
*************************************************/

public class MD5 ( 
/ * Here are S11-S44 is actually a 4 * 4 matrix, in the original C implementation is to use # define to achieve, 
Here them is said to achieve a static final read-only, all in the same process space of more than 
Instance shared between * / 
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 of the MD5 computation is used in the three core data, in the original C implementation 
Structure is defined to MD5_CTX 

* / 
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 is the latest results, said the 16 hexadecimal ASCII. 
* / 
public String digestHexStr; 

/ * Digest, is the latest results of the two binary internal representation, expressed 128bit the MD5 value. 
* / 
private byte [] digest = new byte [16]; 

/ * 
getMD5ofStr is the most important public class MD5 method, you want to import parameter is the string MD5 transform 
Back to the complete transformation of the results, this result is obtained from the public members of the 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 MD5 standard constructor of this class, JavaBean requirements of a public constructor with no parameters 
public MD5 () ( 
md5Init ();

return; 

No comments:

Post a Comment