防止数据库的漏洞的泄露密码,即使泄露,也是一个加密后的结果
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 "";
}
}

posted on 2017-06-23 15:19  青青兮  阅读(3709)  评论(0)    收藏  举报