md5 java代码
时间: 2023-10-06 12:14:43 浏览: 88
md5 java 代码
下面是 Java 中使用 MD5 算法进行加密的代码示例:
```java
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Util {
public static String encrypt(String input) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(input.getBytes());
byte[] digest = md.digest();
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append(String.format("%02x", b & 0xff));
}
return sb.toString();
}
}
```
使用方式:
```java
String input = "Hello, World!";
String encrypted = MD5Util.encrypt(input);
System.out.println("MD5 of `" + input + "` is: " + encrypted);
```
输出:
```
MD5 of `Hello, World!` is: ed076287532e86365e841e92bfc50d8c
```
阅读全文