Java實(shí)現(xiàn)MD5加密的方法
本文實(shí)例講述了Java實(shí)現(xiàn)MD5加密的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5HashUtil
{
private MessageDigest md = null;
private static MD5HashUtil md5 = null;
private static final char[] hexChars ={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
/**
* Constructor is private so you must use the getInstance method
*/
private MD5HashUtil() throws NoSuchAlgorithmException
{
md = MessageDigest.getInstance("MD5");
}
/**
* This returns the singleton instance
*/
public static MD5HashUtil getInstance()throws NoSuchAlgorithmException
{
if (md5 == null)
{
md5 = new MD5HashUtil();
}
return (md5);
}
public static String hashCode(String dataToHash) throws NoSuchAlgorithmException{
return getInstance().hashData(dataToHash.getBytes());
}
public static String hashCode(byte[] dataToHash) throws NoSuchAlgorithmException{
return getInstance().hashData(dataToHash);
}
public String hashData(byte[] dataToHash) {
return hexStringFromBytes((calculateHash(dataToHash))).toLowerCase();
}
private byte[] calculateHash(byte[] dataToHash)
{
md.update(dataToHash, 0, dataToHash.length);
return (md.digest());
}
public String hexStringFromBytes(byte[] b)
{
String hex = "";
int msb;
int lsb = 0;
int i;
// MSB maps to idx 0
for (i = 0; i < b.length; i++)
{
msb = ((int)b[i] & 0x000000FF) / 16;
lsb = ((int)b[i] & 0x000000FF) % 16;
hex = hex + hexChars[msb] + hexChars[lsb];
}
return(hex);
}
public static void main(String args[]) throws NoSuchAlgorithmException
{
String string = "my name is zhangli";
System.out.println(string);
System.out.println(hashCode(string));
}
}
如上代碼為java語言實(shí)現(xiàn)md5加密算法,輸出為加密后的密文!
通常將加密后的密文保存在數(shù)據(jù)庫中,如果需要比較只比較他們的用md5加密過后的密文。
同時(shí),md5加密算法是不可逆的,破解的難度很高,雖然有人破解了md5,但是他們所用的硬件環(huán)境不是我們普通的計(jì)算機(jī)所比擬的,山大的一位很牛的女教授也破解了md5,不過不怎么了解
希望本文所述對(duì)大家的java程序設(shè)計(jì)有所幫助。
相關(guān)文章
JSONObject如何轉(zhuǎn)為實(shí)體類對(duì)象
介紹了JSONObject轉(zhuǎn)為實(shí)體類對(duì)象的三種方法:JSONObject中的toJavaObject方法和getObject方法支持深轉(zhuǎn)換,而JSON中的parseObject方法只能轉(zhuǎn)換一層對(duì)象,此外,還補(bǔ)充說明了在對(duì)JSON轉(zhuǎn)為實(shí)體類對(duì)象時(shí),無論JSON中的數(shù)據(jù)字段是否多于或少于實(shí)體類中字段,轉(zhuǎn)化都不會(huì)報(bào)錯(cuò)2024-11-11
Springboot使用jxls實(shí)現(xiàn)同sheet多個(gè)列表展示
這篇文章主要介紹了Springboot使用jxls實(shí)現(xiàn)同sheet多個(gè)列表展示,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
mybatis對(duì)象List<String> List<Integer>屬性映射方式
這篇文章主要介紹了mybatis對(duì)象List<String> List<Integer>屬性映射方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
java8新特性 stream流的方式遍歷集合和數(shù)組操作
這篇文章主要介紹了java8新特性 stream流的方式遍歷集合和數(shù)組操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
SpringBoot項(xiàng)目集成Swagger和swagger-bootstrap-ui及常用注解解讀
這篇文章主要介紹了SpringBoot項(xiàng)目集成Swagger和swagger-bootstrap-ui及常用注解解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
5分鐘搭建SpringCloud Eureka服務(wù)注冊(cè)中心的實(shí)現(xiàn)
這篇文章主要介紹了5分鐘搭建SpringCloud Eureka服務(wù)注冊(cè)中心的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

