首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

java md5 示范

2012-12-20 
java md5 示例import java.security.MessageDigestimport java.security.NoSuchAlgorithmException?publ

java md5 示例

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

?

public class MD5 {

public static String MD5(String inStr) throws NoSuchAlgorithmException {

MessageDigest md5 = null;

md5 = MessageDigest.getInstance("MD5");

byte[] byteArray = inStr.getBytes();

?

byte[] md5Bytes = md5.digest(byteArray);

String str=byte2hex(md5Bytes);

return str;

}

?

private static String byte2hex(byte[] md5Bytes) {

StringBuffer hexValue = new StringBuffer();

int val=0;

for (int i = 0; i < md5Bytes.length; i++) {

val = ((int) md5Bytes[i]) & 0xff;

if (val < 16)

hexValue.append("0");

hexValue.append(Integer.toHexString(val));

}

return hexValue.toString();

}

?

// 测试主函数

public static void main(String args[]) throws NoSuchAlgorithmException {

String s = new String("a");

System.out.println("原始:" + s);

System.out.println("MD5后:" + MD5(s));

}

}

热点排行