直接上代码:
public class TestMD5 {
//普通使用MD5.
public static String getMD5Str(String str) {
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update(str.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException caught!");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
byte[] byteArray = messageDigest.digest();
StringBuffer md5StrBuff = new StringBuffer();
for (int i = 0; i < byteArray.length; i++) {
if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
else
md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
}
return md5StrBuff.toString();
}
//加盐使用md5
public static String getMD5Str(String salt, String source) {
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update(salt.getBytes("UTF-8")); //先加盐
messageDigest.update(source.getBytes("UTF-8")); //再放需要被加密的数据
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException caught!");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
byte[] byteArray = messageDigest.digest();
StringBuffer md5StrBuff = new StringBuffer();
for (int i = 0; i < byteArray.length; i++) {
if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
else
md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
}
return md5StrBuff.toString();
}
//加盐使用md5的第二种方式。
public static String getMD5Str2(String salt, String source) {
MessageDigest messageDigest = null;
byte[] byteArray = null;
try {
messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update(salt.getBytes("UTF-8")); //先加盐
byteArray = messageDigest.digest(source.getBytes("UTF-8"));//再放需要被加密的数据
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException caught!");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
StringBuffer md5StrBuff = new StringBuffer();
for (int i = 0; i < byteArray.length; i++) {
if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
else
md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
}
return md5StrBuff.toString();
}
/**
* mysql也能使用md5加盐的加密,不过是这样使用:
*
* select MD5(CONCAT('eteokues','123'))
* eteokues表示的是盐,
* 123表示的是需要被加密的数据。
* 所以,加盐的意思就是在需要被加密的数据123之前拼接头字符串。
*
* select MD5("123");
* 表示对123进行非加盐的加密。
* */
//fd4613f37bb1805db10acf1768fb8d76
public static void main(String[] args) {
System.out.println(getMD5Str("123"));
System.out.println(getMD5Str("eteokues123"));
System.out.println(getMD5Str("eteokues", "123"));
System.out.println(getMD5Str("eteokues", "123"));
}
}
MD5的二次加密实现方法
public class MD5Test {
public static void main(String[] args) {
//原始 密码
String source = "111111";
//盐
String salt = "qwerty";
//散列次数
int hashIterations = 2;
//上边散列1次:f3694f162729b7d0254c6e40260bf15c
//上边散列2次:36f2dfa24d0a9fa97276abbe13e596fc
//构造方法中:
//第一个参数:明文,原始密码
//第二个参数:盐,通过使用随机数
//第三个参数:散列的次数,比如散列两次,相当 于md5(md5(''))
Md5Hash md5Hash = new Md5Hash(source, salt, hashIterations);
String password_md5 = md5Hash.toString();
System.out.println(password_md5);
//第一个参数:散列算法
SimpleHash simpleHash = new SimpleHash("md5", source, salt, hashIterations);
System.out.println(simpleHash.toString());
}
}
这是使用shiro的jar得到的方法,
如果是直接实现,参考下面的:
public class MD5Test {
public static void main(String[] args) {
String source = "111111";
String salt = "qwerty";
//上边散列1次:f3694f162729b7d0254c6e40260bf15c
//散列2次:36f2dfa24d0a9fa97276abbe13e596fc
//散列两次实现原理
System.out.println(MD5Tools.getMD5Str(salt, source, 2));
}
}
class MD5Tools{
//普通使用MD5.
public static String getMD5Str(String str) {
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update(str.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException caught!");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
byte[] byteArray = messageDigest.digest();
StringBuffer md5StrBuff = new StringBuffer();
for (int i = 0; i < byteArray.length; i++) {
if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
else
md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
}
return md5StrBuff.toString();
}
//散列多次使用MD5
public static String getMD5Str(String salt, String source, int hashIterations) {
MessageDigest messageDigest = null;
byte[] byteArray = null;
try {
messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update(salt.getBytes("UTF-8")); //先加盐
byteArray = messageDigest.digest(source.getBytes("UTF-8")); //再放需要被加密的数据
if(hashIterations < 1) hashIterations = 1;
for(int i=0; i < hashIterations - 1; i++){
messageDigest.reset();
byteArray = messageDigest.digest(byteArray);
}
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException caught!");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
StringBuffer md5StrBuff = new StringBuffer();
for (int i = 0; i < byteArray.length; i++) {
if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
else
md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
}
return md5StrBuff.toString();
}
}