博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MD5加密
阅读量:4704 次
发布时间:2019-06-10

本文共 1827 字,大约阅读时间需要 6 分钟。

import java.security.MessageDigest;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;public class MD5 {    private final Log logger = LogFactory.getLog(MD5.class);    private String inStr;    private MessageDigest md5;    /* 下面是构造函数 */    public MD5(String inStr) {        this.inStr = inStr;        try {            this.md5 = MessageDigest.getInstance("MD5");        } catch (Exception e) {            logger.fatal("", e);        }    }    /* 下面是关键的md5算法 */    public String compute() {        char[] charArray = this.inStr.toCharArray();        byte[] byteArray = new byte[charArray.length];        for (int i = 0; i < charArray.length; i++)            byteArray[i] = (byte) charArray[i];        byte[] md5Bytes = this.md5.digest(byteArray);        StringBuffer hexValue = new StringBuffer();        for (int i = 0; i < md5Bytes.length; i++) {            int val = ((int) md5Bytes[i]) & 0xff;            if (val < 16)                hexValue.append("0");            hexValue.append(Integer.toHexString(val));        }        return hexValue.toString();    }    /* 下面是关键的md5算法 */    public String computeWithUTF8() {        try {            byte[] btInput = this.inStr.getBytes("UTF-8"); // 必须制定utf-8字符集。            MessageDigest mdInst = MessageDigest.getInstance("MD5");            mdInst.update(btInput);            byte[] md = mdInst.digest();            StringBuffer sb = new StringBuffer();            for (int i = 0; i < md.length; i++) {                int val = ((int) md[i]) & 0xff;                if (val < 16) {                    sb.append("0");                }                sb.append(Integer.toHexString(val));            }            return sb.toString();        } catch (Exception e) {            return null;        }    }}

 

转载于:https://www.cnblogs.com/lxaic/p/5646369.html

你可能感兴趣的文章
Struts2自定义拦截器
查看>>
SHA和MD5的Salt
查看>>
Aspose------导入Excel
查看>>
python之面向对象
查看>>
vi命令
查看>>
文件操作(把源文件拷贝到目标文件路径下)
查看>>
spring-mvc报红错误
查看>>
springboot 整合redis 以及redis的简单使用
查看>>
BZOJ 3884 上帝与集合的正确用法(扩展欧拉定理)
查看>>
心肝脾肺肾的功能
查看>>
[转]不看这篇也许会节省你十分钟,但是却会耽误你的一辈子
查看>>
Linux实战教学笔记08:Linux 文件的属性(上半部分)
查看>>
666
查看>>
[转载]Java(Android)对文件全文生成MD5摘要
查看>>
String.replaceAll()方法替换字符串中的反斜杠(\)
查看>>
Go语言实战 - revel框架教程之MongDB的最佳搭档revmgo
查看>>
Hive简单安装
查看>>
设计模式学习之工厂方法(Factory Method,创建型模式)(2)
查看>>
iOS蓝牙开发(一)蓝牙相关基础知识
查看>>
Collection接口源码解读
查看>>