防止數據庫的漏洞的泄露密碼,即使泄露,也是一個加密后的結果
public static String getPwd(String pwd){
//MD5加密的算法,有JDK實現,我們只需要使用
try {
//獲取加密的對象
MessageDigest instance = MessageDigest.getInstance("MD5");
//使用加密對象的方法,完成加密
byte[] bs = instance.digest(pwd.getBytes());
//朝着mysql加密結果的方向優化
/**
* byte b 1111 1111
* int b 0000 0000 0000 0000 0000 0000 1111 1111
* int 255 0000 0000 0000 0000 0000 0000 1111 1111
* &--------------------------------------------------
* 0000 0000 0000 0000 0000 0000 1111 1111
* */
String str = "";
//第一,將所有的數據,轉換成正數
for (byte b : bs) {
int temp = b & 255;
//第二,將所有的數據,轉換成16進制格式
if(temp >=0 && temp <=15){
str = str +"0"+ Integer.toHexString(temp);
}else{
str = str + Integer.toHexString(temp);;
}
}
return str;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return "";
}
}