Java Web實踐專題——MD5加密

來源:互聯網
上載者:User
          MD5是Message Digest 5的縮寫,是一種密碼編譯演算法,能夠對位元組數組進行加密,有如下特點:

l         不能根據加密後的資訊找加密之前的資訊;l         加密後的結果是128位;l         對於給定的位元組數組,不管什麼時候採用這種密碼編譯演算法,結果是相同的;l         對於不同的位元組數組,加密的結果是不相同的。在Web應用中通常需要對使用者佈建的密碼進行加密之後再儲存,否則資料庫管理員能夠看到明文的密碼,並且密碼也有被駭客擷取的危險。可以使用MD5對使用者的密碼進行加密。但是有兩種情況,你可能需要使用其他的方法處理:l         需要保證密碼在傳輸過程中也是安全的,這時候通常都使用https,幾乎所有的銀行網站都是這樣,相對來說成本比較高。l         如果網站提供了取回密碼的功能。因為使用MD5加密之後原來的密碼是沒有辦法擷取的。MD5的應用程式套件括如下幾個過程:l         把要加密的資訊轉換成位元組數組;l         擷取MessageDigest對象,該對象完成加密;l         使用轉換後的位元組數組初始化MessgeDigest對象;l         調用digest方法進行加密,返回byte數組;l         把byte數群組轉換成字串,然後就可以使用加密後的字串了。假設原來的字串為oldStr,內容為“lixucheng”,加密後的字串為newStr。具體過程分別介紹如下。1、把字串轉換成位元組數組    可以使用字串的getBytes方法進行轉換,例如:byte[] oldBytes = oldStr.getBytes();數組中的資料:108 105 120 117 99 104 101 110 1032、擷取MessgaeDigest對象使用MessageDigest的getInstance(String str)方法得到MessgeDigest對象,參數使用MD5。例如:MessageDigest md = MessageDigest.getInstance("MD5");3、使用轉換後的位元組數組初始化MessgeDigest對象使用update方法進行初始化,參數為轉換後的位元組數組。例如:md.update(oldBytes);4、調用digest方法進行加密方法返回的是位元組數組。例如:byte[] newBytes = md.digest();數組中的資料(16位):-22 1 35 121 -120 65 114 75 127 -34 31 -21 51 -37 -97 -1185、轉換成16進位表示的字串    下面的程式碼完成轉換:            // 構造長度為2倍的字串                     char newStr[] = new char[32];                         // 迴圈進行處理                     for (int i = 0; i < 16; i++) {                            byte tmp = newBytes[i];                            newStr[2*i] = hexDigits[tmp >>> 4 & 0xf];                            newStr[2*i+1] = hexDigits[tmp & 0xf];                     }轉換後的字串(32位):ea0123798841724b7fde1feb33db9f8a提示:如果需要把轉換後的密碼儲存到資料庫中,需要使用的類型為:char(32)。 完整的參考代碼如下: package test; import java.security.*; class MD5_Test {       public final static String MD5(String oldStr) {              char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',                            'a', 'b', 'c', 'd', 'e', 'f' };                System.out.println("原始字串為:"+oldStr);              try {                        // 參數oldStr表示要加密的字串                        // 轉換成位元組流                     byte[] oldBytes = oldStr.getBytes(); for(byte b:oldBytes){                     System.out.print(b+" ");}System.out.println();                          // 得到對象                        MessageDigest md = MessageDigest.getInstance("MD5");                         // 初始化                     md.update(oldBytes);                         // 運行密碼編譯演算法                     byte[] newBytes = md.digest(); for(byte b:newBytes){                     System.out.print(b+" ");}System.out.println();                         // 構造長度為2倍的字串                     char newStr[] = new char[32];                         // 迴圈進行處理                     for (int i = 0; i < 16; i++) {                            byte tmp = newBytes[i];                            newStr[2*i] = hexDigits[tmp >>> 4 & 0xf];                            newStr[2*i+1] = hexDigits[tmp & 0xf];                     }                     System.out.println(newStr);                     return new String(newStr);              } catch (Exception e) {                     return null;              }       }        public static void main(String[] args) {              System.out.println(MD5_Test.MD5("lixucheng"));       }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.