MD5 加密的应用(Java)
1. 什么是 MD5 加密
MD5 消息摘要算法,属 Hash 算法一类,可以对输入任意长度的消息产生一个 128 位的消息摘要(32 位的数字字母混合码)。
MD5 码主要的特点是不可逆,可用于密码加密。
需要注意的是,MD5 已经不是当今最安全的加密算法,网上也有解密 MD5 码的网站,所以对安全性要求比较高的数据建议使用其他更安全的加密算法。
2. 应用(以 Java 代码为例)
2.1 初始版本
2.1.1 代码
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class PasswordEncryptionUtil {
/**
* 加密明文密码
* @param password 明文密码
* @return
*/
public static String encryptPassword(String password) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(password.getBytes());
byte[] hashedPassword = md.digest();
StringBuilder sBuilder = new StringBuilder();
for (byte b : hashedPassword) {
sBuilder.append(String.format("%02x", b));
}
return sBuilder.toString();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("MD5 algorithm not available!", e);
}
}
/**
* 验证密码
* @param password 明文密码
* @param hashed 加密后的密码
* @return
*/
public static boolean checkPassword(String password, String hashed) {
String hashedPass = encryptPassword(password);
return hashedPass.equals(hashed);
}
}
2.1.2 运行结果
加密后的密码:e10adc3949ba59abbe56e057f20f883e
加密前后的密码验证结果为:true
2.2 改进版 1:加盐
为了提高安全性,在初始版本的基础上加盐,得到以下版本。
2.2.1 代码
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class PasswordEncryptionUtil {
/**
* 定义盐值长度
*/
private static final int SALT_LENGTH = 10;
/**
* 加密明文密码
* @param password 明文密码
* @return
* @throws NoSuchAlgorithmException
*/
public static String encryptPassword(String password) throws NoSuchAlgorithmException {
try {
SecureRandom random = new SecureRandom();
byte[] salt = new byte[SALT_LENGTH];
random.nextBytes(salt);
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(salt);
byte[] hashedPassword = md.digest(password.getBytes());
StringBuilder sb = new StringBuilder();
for (byte b : hashedPassword) {
sb.append(String.format("%02x", b));
}
sb.append(":");
for (byte b : salt) {
sb.append(String.format("%02x", b));
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("MD5 algorithm not available!", e);
}
}
/**
* 验证密码
* @param password 明文密码
* @param hashedPassword 加密后的密码
* @return
* @throws NoSuchAlgorithmException
*/
public static boolean verifyPassword(String password, String hashedPassword) throws NoSuchAlgorithmException {
try {
String[] parts = hashedPassword.split(":");
byte[] salt = hexStringToByteArray(parts[1]);
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(salt);
byte[] hashedPasswordToCheck = md.digest(password.getBytes());
StringBuilder sb = new StringBuilder();
for (byte b : hashedPasswordToCheck) {
sb.append(String.format("%02x", b));
}
return sb.toString().equals(parts[0]);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("MD5 algorithm not available!", e);
}
}
/**
* 将字节数组转换为十六进制字符串
* @param s 字节数组
* @return
*/
private static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
}
2.2.2 运行结果
加密后的密码:7ccd1468ddf9e58f57de0926c0caeead:b282ee02d9b2f25f439f
加密前后的密码验证结果为:true
2.3 改进版 2:加盐 + 多次加密
在改进版 1 的基础上,对密码进行多次加密,可以再次提高安全性,如下。
2.3.1 代码
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class PasswordEncryptionUtil {
/**
* 定义盐值长度
*/
private static final int SALT_LENGTH = 10;
/**
* 定义加密次数
*/
private static final int ITERATIONS = 50;
/**
* 加密明文密码
* @param password 明文密码
* @return
* @throws NoSuchAlgorithmException
*/
public static String encryptPassword(String password) throws NoSuchAlgorithmException {
try {
SecureRandom random = new SecureRandom();
byte[] salt = new byte[SALT_LENGTH];
random.nextBytes(salt);
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(salt);
byte[] hashedPasswordBytes = md.digest(password.getBytes());
for (int i = 0; i < ITERATIONS; i++) {
md.reset();
hashedPasswordBytes = md.digest(hashedPasswordBytes);
}
String saltString = byteArrayToHexString(salt);
String hashedPasswordString = byteArrayToHexString(hashedPasswordBytes);
return hashedPasswordString + ":" + saltString;
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("MD5 algorithm not available!", e);
}
}
/**
* 验证密码
* @param password 明文密码
* @param hashedPassword 加密后的密码
* @return
* @throws NoSuchAlgorithmException
*/
public static boolean verifyPassword(String password, String hashedPassword) throws NoSuchAlgorithmException {
try {
String[] parts = hashedPassword.split(":");
byte[] salt = hexStringToByteArray(parts[1]);
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(salt);
byte[] hashedPasswordBytes = md.digest(password.getBytes());
for (int i = 0; i < ITERATIONS; i++) {
md.reset();
hashedPasswordBytes = md.digest(hashedPasswordBytes);
}
String hashedPasswordToCheck = byteArrayToHexString(hashedPasswordBytes);
return hashedPasswordToCheck.equals(parts[0]);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("MD5 algorithm not available!", e);
}
}
/**
* 将字节数组转换为十六进制字符串
* @param s 字节数组
* @return
*/
private static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
/**
* 将字节数组转换为十六进制字符串,以便与存储的哈希密码进行比较
* @param bytes
* @return
*/
private static String byteArrayToHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}
}
2.3.2 运行结果
加密后的密码:5c378887ebfcefedaeba52e14e5fb033:fcbe89fa33f0bbdd81fb
加密前后的密码验证结果为:true