MD5加密不加盐方法:
MD5是一个安全的散列算法(Hash Functions),是哈希算法中的一种,输入两个不同的明文不会得到相同的输出值。根据密文不能得到明文,其过程不可逆,也就是MD5只能用于加密认证,密文是无法解密。
代码:
- public String md5(String str) {
- MessageDigest messageDigest = null;
- try {
- messageDigest = MessageDigest.getInstance(”MD5”);
- messageDigest.reset();
- messageDigest.update(str.getBytes(”UTF-8”));
- } catch (NoSuchAlgorithmException e) {
- System.exit(-1);
- } 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();
- }
public String md5(String str) {
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update(str.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
System.exit(-1);
} 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加密加盐方法:
虽然MD5自身是不可逆,但是现在网络上的MD5数据库的数据量已经非常庞大,大部分常用密码都可以通过碰撞的方法给暴力破解出来,网上比较有名的有彩虹表都能很好的破解常用MD5加密,所以一般通过加盐的方式来提高数据的安全;
盐值加密:把你原来密码,加上一些盐然后再进行一些列的加密算法。
比如你的密码是:899312 用户名是:jiandan
在security 中盐值加密可以是这样加盐的899312{jiandan} 然后 ,在进行一些列的加密
代码:
- package com.itgocome.framework.security;
- import java.security.MessageDigest;
- import java.util.Random;
- import org.apache.commons.codec.binary.Hex;
- /**
- * @author Rain
- * @email osdc@msn.com
- * @date 2013-06-01
- */
- public class PasswordUtil {
- /**
- * 生成含有随机盐的密码
- */
- public static String generate(String password) {
- Random r = new Random();
- StringBuilder sb = new StringBuilder(16);
- sb.append(r.nextInt(99999999)).append(r.nextInt(99999999));
- int len = sb.length();
- if (len < 16) {
- for (int i = 0; i < 16 - len; i++) {
- sb.append(”0”);
- }
- }
- String salt = sb.toString();
- password = md5Hex(password + salt);
- char[] cs = new char[48];
- for (int i = 0; i < 48; i += 3) {
- cs[i] = password.charAt(i / 3 * 2);
- char c = salt.charAt(i / 3);
- cs[i + 1] = c;
- cs[i + 2] = password.charAt(i / 3 * 2 + 1);
- }
- return new String(cs);
- }
- /**
- * 校验密码是否正确
- */
- public static boolean verify(String password, String md5) {
- char[] cs1 = new char[32];
- char[] cs2 = new char[16];
- for (int i = 0; i < 48; i += 3) {
- cs1[i / 3 * 2] = md5.charAt(i);
- cs1[i / 3 * 2 + 1] = md5.charAt(i + 2);
- cs2[i / 3] = md5.charAt(i + 1);
- }
- String salt = new String(cs2);
- return md5Hex(password + salt).equals(new String(cs1));
- }
- /**
- * 获取十六进制字符串形式的MD5摘要
- */
- public static String md5Hex(String src) {
- try {
- MessageDigest md5 = MessageDigest.getInstance(”MD5”);
- byte[] bs = md5.digest(src.getBytes());
- return new String(new Hex().encode(bs));
- } catch (Exception e) {
- return null;
- }
- }
- public static void main(String[] args) {
- String password = generate(”admin”);
- System.out.println(verify(”admin”, password));
- }
- }
package com.itgocome.framework.security;
import java.security.MessageDigest;
import java.util.Random;
import org.apache.commons.codec.binary.Hex;
/**
* @author Rain
* @email osdc@msn.com
* @date 2013-06-01
*/
public class PasswordUtil {
/**
* 生成含有随机盐的密码
*/
public static String generate(String password) {
Random r = new Random();
StringBuilder sb = new StringBuilder(16);
sb.append(r.nextInt(99999999)).append(r.nextInt(99999999));
int len = sb.length();
if (len < 16) {
for (int i = 0; i < 16 - len; i++) {
sb.append("0");
}
}
String salt = sb.toString();
password = md5Hex(password + salt);
char[] cs = new char[48];
for (int i = 0; i < 48; i += 3) {
cs[i] = password.charAt(i / 3 * 2);
char c = salt.charAt(i / 3);
cs[i + 1] = c;
cs[i + 2] = password.charAt(i / 3 * 2 + 1);
}
return new String(cs);
}
/**
* 校验密码是否正确
*/
public static boolean verify(String password, String md5) {
char[] cs1 = new char[32];
char[] cs2 = new char[16];
for (int i = 0; i < 48; i += 3) {
cs1[i / 3 * 2] = md5.charAt(i);
cs1[i / 3 * 2 + 1] = md5.charAt(i + 2);
cs2[i / 3] = md5.charAt(i + 1);
}
String salt = new String(cs2);
return md5Hex(password + salt).equals(new String(cs1));
}
/**
* 获取十六进制字符串形式的MD5摘要
*/
public static String md5Hex(String src) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] bs = md5.digest(src.getBytes());
return new String(new Hex().encode(bs));
} catch (Exception e) {
return null;
}
}
public static void main(String[] args) {
String password = generate("admin");
System.out.println(verify("admin", password));
}
}