返回顶部

收藏

MD5加密

更多
package com.share.barter.common;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5加密
 */
public class MD5{

    public static String MD5Encode(String strSrc, String key) {
    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(strSrc.getBytes("UTF8"));

        byte[] temp;
        temp = md5.digest(key.getBytes("UTF8"));

        return byte2hex(temp);

    } catch (NoSuchAlgorithmException e) {

        e.printStackTrace();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
/**
 * 二行制转字符串
 * @param b
 * @return
 */

    private static String byte2hex(byte[] b) {
    StringBuilder hs = new StringBuilder();
    String stmp;
    for (int n = 0; b!=null && n < b.length; n++) {
        stmp = Integer.toHexString(b[n] & 0XFF);
        if (stmp.length() == 1)
            hs.append('0');
        hs.append(stmp);
    }
    return hs.toString().toUpperCase();
}

    private static byte[] hex2byte(byte[] b) {
    if((b.length%2)!=0)
        throw new IllegalArgumentException();
    byte[] b2 = new byte[b.length/2];
    for (int n = 0; n < b.length; n+=2) {
        String item = new String(b,n,2);
        b2[n/2] = (byte)Integer.parseInt(item,16);
    }
    return b2;
}

}
//该片段来自于http://outofmemory.cn

标签:java,安全

收藏

0人收藏

支持

0

反对

0

»更多 您可能感兴趣的代码
  1. 2012-11-05 21:59:42java获得随机数代码 by 怪兽狂殴奥特曼
  2. 2014-05-28 14:01:26基于密码口令的加密与解密 by Koon.LY
  3. 2014-06-12 10:27:44校验数据的完整性 by liuyan814
  4. 2014-06-27 11:34:01数字签名 by 落叶随风
  5. 2014-09-03 12:14:09非对称加密(公钥加密,私钥解密) by clt
  6. 2014-09-07 13:06:48javaSQL by sdcool
  7. 2014-09-22 12:36:58NRZ Encoding by 法名空虚
  8. 2014-09-26 11:16:06JAVA的AES加密步骤解释 by Hugh
  9. 2014-11-08 10:55:27md5sums by Foyon
  10. 2014-06-04 11:47:41自定义 Hibernate 的 HQL 函数 by clt
  11. 2014-06-02 18:59:29CRC by Hugh

发表评论