通用32位MD5算法总结:MD5Util

9 篇文章 0 订阅
3 篇文章 0 订阅

1.小写:

比如

abcdef
e80b5017098950fc58aad83c8c14978e


 public static String HEXAndMd5(String plainText) {
    try {
      MessageDigest md = MessageDigest.getInstance("MD5");
      try {
        md.update(plainText.getBytes("UTF8"));
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      }
      byte b[] = md.digest();
      int i;
      StringBuffer buf = new StringBuffer(200);
      for (int offset = 0; offset < b.length; offset++) {
        i = b[offset] & 0xff;
        if (i < 16) buf.append("0");
        buf.append(Integer.toHexString(i));
      }
      return buf.toString();
    } catch (NoSuchAlgorithmException e) {
      LoggerUtil.error("Md5加密异常", e);
      return null;
    }
  }


 



1.大写:

比如

abcdef
E80B5017098950FC58AAD83C8C14978E


   /**
     * 获得MD5加密字符串
     *
     * @param source 源字符串
     *
     * @return 加密后的字符串
     *
     */
    public static String getMD5(String source) {
        String mdString = null;
        if (source != null) {
            try {
//关键是这句
                mdString = getMD5(source.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return mdString;
    }

    /**
     * 获得MD5加密字符串
     *
     * @param source 源字节数组
     *
     * @return 加密后的字符串
     */
    public static String getMD5(byte[] source) {
        String s = null;
        char [] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
                '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        final int temp = 0xf;
        final int arraySize = 32;
        final int strLen = 16;
        final int offset = 4;
        try {
            java.security.MessageDigest md = java.security.MessageDigest
                    .getInstance("MD5");
            md.update(source);
            byte [] tmp = md.digest();
            char [] str = new char[arraySize];
            int k = 0;
            for (int i = 0; i < strLen; i++) {
                byte byte0 = tmp[i];
                str[k++] = hexDigits[byte0 >>> offset & temp];
                str[k++] = hexDigits[byte0 & temp];
            }
            s = new String(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return s;
    }










  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值