Posted on 2007-02-06 17:15
lixw 阅读(150)
评论(0) 编辑 收藏
1
import
java.security.
*
;
2
3
public
class
MD5
{
4
5
public
final
static
String getMD5(String s)
{
6
char
hexDigits[]
=
{
7
'
0
'
,
'
1
'
,
'
2
'
,
'
3
'
,
8
'
4
'
,
'
5
'
,
'
6
'
,
'
7
'
,
9
'
8
'
,
'
9
'
,
'
a
'
,
'
b
'
,
10
'
c
'
,
'
d
'
,
'
e
'
,
'
f
'
}
;
11
try
{
12
byte
[] strTemp
=
s.getBytes();
13
MessageDigest mdTemp
=
MessageDigest.getInstance(
"
MD5
"
);
14
mdTemp.update(strTemp);
15
byte
[] md
=
mdTemp.digest();
16
int
j
=
md.length;
17
char
str[]
=
new
char
[j
*
2
];
18
int
k
=
0
;
19
for
(
int
i
=
0
; i
<
j; i
++
)
{
20
byte
byte0
=
md[i];
21
str[k
++
]
=
hexDigits[byte0
>>>
4
&
0xf
];
22
str[k
++
]
=
hexDigits[byte0
&
0xf
];
23
}
24
return
new
String(str);
25
}
catch
(Exception e)
{
26
return
null
;
27
}
28
}
29
30
public
static
void
main(String[] args)
{
31
32
System.out.print(MD5.getMD5(
"
abcd
"
));
33
}
34
}
35