溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

JAVA 消息摘要算法 MD5和SHA的代碼

發(fā)布時間:2020-07-19 23:55:24 來源:網(wǎng)絡(luò) 閱讀:158 作者:Waldron 欄目:編程語言

將內(nèi)容過程中常用的內(nèi)容片段珍藏起來,下邊資料是關(guān)于JAVA 消息摘要算法 MD5和SHA的內(nèi)容。

public static String stringMD5(String input) { ```


try {  

MessageDigest messageDigest =MessageDigest.getInstance("MD5");  

byte[] inputByteArray = input.getBytes();  

messageDigest.update(inputByteArray);  

byte[] resultByteArray = messageDigest.digest();  

return byteArrayToHex(resultByteArray);  

} catch (NoSuchAlgorithmException e) {  

return null;  

}  

}  

下面這個函數(shù)用于將字節(jié)數(shù)組換成成16進制的字符串

public static String byteArrayToHex(byte[] byteArray) {  

char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F' };  

int index = 0;  

for (byte b : byteArray) {  

resultCharArray[index++] = hexDigits[b>>> 4 & 0xf];  

resultCharArray[index++] = hexDigits[b& 0xf];  

}  

return new String(resultCharArray);  

從上面代碼可以看出,使用MessageDigest對字符串進行MD5算法的步驟是,先將字符串轉(zhuǎn)換成字節(jié)數(shù)組,在進行MD5算法,最后返回的也是一個字節(jié)數(shù)組,要我們自己轉(zhuǎn)成32位的字符串。2.文件MD5對文件進行MD5也可以像字符串MD5一樣的,首先要把文件轉(zhuǎn)成字節(jié)數(shù)組,后面和字符串MD5完全一樣。但是如果是一個特別大的文件,一下子把一個文件的數(shù)組全部讀到內(nèi)存中,那么估計內(nèi)存也吃不消。對于大文件,可以使用DigestInputStream。

public static String fileMD5(String inputFile) throws IOException {  
FileInputStream fileInputStream = null;  
DigestInputStream digestInputStream = null;  
try {  
MessageDigest messageDigest =MessageDigest.getInstance("MD5");  
fileInputStream = new FileInputStream(inputFile);  
digestInputStream = new DigestInputStream(fileInputStream,messageDigest);  

byte[] buffer =new byte[bufferSize];  
while (digestInputStream.read(buffer) > 0);  

messageDigest= digestInputStream.getMessageDigest();  

byte[] resultByteArray = messageDigest.digest();  

return byteArrayToHex(resultByteArray);  
} catch (NoSuchAlgorithmException e) {  
return null;  
} finally {  
try {  
digestInputStream.close();  
} catch (Exception e) {  
}  

try {  
fileInputStream.close();  
} catch (Exception e) {  
}  
}  
}  

測試文件MD5的main方法

public static void main(String[] args) {  

long startTime = System.currentTimeMillis();  

try {  
System.out.println(fileMD5("E:/軟件/VS2008ProEdition90DayTrialCHSX1435983.iso"));  
} catch (IOException e) {  
e.printStackTrace();  
}  

long endTime = System.currentTimeMillis();  

System.out.println((endTime - startTime)/1000);  
}  

最一般的用戶,對用戶名和密碼進行MD5我們知道,編程中數(shù)據(jù)的傳輸,保存,為了考慮安全性的問題,需要將數(shù)據(jù)進行加密.我們拿數(shù)據(jù)庫做例子.如果一個用戶注冊系統(tǒng)的數(shù)據(jù)庫,沒有對用戶的信息進行保存,如,我去頁面注冊,輸入"Vicky","123456".注冊.web服務(wù)器未對數(shù)據(jù)進行加密而直接寫入數(shù)據(jù)庫,那么數(shù)據(jù)庫中的用戶信息,便是一個直接可用的數(shù)據(jù)!一旦服務(wù)器服務(wù)器被黑~那么用戶的信息將毫無保留的展現(xiàn)在***面前...為了解決這個弊端,現(xiàn)在大多數(shù)都會將信息進行MD5加密.如"Vicky"與"123456"加密后,會生成16位或者32位字符串.而***即便獲得這些數(shù)據(jù)也無法使用...

@Test  
public void testMD() {  
try {             
String username = "Vicky";  
MessageDigest messageDigest = MessageDigest.getInstance("MD5");  
messageDigest.update(username.getBytes());  
String usernameMD5 = messageDigest.digest().toString();  
System.out.println(usernameMD5);  
} catch (Exception e) {  
e.printStackTrace();  
}  
}  

打印的是:[B@107077e,這是因為輸出的是byte[](messageDigest.digest()得到的是個二進制byte數(shù)組,有可能某些byte是不可打印的字符。)...我們可以使用Base64來處理byte[]。MessageDigest不僅僅只為我們提供了"MD5"加密,還提供了"SHA-1"等四種不同的加密方式。創(chuàng)建的方法只為:MessageDigestmessageDigest=MessageDigest.getInstance("SHA-1");MD5與SHA-1的區(qū)別為:MD5是16位,SHA是20位(這是兩種報文摘要的算法)

public static void main(String[] args) throws Exception {  
String Str = "H浙江省要臺州要市";  
getFeatureSHAbyte(Str);  
}  

public static byte[] getFeatureSHAbyte(String key) throws Exception {  

MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");  
messageDigest.update(key.getBytes());  
byte[] B = key.getBytes();  
System.out.print("原二進制數(shù):");  
for(int i : B){  
System.out.print(i+",");  
}   

System.out.println(Base64.encode(messageDigest.digest()));  
return B;  
}  

當(dāng)然我們可以編寫函數(shù),處理二進制轉(zhuǎn)hex字符串.

private String toHex(byte buffer[]) {  
for (int i = 0; i < buffer.length; i++) {  
sb.append(Character.forDigit((buffer[i] & 240) >> 4, 16));  
sb.append(Character.forDigit(buffer[i] & 15, 16));  
}  

return sb.toString();  
}  
向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI