Java md5加密
时间: 2023-09-11 08:13:03 浏览: 97
java md5 加密
Java提供了自带的jar工具类MessageDigest来实现MD5加密算法。你可以使用以下代码来将字符串转换成MD5摘要值:
```java
public class MD5Utils {
public static String stringToMD5(String plainText) {
byte[] secretBytes = null;
try {
secretBytes = MessageDigest.getInstance("md5").digest(plainText.getBytes());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("没有这个md5算法!");
}
String md5code = new BigInteger(1, secretBytes).toString(16);
for (int i = 0; i < 32 - md5code.length(); i++) {
md5code = "0" + md5code;
}
return md5code;
}
}
```
使用示例:
```java
@Test
public void testMd5() throws NoSuchAlgorithmException{
MessageDigest md = MessageDigest.getInstance("MD5");
String resultString = MD5Utils.stringToMD5("123456");
System.out.println(resultString); // 输出:e10adc3949ba59abbe56e057f20f883e
}
```
这样你就可以得到字符串"123456"的MD5加密结果"e10adc3949ba59abbe56e057f20f883e"。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [java实现MD5加密](https://blog.csdn.net/qq_45681735/article/details/106666067)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文