JAVA MD5

来源:互联网 发布:阿里云服务器安全配置 编辑:程序博客网 时间:2024/06/05 08:58

MD5 实用类

package com.dce.framework.util;import java.security.MessageDigest;/** * Description: * 日期:2015-4-10 * 模块名称: * 描述: 标准MD5加密方法,使用java类库的security包的MessageDigest类处理 <BR> *      也可变为非标准MD5,请修改下面的移位算法 * 备注: * @author  * @version 1.0 */public class MD5 {/** * 获得MD5加密密码的方法 */public static String getMD5ofStr(String origString) {String origMD5 = null;try {MessageDigest md5 = MessageDigest.getInstance("MD5");byte[] result = md5.digest(origString.getBytes());origMD5 = byteArray2HexStr(result);} catch (Exception e) {e.printStackTrace();}return origMD5;}/** * 获得MD5加密密码的方法 */public static String getMD5ofStr(String origString, int encKey1, int encKey2) {String origMD5 = null;try {MessageDigest md5 = MessageDigest.getInstance("MD5");byte[] result = md5.digest(origString.getBytes());origMD5 = byteArray2HexStr(result, encKey1, encKey2);} catch (Exception e) {e.printStackTrace();}return origMD5;}/** * 处理字节数组得到MD5密码的方法 */private static String byteArray2HexStr(byte[] bs) {StringBuffer sb = new StringBuffer();for (byte b : bs) {sb.append(byte2HexStr(b));}return sb.toString();}/** * 处理字节数组得到MD5密码的方法 */private static String byteArray2HexStr(byte[] bs, int encKey1, int encKey2) {StringBuffer sb = new StringBuffer();for (byte b : bs) {sb.append(byte2HexStr(b, encKey1, encKey2));}return sb.toString();}/** * 字节标准移位转十六进制方法 */private static String byte2HexStr(byte b) {String hexStr = null;int n = b;if (n < 0) {// 若需要自定义加密,请修改这个移位算法即可n = b & 0x7F + 128;}hexStr = Integer.toHexString(n / 16) + Integer.toHexString(n % 16);return hexStr.toUpperCase();}/** * 字节标准移位转十六进制方法 */private static String byte2HexStr(byte b, int encKey1, int encKey2) {String hexStr = null;int n = b;if (n < 0) {// 若需要自定义加密,请修改这个移位算法即可n = b & encKey1 + encKey2;}hexStr = Integer.toHexString(n / 16) + Integer.toHexString(n % 16);return hexStr.toUpperCase();}/** * 提供一个MD5多次加密方法 */public static String getMD5ofStr(String origString, int times) {String md5 = getMD5ofStr(origString);for (int i = 0; i < times - 1; i++) {md5 = getMD5ofStr(md5);}return getMD5ofStr(md5);}/** * 密码验证方法 */public static boolean verifyPassword(String inputStr, String MD5Code) {return getMD5ofStr(inputStr).equals(MD5Code);}/** * 多次加密时的密码验证方法 */public static boolean verifyPassword(String inputStr, String MD5Code,int times) {return getMD5ofStr(inputStr, times).equals(MD5Code);}/** * 提供一个测试的主函数 */public static void main(String[] args) {System.out.println("12345:" + getMD5ofStr("12345"));System.out.println("111111:" + getMD5ofStr("111111"));}}


1 0
原创粉丝点击