java實現(xiàn)md5加密示例
/**
* 實現(xiàn)MD5加密
*
*/
public class MD5 {
/**
* 獲取加密后的字符串
* @param input
* @return
*/
public static String stringMD5(String pw) {
try {
// 拿到一個MD5轉換器(如果想要SHA1參數(shù)換成”SHA1”)
MessageDigest messageDigest =MessageDigest.getInstance("MD5");
// 輸入的字符串轉換成字節(jié)數(shù)組
byte[] inputByteArray = pw.getBytes();
// inputByteArray是輸入字符串轉換得到的字節(jié)數(shù)組
messageDigest.update(inputByteArray);
// 轉換并返回結果,也是字節(jié)數(shù)組,包含16個元素
byte[] resultByteArray = messageDigest.digest();
// 字符數(shù)組轉換成字符串返回
return byteArrayToHex(resultByteArray);
} catch (NoSuchAlgorithmException e) {
return null;
}
}
public static String byteArrayToHex(byte[] byteArray) {
// 首先初始化一個字符數(shù)組,用來存放每個16進制字符
char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F' };
// new一個字符數(shù)組,這個就是用來組成結果字符串的(解釋一下:一個byte是八位二進制,也就是2位十六進制字符(2的8次方等于16的2次方))
char[] resultCharArray =new char[byteArray.length * 2];
// 遍歷字節(jié)數(shù)組,通過位運算(位運算效率高),轉換成字符放到字符數(shù)組中去
int index = 0;
for (byte b : byteArray) {
resultCharArray[index++] = hexDigits[b>>> 4 & 0xf];
resultCharArray[index++] = hexDigits[b& 0xf];
}
// 字符數(shù)組組合成字符串返回
return new String(resultCharArray);
}
}
PS:關于加密技術,本站還提供了如下加密工具供大家參考使用:
MD5在線加密工具:http://tools.jb51.net/password/CreateMD5Password
Escape加密/解密工具:http://tools.jb51.net/password/escapepwd
在線SHA1加密工具:http://tools.jb51.net/password/sha1encode
短鏈(短網址)在線生成工具:http://tools.jb51.net/password/dwzcreate
短鏈(短網址)在線還原工具:http://tools.jb51.net/password/unshorturl
高強度密碼生成器:http://tools.jb51.net/password/CreateStrongPassword
相關文章
GC調優(yōu)實戰(zhàn)之高分配速率High?Allocation?Rate
這篇文章主要為大家介紹了GC調優(yōu)之高分配速率High?Allocation?Rate的實戰(zhàn)示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-01-01springboot整合JSR303校驗功能實現(xiàn)代碼
這篇文章主要介紹了springboot整合JSR303校驗功能實現(xiàn),JSR303校驗方法有統(tǒng)一校驗的需求,統(tǒng)一校驗實現(xiàn)以及分組校驗,本文結合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-01-01Spring Security 實現(xiàn)“記住我”功能及原理解析
這篇文章主要介紹了Spring Security 實現(xiàn)“記住我”功能及原理解析,需要的朋友可以參考下2020-05-05MyBatis使用自定義TypeHandler轉換類型的實現(xiàn)方法
這篇文章主要介紹了MyBatis使用自定義TypeHandler轉換類型的實現(xiàn)方法,本文介紹使用TypeHandler 實現(xiàn)日期類型的轉換,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-10-10