md5 加密

**
 * MD5加密类
 * 
 * @author  
 * 
 */
public class PassWordSecurity {

	private static final String HEX_NUMS_STR = "0123456789ABCDEF";
	/**
	 * 盐长度
	 */
	private static final int SALT_LENGTH = 6;

	/**
	 * 获取随机密文
	 * 
	 * @return 密码对象
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 */
	public static PassWordBean getRandomPwd() {
		PassWordBean passWord = new PassWordBean();
		// 生成随机密码
		SecureRandom random = new SecureRandom();
		String password = String.valueOf(Math.abs(random.nextLong()));
		String ciphertext;
		try {
			ciphertext = getEncryptedPwd(password);
			passWord.setPassword(password);
			passWord.setCiphertext(ciphertext);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
		return passWord;
	}

	/**
	 * 获取指定长度的随机密文
	 * 
	 * @return 密码对象
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 */
	public static PassWordBean getRandomPwd(int length) {
		PassWordBean passWord = new PassWordBean();
		// 生成随机密码
		SecureRandom random = new SecureRandom();
		double pross = (1 + random.nextDouble()) * Math.pow(10, length);

		String password = String.valueOf(pross);
		password = password.substring(1, length + 1).replace(".", "0");
		String ciphertext;
		try {
			ciphertext = getEncryptedPwd(password);
			passWord.setPassword(password);
			passWord.setCiphertext(ciphertext);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
		return passWord;
	}

	/**
	 * 对指定字符串进行加密,
	 * 
	 * @param password
	 *            要加密的字符串
	 * @return 32+SALT_LENGTH*2位的密文
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 */
	public static String getEncryptedPwd(String password)
			throws NoSuchAlgorithmException, UnsupportedEncodingException {
		int saltLength = SALT_LENGTH;

		if (StringUtil.isEmpty(password) || saltLength % 2 != 0) {
			return null;
		}
		byte[] salt = new byte[SALT_LENGTH];
		// 生成随机盐
		SecureRandom random = new SecureRandom();
		random.nextBytes(salt);

		MessageDigest md = MessageDigest.getInstance("MD5");
		md.update(salt);
		md.update(password.getBytes("UTF-8"));
		byte[] digest = md.digest();
		// 加密后的密码
		byte[] pwd = new byte[digest.length + SALT_LENGTH];
		// 将盐字节前半段拷贝到密文中
		System.arraycopy(salt, 0, pwd, 0, SALT_LENGTH / 2);
		// 将消息摘要拷贝到密文的盐字节后
		System.arraycopy(digest, 0, pwd, SALT_LENGTH / 2, digest.length);
		// 将盐字节后半段拷贝到最后位置
		System.arraycopy(salt, SALT_LENGTH / 2, pwd, SALT_LENGTH / 2
				+ digest.length, SALT_LENGTH / 2);

		return byteToHexString(pwd);
	}

	/**
	 * 验证字符串是否与密文一致
	 * 
	 * @param password
	 *            要比较的字符串
	 * @param encryptedPwd
	 *            要比较的密文
	 * @return 是否一致
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 */
	public static boolean validPassword(String password, String encryptedPwd) {
		int saltLength = SALT_LENGTH;
		if (StringUtil.isEmpty(password) || StringUtil.isEmpty(encryptedPwd)
				|| saltLength % 2 != 0) {
			return false;
		}

		byte[] pwd = hexStringToByte(encryptedPwd);
		byte[] salt = new byte[SALT_LENGTH];

		// 将私钥从密文字节数组中提取出来
		System.arraycopy(pwd, 0, salt, 0, SALT_LENGTH / 2);
		System.arraycopy(pwd, pwd.length - saltLength / 2, salt,
				SALT_LENGTH / 2, SALT_LENGTH / 2);

		MessageDigest md;
		try {
			md = MessageDigest.getInstance("MD5");
			md.update(salt);
			md.update(password.getBytes("UTF-8"));
			byte[] digest = md.digest();
			byte[] encryptDeigest = new byte[pwd.length - SALT_LENGTH];
			// 取得密文的的消息摘要
			System.arraycopy(pwd, SALT_LENGTH / 2, encryptDeigest, 0,
					encryptDeigest.length);
			return Arrays.equals(digest, encryptDeigest)?true:false;
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return false;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return false;
		} finally{
		
		}
	}

	/**
	 * 验证密码格式
	 * 
	 * @param password
	 *            要验证的密码
	 * @return 是否符合格式
	 */
	public static boolean check(String password) {
		return true;
	}

	/**
	 * 将指定byte数组转换成16进制字符串
	 * 
	 * @param b
	 *            要转换的byte数组
	 * @return 转换后的字符串
	 */
	private static String byteToHexString(byte[] b) {
		StringBuffer hexString = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			hexString.append(hex.toUpperCase());
		}
		return hexString.toString();
	}

	/**
	 * 将16进制字符串转换成字节数组
	 * 
	 * @param hex
	 *            要转换的字符串
	 * @return 转换后的字节数组
	 */
	private static byte[] hexStringToByte(String hex) {
		int len = (hex.length() / 2);
		byte[] result = new byte[len];
		char[] hexChars = hex.toCharArray();
		for (int i = 0; i < len; i++) {
			int pos = i * 2;
			result[i] = (byte) (HEX_NUMS_STR.indexOf(hexChars[pos]) << 4 | HEX_NUMS_STR
					.indexOf(hexChars[pos + 1]));
		}
		return result;
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值