转摘自:[url]http://school.cfan.com.cn/pro/java/2007-08-29/1188351839d99193.shtml[/url]
源码
reference:
1.[url]http://mkchen.bokee.com/2353383.html[/url]MD5加密
2.[url]http://school.cfan.com.cn/pro/java/2007-08-29/1188351839d99193.shtml[/url]在JSP中如何实现MD5加密
3.[url]http://baike.baidu.com/view/7636.htm[/url]百度百科md5
4.[url]http://www.520hack.com/Article/Text2/crackjc/200905/14403.html[/url]教你如何轻松解密Md5密码
经验证没有问题可用.
源码
/**
* 类名: MD5Digest<br>
* 说明: 用来进行密码加密的md5公用参数<br>
* 编写日期: 2001/03/05<br>
* 修改者: <br>
* 修改信息: <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[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 算法的Java Bean
@author:Topcat Tuppin
Last Modified:10,Mar,2001
*************************************************/
package beartool;
import java.lang.reflect.*;
/*************************************************
md5 类实现了RSA Data Security, Inc.在提交给IETF
的RFC1321中的MD5 message-digest 算法。
*************************************************/
public class MD5 {
/* 下面这些S11-S44实际上是一个4*4的矩阵,在原始的C实现中是用#define 实现的,
这里把它们实现成为static final是表示了只读,切能在同一个进程空间内的多个
Instance间共享*/
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 };
/* 下面的三个成员是MD5计算过程中用到的3个核心数据,在原始的C实现中
被定义到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是MD5的唯一一个公共成员,是最新一次计算结果的
16进制ASCII表示.
*/
public String digestHexStr;
/* digest,是最新一次计算结果的2进制内部表示,表示128bit的MD5值.
*/
private byte[] digest = new byte[16];
/*
getMD5ofStr是类MD5最主要的公共方法,入口参数是你想要进行MD5变换的字符串
返回的是变换完的结果,这个结果是从公共成员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;
}
// 这是MD5这个类的标准构造函数,JavaBean要求有一个public的并且没有参数的构造函数
public MD5() {
md5Init();
return;
}
reference:
1.[url]http://mkchen.bokee.com/2353383.html[/url]MD5加密
2.[url]http://school.cfan.com.cn/pro/java/2007-08-29/1188351839d99193.shtml[/url]在JSP中如何实现MD5加密
3.[url]http://baike.baidu.com/view/7636.htm[/url]百度百科md5
4.[url]http://www.520hack.com/Article/Text2/crackjc/200905/14403.html[/url]教你如何轻松解密Md5密码
private static void Md5(String plainText ) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if(i<0) i+= 256;
if(i<16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
System.out.println("result: " + buf.toString());//32位的加密
System.out.println("result: " + buf.toString().substring(8,24));//16位的加密
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
经验证没有问题可用.