密码与加密验证
此验证是不可逆转的
public class Passwor
{
private final static String[] hexDigital =
{ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
public static String creatPassword(String inputString)
{
return enCodeByMD5(inputString);
}
public static boolean authentiCreatPassword(String password, String inputString)
{
if (password.equals(enCodeByMD5(inputString)))
return true;
return false;
}
private static String enCodeByMD5(String originString)
{
try
{
// 创建具有指定算法名称的信息摘要
MessageDigest mDigest = MessageDigest.getInstance("MD5");
// 使用指定的字节数组对摘要进行最后的更新,然后完成摘要计算
byte[] results = mDigest.digest(originString.getBytes());
String resultString = byteArrayToHexString(results);
return resultString.toUpperCase();
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
private static String byteArrayToHexString(byte[] b)
{
StringBuffer resultBuffer=new StringBuffer();
for(int i=0;i<b.length;i++)
resultBuffer.append(byteToHexString(b[i]));
return resultBuffer.toString();
}
private static String byteToHexString(byte b)
{
int n=b;
if(n<0)
n+=256;
int d1=n/16;
int d2=n%16;
return hexDigital[d1]+hexDigital[d2];//返回十六进制的字符串形式
}
public static void main(String []args)
{
//通常用于管理账户信息数据数据库,用户设置的密码通过加密后存放在数据库中,管理看不到原密码
String passwordString=Passwor.creatPassword("1234567");
System.out.println(passwordString);
String inputString="123456";
System.out.println(Passwor.authentiCreatPassword(passwordString, inputString));
inputString="1234567";
System.out.println(Passwor.authentiCreatPassword(passwordString, inputString));
}
梦里不知身是客,一晌贪欢。