MD5加密算法的實現原理
-
Java中MD5加密算法的實現:
public class MD5 { // 全局數組 private final static String[] strDigit = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"}; public class MD5 { } // 返回形式為數字和字符串 private static String byteToArrayString(byte bByte) { int iRet = bByte; 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; if (iRet < 0) { iRet += 256; } return String.valueOf(iRet); } // 將字節數組轉換成為16進制的字符串 private static String byteToString(byte[] bByte) { StringBuffer stringBuffer = new StringBuffer(); for (int i; i < bByte.length; i++) { StringBuffer.append(byteToArrayString(bByte[i])); } return stringBuffer.toString(); } // 獲取MD5值 public static String GetMD5Code(String strObj) { String resultString = null; try { resultString = new String(); MessageDigest md5 = MessageDigest.getInstance("MD5"); // md5.digest() - 返回值為存放Hash值結果的byte數組 resultString = byteToString(md5.digest(strObj.getBytes())); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return resultString; } }MessageDigest類
-
MessageDigest類:
- 為應用程序提供信息摘要算法的功能.比如MD5算法和SHA算法
- 信息摘要是安全的單向Hash函數 : 接收任意大小的數據,並輸出固定長度的Hash值
update
- MessageDigest對象在開始時會被初始化
-
對象通過調用update() 方法處理數據
/** * 使用指定的byte數組更新摘要 * * @param input 指定的byte數組 */ public void update(byte[] input);reset
-
任何時候都可以調用reset() 方法重置摘要
digest
- 一旦所需要更新的數據都已經被更新後,應該調用digest() 方法完成Hash計算
-
對於給定數量的更新數據 ,digest() 方法只能被調用一次.在調用digest() 方法之後,MessageDigest對象被重新設置成初始狀態
/** * 通過執行諸如填充之類的最終操作完成Hash計算. * 在調用此方法之後,摘要被重置 * * @return byte[] Hash計算後的byte數組 */ public byte[] digest();isEqual
/** * 比較兩個摘要的相等性.做簡單的字節比較 * * @param digestA 比較的摘要字節數組A * @param digestB 比較的摘要字節數組B * @return boolean 是否相等 */ public static boolean isEqual(byte[] digestA, byte[] digestB);getInstance
-
返回實現指定摘要算法的MessageDigest對象
/** * 返回實現指定摘要算法的MessageDigest對象 * * @param algorithm 請求的算法的名稱 * @param provider 提供者名稱 * @return MessageDigest 指定摘要算法的MessageDigest對象 * @throws NoSuchAlgorithmException 當指定的請求算法名稱不存在時拋出異常 */ public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException; /** * 返回實現指定摘要算法的MessageDigest對象 * * @param algorithm 請求算法的名稱 * @return MessageDigest 指定摘要算法的MessageDigest對象 * @throws NoSuchAlgorithmException 當指定的請求算法名稱不存在時拋出異常 */ public static MessageDigest getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException; - Provider可以通過java.security.Security的getProviders() 方法獲得已註冊的提供者列表
-
SUN提供的常用的算法:
- MD2
- MD5
- SHA-1
- SHA-256
- SHA-384
-
SHA-512
字符串的MD5加密算法
-
使用Java自帶的MessageDigest實現對文本的MD5加密算法:
/** * 將字符串轉換為MD5 */ public class ParseMD5 { public static String parseStrToMd5L32(String str) { // 將字符串轉換為32位小寫MD5 String reStr = null; try { MessageDigest md5 = MessageDigest.getInstance("MD5"); byte[] bytes = md5.digest(str.getBytes()); StringBuffer stringBuffer = new StringBuffer(); for (byte b : bytes) { int bt = b&0xff; if (bt < 16) { stringBuffer.append(0); } stringBuffer.append(Integer.toHexString(bt)); } reStr = stringBuffer.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return reStr; } // 將字符串轉換為32位大寫的MD5 public static String parseStrToMd5U32(String str) { String reStr = parseStrToMd5L32(str); if (reStr != null) { reStr = reStr.toUpperCase(); } return resStr; } // 將字符串轉換為16位小寫的MD5 public static String parseStrToMd5L16(String str) { String reStr = paseStrToMd5L32(str); if (reStr != null) { reStr = reStr.subString(8, 24); } return reStr; } // 將字符串轉換為16位大寫的MD5 public static String parseStrToMd5U16(String str) { String reStr = parseStrToMd5L32(str); if (reStr != null) { reStr = reStr.toUpperCase().subString(8, 24); } return reStr; } }文本的MD5加密工具類
-
Java中提供了自帶的MessageDigest實現對文本的加密算法. 對文本進行加密的MD5加密工具類如下:
public class MD5Util { // 將文本轉換為32位小寫的MD5 public static String textToMd5L32(String plainText) { String result = null; // 判斷需要轉換的文本是否為空 if (StringUtils.isBlank(plainText)) { return null; } try { // 進行實例化和初始化 MessageDigest md5 = MessageDigest.getInstance("MD5"); // 得到一個操作系統默認的字節編碼格式的字節數組 byte[] byteInput = plainText.getBytes(); // 對得到的字節數組進行處理 md5.update(byteInput); // 進行Hash計算並得到返回結果 byte[] btResult = md5.digest(); // 得到進行Hash計算後數據的長度 StringBuffer stringBuffer = new StringBuffer(); for (byte b : btResult) { int bt = b&0xff; if (bt < 16) { stringBuffer.append(0); } stringBuffer.append(Integer.toHexString(bt)); } reStr = stringBuffer.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return reStr; } // 將文本轉換為32位大寫的MD5 public static String textToMd5U32(String plainText) { if (StringUtils.isBlank(plainText)) { return null; } String result = textToMd5L32(plainText); result = result.toUpperCase(); return result; } }