java md5方法_java md5加密的方法代码

javamd5加密的方法代码,在程序开发中很多重要的信息,密码等都要用md5加密,什么,你还自己研究怎么写代码,不用了,直接复制过去用就可以了,省时省力。

public class MD5Encode {

private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",

"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

/**

* 转换字节数组为16进制字串

*

* @param b

*          字节数组

* @return 16进制字串

*/

public static String byteArrayToHexString(byte[] b) {

StringBuffer resSb = new StringBuffer();

for (int i = 0; i 

resSb.append(byteToHexString(b[i]));

}

return resSb.toString();

}

private static String byteToHexString(byte b) {

int n = b;

if (n 

n = 256 + n;

int d1 = n / 16;

int d2 = n % 16;

return hexDigits[d1] + hexDigits[d2];

}

public static String getMD5Str(String str) {

String resStr = "";

try {

MessageDigest md = MessageDigest.getInstance("MD5");

resStr = byteArrayToHexString(md.digest(str.getBytes()));

} catch (Exception ex) {

// PTK.inst().wWarn(PTK.getExceptionFullStr(ex));

}

return resStr;

}

public static String encodePWD(String userid, String pwd) {

return getMD5Str(userid.toUpperCase() + ":" + pwd);

}

public static String getFileMD5(String fn) {

return getFileMD5(new File(fn));

}

public static String getFileMD5(File file) {

FileInputStream fis = null;

try {

MessageDigest md = MessageDigest.getInstance("MD5");

fis = new FileInputStream(file);

byte[] buffer = new byte[8192];

int length = -1;

while ((length = fis.read(buffer)) != -1) {

md.update(buffer, 0, length);

}

return byteArrayToHexString(md.digest());

} catch (Exception ex) {

return null;

} finally {

// UnZipUtil.closeStream(fis);

}

}

public static void main(String[] args) {

System.out.println("mohwst=" + encodePWD("1", "demo112233"));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值