8fff9e46c5c3b24a4b6a21dadaaa2b16aadbf64e.svn-base
952 字节
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.espeed.tool;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
//用md5对指定文本加密(采用的是32位加密)
public class Md5Code
{
public static String createMd5(String txt) throws Exception
{
String result = null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(txt.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));
}
result = buf.toString(); //md5 32bit
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return result;
}
//测试
public static void main(String[] args) throws Exception
{
System.out.println(Md5Code.createMd5("sz123456789"));
}
}