java开发之MD5加密算法的实现
时间:2022-05-13 23:44:35
先看看代码再说:
package com.b510.note;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* MD5加密
*
* @author Hongten
*
*/
public class MD5 {
public static void main(String[] args) {
System.out.println(MD5.getMD5("123456"));
}
/**
* 用md5编码后的码值
*
* @param sInput
* 明码
* @return md5加密后的密码
*/
public static String getMD5(String sInput) {
String algorithm = "";
if (sInput == null) {
return "null";
}
try {
algorithm = System.getProperty("MD5.algorithm", "MD5");
} catch (SecurityException se) {
}
MessageDigest md = null;
try {
md = MessageDigest.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
byte buffer[] = sInput.getBytes();
for (int count = 0; count < sInput.length(); count++) {
md.update(buffer, 0, count);
}
byte bDigest[] = md.digest();
BigInteger bi = new BigInteger(bDigest);
return (bi.toString(16));
}
}
运行效果:
1 186d636867f51c667893c1b1b3d96dc3
我们可以根据自己的需要,来得到自己想要的md5加密后的密文;
1 //这里的数字是可以根据需要而进行改变的
2 return (bi.toString(16));
标签:java,md5加密
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java文件操作实例详解
2023-11-25 10:29:40
![](https://img.aspxhome.com/file/2023/2/59562_0s.jpg)
Flutter Widgets之标签类控件Chip详解
2023-06-26 14:22:35
![](https://img.aspxhome.com/file/2023/0/120090_0s.jpg)
Flutter倒计时/计时器的实现代码
2023-07-01 03:50:50
![](https://img.aspxhome.com/file/2023/5/103895_0s.jpg)
在springboot中对kafka进行读写的示例代码
2023-11-26 00:48:02
![](https://img.aspxhome.com/file/2023/7/60457_0s.png)
Spring boot集成Mybatis的方法教程
2023-11-25 06:20:41
![](https://img.aspxhome.com/file/2023/2/59852_0s.png)
java中驼峰与下划线的写法互转
2023-09-21 09:47:36
Java由浅入深刨析继承
2023-11-23 08:04:24
![](https://img.aspxhome.com/file/2023/4/59564_0s.jpg)
Java内存模型详解
2023-06-21 21:04:29
![](https://img.aspxhome.com/file/2023/2/57592_0s.webp)
iOS WebView中使用webp格式图片的方法
2023-06-17 22:06:03
Android利用传感器实现微信摇一摇功能
2023-07-12 05:05:18
java实现简单石头剪刀布游戏
2023-07-20 05:31:37
![](https://img.aspxhome.com/file/2023/8/57628_0s.jpg)
详解Java代码常见优化方案
2023-11-29 03:13:04
分享几个Java工作中实用的代码优化技巧
2023-11-28 12:04:50
![](https://img.aspxhome.com/file/2023/0/59890_0s.webp)
Java看完秒懂版熔断和降级的关系
2023-11-06 01:12:13
![](https://img.aspxhome.com/file/2023/9/58919_0s.jpg)
Java对zip,rar,7z文件带密码解压实例详解
2023-11-29 05:08:32
![](https://img.aspxhome.com/file/2023/4/60674_0s.png)
Java关键字instanceof的两种用法实例
2023-07-31 06:24:50
Java 判断字符串中是否包含中文的实例详解
2023-11-06 13:17:18
![](https://img.aspxhome.com/file/2023/3/58913_0s.png)
Java 使用IO流实现大文件的分割与合并实例详解
2023-08-23 09:33:33
![](https://img.aspxhome.com/file/2023/8/58078_0s.png)
Linux下g++编译与使用静态库和动态库的方法
2023-06-21 13:41:46
![](https://img.aspxhome.com/file/2023/2/68232_0s.jpg)
IDEA 2021.2 激活教程及启动报错问题解决方法
2023-11-14 14:10:27
![](https://img.aspxhome.com/file/2023/5/59345_0s.png)