java开发_MD5_加密算法

package com.b510.note;
 2 
 3 import java.math.BigInteger;
 4 import java.security.MessageDigest;
 5 import java.security.NoSuchAlgorithmException;
 6 
 7 /**
 8  * MD5加密
 9  * 
10  * @author Hongten
11  * 
12  */
13 public class MD5 {
14 
15     public static void main(String[] args) {
16         System.out.println(MD5.getMD5("123456"));
17     }
18 
19     /**
20      * 用md5编码后的码值
21      * 
22      * @param sInput
23      *            明码
24      * @return md5加密后的密码
25 */
26     public static String getMD5(String sInput) {
27 
28         String algorithm = "";
29         if (sInput == null) {
30             return "null";
31         }
32         try {
33             algorithm = System.getProperty("MD5.algorithm", "MD5");
34         } catch (SecurityException se) {
35         }
36         MessageDigest md = null;
37         try {
38             md = MessageDigest.getInstance(algorithm);
39         } catch (NoSuchAlgorithmException e) {
40             e.printStackTrace();
41         }
42         byte buffer[] = sInput.getBytes();
43 
44         for (int count = 0; count < sInput.length(); count++) {
45             md.update(buffer, 0, count);
46         }
47         byte bDigest[] = md.digest();
48         BigInteger bi = new BigInteger(bDigest);
49         return (bi.toString(16));
50     }
51 }

你可能感兴趣的:(java开发_MD5_加密算法)