先從下面這張圖對MD5加密實現進行了解,具體如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
package com.pb; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Scanner; /* * 驗證MD5 * 1.初始化MessageDigest信息摘要對象 * 2.傳入需要計算的字符串更新摘要信息 * 3.計算信息摘要 * 4.將byte[] 轉換為找度為32位的16進制字符串 */ public class MD5 { /* * 生成md5 有傳入參數字符串 */ public void generateMD5(String input){ try { //1.初始化MessageDigest信息摘要對象,并指定為MD5不分大小寫都可以 MessageDigest md=MessageDigest.getInstance( "md5" ); //2.傳入需要計算的字符串更新摘要信息,傳入的為字節數組byte[], //將字符串轉換為字節數組使用getBytes()方法完成 //指定時其字符編碼 為utf-8 md.update(input.getBytes( "utf-8" )); //3.計算信息摘要digest()方法 //返回值為字節數組 byte [] hashCode=md.digest(); //4.將byte[] 轉換為找度為32位的16進制字符串 //聲明StringBuffer對象來存放最后的值 StringBuffer sb= new StringBuffer(); //遍歷字節數組 for ( byte b:hashCode){ //對數組內容轉化為16進制, sb.append(Character.forDigit(b>> 4 & 0xf , 16 )); //換2次為32位的16進制 sb.append(Character.forDigit(b& 0xf , 16 )); } System.out.println( "加密后的結果是:" +sb.toString()); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { //聲明加密MD5類的對象 MD5 md5= new MD5(); //使用Scanner來輸入一個字符 Scanner scanner= new Scanner(System.in); System.out.println( "請輸入要加密的內容:" ); String input = scanner.nextLine(); //調用加密方法 md5.generateMD5(input); } } |
結果:
請輸入要加密的內容:
學習MD5加密過程
加密后的結果是:b826cdac46f01dcc8ccc60a76cebf858
第二段代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
package test.md5; import java.security.MessageDigest; public class MD5Util { public final static String MD5(String s) { char hexDigits[]={ '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'A' , 'B' , 'C' , 'D' , 'E' , 'F' }; try { byte [] btInput = s.getBytes(); // 獲得MD5摘要算法的 MessageDigest 對象 MessageDigest mdInst = MessageDigest.getInstance( "MD5" ); // 使用指定的字節更新摘要 mdInst.update(btInput); // 獲得密文 byte [] md = mdInst.digest(); // 把密文轉換成十六進制的字符串形式 int j = md.length; char str[] = new char [j * 2 ]; int k = 0 ; for ( int i = 0 ; i < j; i++) { byte byte0 = md[i]; str[k++] = hexDigits[byte0 >>> 4 & 0xf ]; str[k++] = hexDigits[byte0 & 0xf ]; } return new String(str); } catch (Exception e) { e.printStackTrace(); return null ; } } public static void main(String[] args) { System.out.println(MD5Util.MD5( "20121221" )); System.out.println(MD5Util.MD5( "加密" )); } } |
第三段代碼:MD5加密算法的java實現
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
package other; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /* * MD5 算法 */ public class MD5 { // 全局數組 private final static String[] strDigits = { "0" , "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "a" , "b" , "c" , "d" , "e" , "f" }; public MD5() { } // 返回形式為數字跟字符串 private static String byteToArrayString( byte bByte) { int iRet = bByte; // System.out.println("iRet="+iRet); if (iRet < 0 ) { iRet += 256 ; } int iD1 = iRet / 16 ; int iD2 = iRet % 16 ; return strDigits[iD1] + strDigits[iD2]; } // 返回形式只為數字 private static String byteToNum( byte bByte) { int iRet = bByte; System.out.println( "iRet1=" + iRet); if (iRet < 0 ) { iRet += 256 ; } return String.valueOf(iRet); } // 轉換字節數組為16進制字串 private static String byteToString( byte [] bByte) { StringBuffer sBuffer = new StringBuffer(); for ( int i = 0 ; i < bByte.length; i++) { sBuffer.append(byteToArrayString(bByte[i])); } return sBuffer.toString(); } public static String GetMD5Code(String strObj) { String resultString = null ; try { resultString = new String(strObj); MessageDigest md = MessageDigest.getInstance( "MD5" ); // md.digest() 該函數返回值為存放哈希值結果的byte數組 resultString = byteToString(md.digest(strObj.getBytes())); } catch (NoSuchAlgorithmException ex) { ex.printStackTrace(); } return resultString; } public static void main(String[] args) { MD5 getMD5 = new MD5(); System.out.println(getMD5.GetMD5Code( "000000" )); } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。