java 实现MD5加密算法的简单实例
作者:lqh 时间:2023-07-19 21:53:56
java 实现MD5加密算法的简单实例
实现代码:
import java.security.NoSuchAlgorithmException;
public class MD5HashUtil
{
private MessageDigest md = null;
private static MD5HashUtil md5 = null;
private static final char[] hexChars ={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
/**
* Constructor is private so you must use the getInstance method
*/
private MD5HashUtil() throws NoSuchAlgorithmException
{
md = MessageDigest.getInstance("MD5");
}
/**
* This returns the singleton instance
*/
public static MD5HashUtil getInstance()throws NoSuchAlgorithmException
{
if (md5 == null)
{
md5 = new MD5HashUtil();
}
return (md5);
}
public static String hashCode(String dataToHash) throws NoSuchAlgorithmException{
return getInstance().hashData(dataToHash.getBytes());
}
public static String hashCode(byte[] dataToHash) throws NoSuchAlgorithmException{
return getInstance().hashData(dataToHash);
}
public String hashData(byte[] dataToHash) {
return hexStringFromBytes((calculateHash(dataToHash))).toLowerCase();
}
private byte[] calculateHash(byte[] dataToHash)
{
md.update(dataToHash, 0, dataToHash.length);
return (md.digest());
}
public String hexStringFromBytes(byte[] b)
{
String hex = "";
int msb;
int lsb = 0;
int i;
// MSB maps to idx 0
for (i = 0; i < b.length; i++)
{
msb = ((int)b[i] & 0x000000FF) / 16;
lsb = ((int)b[i] & 0x000000FF) % 16;
hex = hex + hexChars[msb] + hexChars[lsb];
}
return(hex);
}
public static void main(String args[]) throws NoSuchAlgorithmException
{
String string = "my name is zhangli";
System.out.println(string);
System.out.println(hashCode(string));
}
}
如上代码为java语言实现md5加密算法,输出为加密后的密文!
通常将加密后的密文保存在数据库中,如果需要比较只比较他们的用md5加密过后的密文。
同时,md5加密算法是不可逆的,破解的难度很高。
来源:http://blog.csdn.net/fzhlee/article/details/5724530
标签:java,MD5,加密算法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Boot如何通过CORS处理跨域问题
2021-09-10 02:44:15
java操作json对象出现StackOverflow错误的问题及解决
2023-03-04 20:06:14
![](https://img.aspxhome.com/file/2023/2/63162_0s.png)
android ListView和GridView拖拽移位实现代码
2023-05-19 18:27:01
![](https://img.aspxhome.com/file/2023/5/100595_0s.png)
C#控制台下测试多线程的方法
2022-02-19 15:43:51
java判断两个时间是不是同一天的方法
2022-09-23 03:09:16
Java8新特性Stream流实例详解
2023-05-04 12:55:05
C#实现百分比转小数的方法
2021-09-09 00:53:53
深入了解java.util.Arrays的使用技巧
2023-01-10 18:01:32
Android广播接收机制详细介绍(附短信接收实现)
2023-04-16 10:09:57
Java优化if-else代码的实战记录
2023-05-16 22:55:18
Java中 ? extends T 和 ? super T的理解
2022-06-26 19:50:23
深入java事件注册的应用分析
2022-07-12 01:37:55
Android实现加载圈
2023-03-27 16:44:34
![](https://img.aspxhome.com/file/2023/7/137227_0s.jpg)
Android编程自定义AlertDialog样式的方法详解
2023-09-26 20:55:15
C# 多网卡 Server Listen
2022-05-30 16:09:18
使用@TransactionalEventListener监听事务教程
2023-10-05 02:50:44
Android 仿淘宝、京东商品详情页向上拖动查看图文详情控件DEMO详解
2022-01-17 12:21:18
![](https://img.aspxhome.com/file/2023/7/138077_0s.gif)
详解SpringBoot项目整合Vue做一个完整的用户注册功能
2022-02-13 21:46:35
![](https://img.aspxhome.com/file/2023/6/61286_0s.jpg)
Spring Boot+Mybatis+Druid+PageHelper实现多数据源并分页的方法
2023-09-06 19:53:30
![](https://img.aspxhome.com/file/2023/9/65269_0s.png)
Java实现读写文件功能的代码分享
2023-02-07 04:07:02
![](https://img.aspxhome.com/file/2023/3/78983_0s.png)