博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java实现字符串转换十六进制MD5值
阅读量:4315 次
发布时间:2019-06-06

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

public class Encode {

    public final static String md5(String s) {
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'A', 'B', 'C', 'D', 'E', 'F' };
        try {
            byte[] btInput = s.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

转载于:https://www.cnblogs.com/gslblog/p/6700874.html

你可能感兴趣的文章
HDU 1829/POJ 2492 A Bug's Life
查看>>
[UOJ UR#16]破坏发射台
查看>>
修改spring boot 启动logo
查看>>
《Android深度探索》第六章心得体会
查看>>
jQuery绑定键盘事件
查看>>
java中的for嵌套(一个好例子)
查看>>
MathML
查看>>
SQL 排名函数
查看>>
IIS与ApplicationPool重启检测自动化解决方案
查看>>
zabbix3.4实现邮件报警
查看>>
Xpath提取一个标签里的所有文本
查看>>
11 吐司 Toast 代码案例
查看>>
CKplayer:视频推荐和分享插件设置
查看>>
通过服务修改widgetUI
查看>>
win10连接无线网,开启移动热点,手机连接它手机一直显示获取ip地址中。
查看>>
MapReduce的倒排索引
查看>>
Heterogeneity Activity Recognition Data Set类别
查看>>
服务中的 API 网关(API Gateway)
查看>>
Android--TextView第一个单词大写
查看>>
网友给的链接
查看>>