1.場景
在很多場景需要使用MD5加密,如,驗證字符串,驗證字符串,密碼等,MD5作爲不可逆的加shi密方式,在很多方面有着天然的優勢
2.java使用MD5加密
public static String getMd5(String password) throws Exception{
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(password.getBytes());
byte by[]=md5.digest();
int i;
StringBuffer sbf = new StringBuffer();
for (int j = 0; j < by.length; j++) {
i=by[j];
if (i<0) {
i+=256;
}else if(i<16){
sbf.append("0"); //因爲大於16的有兩位,因此小於16需要補位,
}
sbf.append(Integer.toHexString(i));
}
return sbf.toString();
}
如果想要得到32位的加密字符串,請注意,MD5計算時容易出現小於16的數字,注意補位,如果時py,只需要應用lib直接,調用,真方便
測試: