java的md5加密类(zt)

作者:网络 来源:佚名 更新时间:2008-11-13 10:18:56 点击:
java的md5加密类

md5算法是将数据进行不可逆加密的算法有较好的安全性,在国内如寿信的安全支付平台就采用此算法。

源代码如下
/************************************************
md5 算法的java bean
last modified:10,mar,2001
*************************************************/

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;
}



/* md5init是一个初始化函数,初始化核心变量,装入标准的幻数 */
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 是4个基本的md5函数,在原始的md5的c实现中,由于它们是
简单的位运算,可能出于效率的考虑把它们实现成了宏,在java中,我们把它们
  实现成了private方法,名字保持了原来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和ii将调用f,g,h,i进行近一步变换
  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是md5的主计算过程,inbuf是要变换的字节串,inputlen是长度,这个
函数由getmd5ofstr调用,调用之前需要调用md5init,因此把它设计成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整理和填写输出结果
*/
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是一个内部使用的byte数组的块拷贝函数,从input的inpos开始把len长度的
      字节拷贝到output的outpos位置开始
*/

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是md5核心变换程序,有md5update调用,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把long数组按顺序拆成byte数组,因为java的long类型是64bit的,
  只拆低32bit,以适应原始c实现的用途
*/
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把byte数组按顺序合成成long数组,因为java的long类型是64bit的,
  只合成低32bit,高32bit清零,以适应原始c实现的用途
*/
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是我写的一个把byte按照不考虑正负号的原则的"升位"程序,因为java没有unsigned运算
*/
public static long b2iu(byte b) {
return b < 0 ? b & 0x7f + 128 : b;
}

/*bytehex(),用来把一个byte类型的数转换成十六进制的ascii表示,
 因为java中的byte的tostring无法实现这一点,我们又没有c语言中的
  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;
}
} 注册会员,创建你的web开发资料库,