|
楼主 |
发表于 2004-10-30 13:30:30
|
显示全部楼层
自己用蛮干的方式做出来了,很粗糙,感觉乱七八糟的,大家看看能不能帮忙改进一下。[code:1]import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Password {
//构造转换表
private static final char[] CodeTBL = {
'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','#','*',
};
//这就是整个程序的关键所在
private static String digestToPswd(byte[] digest) {
//因为下面的循环每次处理三个byte,并获得四个char,所以Password的尺寸只能是这样
char[] Password = new char[(((digest.length) / 3) * 4)];
//这个for循环的作用就是把原来的byte[]按每次六位的方式重新组合,然后从转换表中取出对应的符号,赋给Password
for (int i = 0, j = 0;
i + 3 < Password.length && j + 2 < digest.length;
j++) {
//取本次循环第一个byte的前六位,因为Java没有unsigned,所以需要用 ?: 运算来保证转换过程中二进制序列没有被改变
Password[i++] =
CodeTBL[(
((digest[j] < 0) ? (256 + digest[j]) : (int) (digest[j]))
>> 2)];
//取本次循环第一个byte的后两位,前移,让出空间,然后加上本次循环第二个byte的前四位
Password[i++] =
CodeTBL[(
((((digest[j] < 0) ? (256 + digest[j]) : (int) (digest[j]))
& 3)
<< 4)
+ (((digest[++j] < 0)
? (256 + digest[j])
: (int) (digest[j]))
>> 4))];
//取本次循环第二个byte的后四位,前移,让出空间,然后加上本次循环第三个byte的前两位
Password[i++] =
CodeTBL[(
((((digest[j] < 0) ? (256 + digest[j]) : (int) (digest[j]))
& 15)
<< 2)
+ (((digest[++j] < 0)
? (256 + digest[j])
: (int) (digest[j]))
>> 6))];
//取本次循环第三个byte的后六位
Password[i++] =
CodeTBL[(
((digest[j] < 0) ? (256 + digest[j]) : (int) (digest[j]))
& 63)];
}
return new String(Password);
}
private static MessageDigest MD;
static{
try {
MD = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
private MD5Password() {
}
public static String getPassword(byte[] input) {
MD.update(input);
return digestToPswd(MD.digest());
}
public static void main(String[] args) {
byte[] bt={1,6,55,25,34,25,78,99};
System.out.println(getPassword(bt));
}
}
[/code:1]这里还有一段利用Random类来构造转换表的代码,也麻烦大家帮忙改进一下。
[code:1]import java.util.Random;
public class BuildCodeTBL {
private final char[] CodeTBL = {
'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','#','*',
};
private char[] newCodeTBL = new char[CodeTBL.length];
private int[] TBL = new int[newCodeTBL.length];
private Random rdm = new Random();
private boolean bl;
public BuildCodeTBL() {
super();
for (int i = 0, j = 0; i < TBL.length; ) {
j = rdm.nextInt(65);
bl = false;
for (int k = 0; k <= i; k++) {
bl |= (j == TBL[k]);
}
if (!bl) {
TBL[i] = j - 1;
i++;
}
}
for (int i = 0; i < TBL.length && i < newCodeTBL.length; i++) {
newCodeTBL[i] = CodeTBL[TBL[i]];
}
}
public static void main(String[] args) {
BuildCodeTBL bct = new BuildCodeTBL();
for (int i=0;i<4;i++){
for (int j=0;j<16;j++){
System.out.print("'");
System.out.print(bct.newCodeTBL[i*16+j]);
System.out.print("',");
}
System.out.println();
}
}
}
[/code:1] |
|