Home > Java > javaTutorial > body text

Java implementation of md5 encryption example

高洛峰
Release: 2017-01-24 11:13:10
Original
1449 people have browsed it

/**
 * 实现MD5加密
 *
 */
public class MD5 {
 
 /**
  * 获取加密后的字符串
  * @param input
  * @return
  */
 public static String stringMD5(String pw) {
  try {  
     
        // 拿到一个MD5转换器(如果想要SHA1参数换成”SHA1”)  
        MessageDigest messageDigest =MessageDigest.getInstance("MD5");  
        // 输入的字符串转换成字节数组  
        byte[] inputByteArray = pw.getBytes();  
        // inputByteArray是输入字符串转换得到的字节数组  
        messageDigest.update(inputByteArray);  
        // 转换并返回结果,也是字节数组,包含16个元素  
        byte[] resultByteArray = messageDigest.digest();  
        // 字符数组转换成字符串返回  
        return byteArrayToHex(resultByteArray);  
     } catch (NoSuchAlgorithmException e) {  
        return null;  
     }  
 }
 
    public static String byteArrayToHex(byte[] byteArray) {  
        
        // 首先初始化一个字符数组,用来存放每个16进制字符  
        char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F' };  
        // new一个字符数组,这个就是用来组成结果字符串的(解释一下:一个byte是八位二进制,也就是2位十六进制字符(2的8次方等于16的2次方))  
        char[] resultCharArray =new char[byteArray.length * 2];  
        // 遍历字节数组,通过位运算(位运算效率高),转换成字符放到字符数组中去  
        int index = 0; 
        for (byte b : byteArray) {  
           resultCharArray[index++] = hexDigits[b>>> 4 & 0xf];  
           resultCharArray[index++] = hexDigits[b& 0xf];  
        }
        // 字符数组组合成字符串返回  
        return new String(resultCharArray);  
    }
}
Copy after login

For more articles related to Java implementation of md5 encryption examples, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!