A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© juanjuan 中级黑马   /  2016-10-8 01:16  /  816 人查看  /  1 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

md5加密的方法代码,在程序开发中很多重要的信息,密码等都要用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 < b.length; i++) {

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

    }

    return resSb.toString();

  }



  private static String byteToHexString(byte b) {

    int n = b;

    if (n < 0)

      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"));

  }

}

1 个回复

倒序浏览
楼主,你确定这么简单???
来自宇宙超级黑马专属苹果客户端来自宇宙超级黑马专属苹果客户端
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马