Java计算文本MD5加密值的方法示例
作者:秋夜雨微凉 时间:2023-11-15 13:18:48
本文实例讲述了Java计算文本MD5加密值的方法。分享给大家供大家参考,具体如下:
java计算文本MD5值,用于加密
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class GetMd5 {
public static void main(String[] args) {
String a="123";
System.out.println(getMd5(a));
}
/**
* 获取文本字段的MD5值
* @param txt
* @return
*/
public static String getMd5(String txt){
String rs = "";
String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8","9", "a", "b", "c", "d", "e", "f" };
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
byte[] b = messageDigest.digest(txt.getBytes());
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++) {
int n = b[i];
if (n < 0)
n = 256 + n;
int d1 = n / 16;
int d2 = n % 16;
resultSb.append(hexDigits[d1] + hexDigits[d2]);
}
rs = resultSb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return rs;
}
}
输出值:
202cb962ac59075b964b07152d234b70
希望本文所述对大家java程序设计有所帮助。
来源:https://blog.csdn.net/u013183865/article/details/50595746
标签:Java,MD5加密
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实现求二叉树的深度和宽度
2022-01-11 18:38:41
SpringBoot中打war包需要注意事项
2023-11-05 19:44:42
![](https://img.aspxhome.com/file/2023/6/58616_0s.png)
c#学习之30分钟学会XAML
2022-02-08 03:09:52
![](https://img.aspxhome.com/file/2023/7/89837_0s.png)
浅谈Java继承中的转型及其内存分配
2023-11-03 22:56:04
![](https://img.aspxhome.com/file/2023/7/103967_0s.png)
Java实现浪漫流星表白的示例代码
2023-04-02 14:50:35
![](https://img.aspxhome.com/file/2023/5/68715_0s.gif)
Android开发笔记之如何正确获取WebView的网页Title
2022-06-04 07:02:41
![](https://img.aspxhome.com/file/2023/0/139500_0s.jpg)
Android实现短信验证码获取自动填写功能(详细版)
2022-07-22 07:08:10
Java8中forEach语句循环一个List和Map
2021-06-02 07:03:54
探究实现Aware接口的原理及使用
2022-10-14 14:29:39
![](https://img.aspxhome.com/file/2023/0/126350_0s.jpg)
Spring AOP实现权限检查的功能
2023-08-10 06:51:14
![](https://img.aspxhome.com/file/2023/5/69285_0s.png)
Android开发新手常见的10个误区
2021-07-30 02:14:10
SpringBoot+easypoi实现数据的Excel导出
2023-04-05 12:27:19
详解Android 获取手机中微信聊天记录方法
2021-10-25 02:32:38
![](https://img.aspxhome.com/file/2023/7/122087_0s.png)
Android编程设计模式之备忘录模式详解
2023-06-29 14:55:15
![](https://img.aspxhome.com/file/2023/0/111730_0s.jpg)
Unity实现切割图集工具
2021-07-03 23:14:57
![](https://img.aspxhome.com/file/2023/4/82124_0s.jpg)
Unity3d 如何更改Button的背景色
2023-10-02 15:48:39
![](https://img.aspxhome.com/file/2023/3/122733_0s.jpg)
C#远程获取图片文件流的方法
2023-03-17 15:15:18
PopupWindow+RecyclerView实现上下滑动框功能
2023-06-24 16:38:24
Spring中的@Transactional的工作原理
2023-08-06 02:00:04
![](https://img.aspxhome.com/file/2023/8/58028_0s.webp)
mybatis 传入null值的解决方案
2023-11-23 06:54:44
![](https://img.aspxhome.com/file/2023/5/59775_0s.png)