byte[] unencodedPassword = password.getBytes(); MessageDigest md = null; try { // first create an instance, given the provider md = MessageDigest.getInstance("MD5"); } catch (Exception e) { log.error("Exception: " + e); return password; } md.reset(); // call the update method one or more times // (useful when you don't know the size of your data, eg. stream) md.update(unencodedPassword); // now calculate the hash byte[] encodedPassword = md.digest(); StringBuffer buf = new StringBuffer(); for (int i = 0; i < encodedPassword.length; i++) { if ((encodedPassword[i] & 0xff) < 0x10) { buf.append("0"); } buf.append(Long.toString(encodedPassword[i] & 0xff, 16)); } return buf.toString(); }
{
StringBuffer buf = new StringBuffer("");
try
{
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.getBytes());
byte b[] = md.digest(); int i; for (int offset = 0; offset < b.length; offset++)
{
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
return buf.toString();
}
/**
* MD5加密运算
* @param password 原字符串
* @return MD5运算后的字符串
*/
public static String md5(String password)
{
byte[] unencodedPassword = password.getBytes(); MessageDigest md = null; try {
// first create an instance, given the provider
md = MessageDigest.getInstance("MD5");
} catch (Exception e) {
log.error("Exception: " + e); return password;
} md.reset(); // call the update method one or more times
// (useful when you don't know the size of your data, eg. stream)
md.update(unencodedPassword); // now calculate the hash
byte[] encodedPassword = md.digest(); StringBuffer buf = new StringBuffer(); for (int i = 0; i < encodedPassword.length; i++) {
if ((encodedPassword[i] & 0xff) < 0x10) {
buf.append("0");
} buf.append(Long.toString(encodedPassword[i] & 0xff, 16));
} return buf.toString();
}
至于数据库中不可见,简单的说。从程序校验用户密码开始,就是将密码进行加密后和数据库中的加密密文进行比较。新曾用户的时候也只是单纯的将密码进行sha1 ,md5加密后存放到数据库。至于算法到处都是随便一找就一堆。