search
HomeJavajavaTutorialJava code example sharing for MD5 encryption and decryption

Basics: Use of MessageDigest class

In fact, MD5 encryption needs to be completed in Java. Most of the MessageDigest class can help you implement it. A few lines of code are enough:

/**
 * 对字符串md5加密
 *
 * @param str
 * @return
 */
import java.security.MessageDigest;
public static String getMD5(String str) {
 try {
 // 生成一个MD5加密计算摘要
 MessageDigest md = MessageDigest.getInstance("MD5");
 // 计算md5函数
 md.update(str.getBytes());
 // digest()最后确定返回md5 hash值,返回值为8为字符串。因为md5 hash值是16位的hex值,实际上就是8位的字符
 // BigInteger函数则将8位的字符串转换成16位hex值,用字符串来表示;得到字符串形式的hash值
 return new BigInteger(1, md.digest()).toString(16);
 } catch (Exception e) {
 throw new SpeedException("MD5加密出现错误");
 }
}


Advanced: Encryption and Decryption Class
Java implements MD5 encryption and decryption classes, with test classes attached, see the code for details.

MD5 encryption and decryption class——MyMD5Util, the code is as follows

package com.zyg.security.md5;
 
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;
 
public class MyMD5Util {
  
 private static final String HEX_NUMS_STR="0123456789ABCDEF";
 private static final Integer SALT_LENGTH = 12;
  
 /**
 * 将16进制字符串转换成字节数组
 * @param hex
 * @return
 */
 public static byte[] hexStringToByte(String hex) {
 int len = (hex.length() / 2);
 byte[] result = new byte[len];
 char[] hexChars = hex.toCharArray();
 for (int i = 0; i < len; i++) {
 int pos = i * 2;
 result[i] = (byte) (HEX_NUMS_STR.indexOf(hexChars[pos]) << 4
 | HEX_NUMS_STR.indexOf(hexChars[pos + 1]));
 }
 return result;
 }
 
  
 /**
 * 将指定byte数组转换成16进制字符串
 * @param b
 * @return
 */
 public static String byteToHexString(byte[] b) {
 StringBuffer hexString = new StringBuffer();
 for (int i = 0; i < b.length; i++) {
 String hex = Integer.toHexString(b[i] & 0xFF);
 if (hex.length() == 1) {
 hex = &#39;0&#39; + hex;
 }
 hexString.append(hex.toUpperCase());
 }
 return hexString.toString();
 }
  
 /**
 * 验证口令是否合法
 * @param password
 * @param passwordInDb
 * @return
 * @throws NoSuchAlgorithmException
 * @throws UnsupportedEncodingException
 */
 public static boolean validPassword(String password, String passwordInDb)
 throws NoSuchAlgorithmException, UnsupportedEncodingException {
 //将16进制字符串格式口令转换成字节数组
 byte[] pwdInDb = hexStringToByte(passwordInDb);
 //声明盐变量
 byte[] salt = new byte[SALT_LENGTH];
 //将盐从数据库中保存的口令字节数组中提取出来
 System.arraycopy(pwdInDb, 0, salt, 0, SALT_LENGTH);
 //创建消息摘要对象
 MessageDigest md = MessageDigest.getInstance("MD5");
 //将盐数据传入消息摘要对象
 md.update(salt);
 //将口令的数据传给消息摘要对象
 md.update(password.getBytes("UTF-8"));
 //生成输入口令的消息摘要
 byte[] digest = md.digest();
 //声明一个保存数据库中口令消息摘要的变量
 byte[] digestInDb = new byte[pwdInDb.length - SALT_LENGTH];
 //取得数据库中口令的消息摘要
 System.arraycopy(pwdInDb, SALT_LENGTH, digestInDb, 0, digestInDb.length);
 //比较根据输入口令生成的消息摘要和数据库中消息摘要是否相同
 if (Arrays.equals(digest, digestInDb)) {
 //口令正确返回口令匹配消息
 return true;
 } else {
 //口令不正确返回口令不匹配消息
 return false;
 }
 }
 
 
 /**
 * 获得加密后的16进制形式口令
 * @param password
 * @return
 * @throws NoSuchAlgorithmException
 * @throws UnsupportedEncodingException
 */
 public static String getEncryptedPwd(String password)
 throws NoSuchAlgorithmException, UnsupportedEncodingException {
 //声明加密后的口令数组变量
 byte[] pwd = null;
 //随机数生成器
 SecureRandom random = new SecureRandom();
 //声明盐数组变量
 byte[] salt = new byte[SALT_LENGTH];
 //将随机数放入盐变量中
 random.nextBytes(salt);
 
 //声明消息摘要对象
 MessageDigest md = null;
 //创建消息摘要
 md = MessageDigest.getInstance("MD5");
 //将盐数据传入消息摘要对象
 md.update(salt);
 //将口令的数据传给消息摘要对象
 md.update(password.getBytes("UTF-8"));
 //获得消息摘要的字节数组
 byte[] digest = md.digest();
 
 //因为要在口令的字节数组中存放盐,所以加上盐的字节长度
 pwd = new byte[digest.length + SALT_LENGTH];
 //将盐的字节拷贝到生成的加密口令字节数组的前12个字节,以便在验证口令时取出盐
 System.arraycopy(salt, 0, pwd, 0, SALT_LENGTH);
 //将消息摘要拷贝到加密口令字节数组从第13个字节开始的字节
 System.arraycopy(digest, 0, pwd, SALT_LENGTH, digest.length);
 //将字节数组格式加密后的口令转化为16进制字符串格式的口令
 return byteToHexString(pwd);
 }
}


##Test class——Client, the code is as follows:

package com.zyg.security.md5;
 
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
 
public class Client {
 private static Map users = new HashMap();
  
 public static void main(String[] args){
 String userName = "zyg";
 String password = "123";
 registerUser(userName,password);
  
 userName = "changong";
 password = "456";
 registerUser(userName,password);
  
 String loginUserId = "zyg";
 String pwd = "1232";
 try {
 if(loginValid(loginUserId,pwd)){
 System.out.println("欢迎登陆!!!");
 }else{
 System.out.println("口令错误,请重新输入!!!");
 }
 } catch (NoSuchAlgorithmException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (UnsupportedEncodingException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 }
  
 /**
 * 注册用户
 *
 * @param userName
 * @param password
 */
 public static void registerUser(String userName,String password){
 String encryptedPwd = null;
 try {
 encryptedPwd = MyMD5Util.getEncryptedPwd(password);
  
 users.put(userName, encryptedPwd);
  
 } catch (NoSuchAlgorithmException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (UnsupportedEncodingException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 }
  
 /**
 * 验证登陆
 *
 * @param userName
 * @param password
 * @return
 * @throws UnsupportedEncodingException
 * @throws NoSuchAlgorithmException
 */
 public static boolean loginValid(String userName,String password)
 throws NoSuchAlgorithmException, UnsupportedEncodingException{
 String pwdInDb = (String)users.get(userName);
 if(null!=pwdInDb){ // 该用户存在
 return MyMD5Util.validPassword(password, pwdInDb);
 }else{
 System.out.println("不存在该用户!!!");
 return false;
 }
 }
}


For more code examples of Java implementation of MD5 encryption and decryption, please pay attention to the PHP Chinese website for related articles!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment