Android初級教程:對檔案和字串進行MD5加密工具類

來源:互聯網
上載者:User

標籤:

轉載請註明出處:http://blog.csdn.net/qq_32059827/article/details/52200008   點擊開啟連結

之前寫過一篇博文,是針對字串進行md5加密的。今天對其進行改進,加入針對某個檔案,進行md5加密,並擷取加密後的值。並把兩個功能封裝成了工具類,如果有需要這個演算法的,可直接使用。

直接上演算法封裝的工具類代碼:

package com.itydl.utils;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;/** * 針對字串做的md5加密,以及涉及md5操作的工具類 * @author lenovo * */public class Md5Utils {/** * 返迴文件的md5值 * @param path * 要加密的檔案的路徑 * @return * 檔案的md5值 */public static String getFileMD5(String path){StringBuilder sb = new StringBuilder();try {FileInputStream fis = new FileInputStream(new File(path));//擷取MD5加密器MessageDigest md = MessageDigest.getInstance("md5");//類似讀取檔案byte[] bytes = new byte[10240];//一次讀取寫入10kint len = 0;while((len = fis.read(bytes))!=-1){//從原目的地讀取資料//把資料寫到md加密器,類比fos.write(bytes, 0, len);md.update(bytes, 0, len);}//讀完整個檔案資料,並寫到md加密器中byte[] digest = md.digest();//完成加密,得到md5值,但是是byte類型的。還要做最後的轉換for (byte b : digest) {//遍曆位元組,把每個位元組拼接起來//把每個位元組轉換成16進位數int d = b & 0xff;//只保留後兩位元String herString = Integer.toHexString(d);//把int類型資料轉為16進位字串表示//如果只有一位,則在前面補0.讓其也是兩位if(herString.length()==1){//位元組高4位為0herString = "0"+herString;//拼接字串,拼成兩位表示}sb.append(herString);}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (NoSuchAlgorithmException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return sb.toString();}/** * 對傳遞過來的字串進行md5加密 * @param str * 待加密的字串 * @return * 字串Md5加密後的結果 */public static String md5(String str){StringBuilder sb = new StringBuilder();//字串容器try {//擷取md5加密器.public static MessageDigest getInstance(String algorithm)返回實現指定摘要演算法的 MessageDigest 對象。MessageDigest md = MessageDigest.getInstance("MD5");byte[] bytes = str.getBytes();//把要加密的字串轉換成位元組數組byte[] digest = md.digest(bytes);//使用指定的 【byte 數組】對摘要進行最後更新,然後完成摘要計算。即完成md5的加密for (byte b : digest) {//把每個位元組轉換成16進位數int d = b & 0xff;//只保留後兩位元String herString = Integer.toHexString(d);//把int類型資料轉為16進位字串表示//如果只有一位,則在前面補0.讓其也是兩位if(herString.length()==1){//位元組高4位為0herString = "0"+herString;//拼接字串,拼成兩位表示}sb.append(herString);}} catch (NoSuchAlgorithmException e) {// TODO Auto-generated catch blocke.printStackTrace();}return sb.toString();}}



Android初級教程:對檔案和字串進行MD5加密工具類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.