java MD5完整加解密工具类

#第一部分
package com.yiban.rec.bill.parse.util;

import java.io.ByteArrayOutputStream;

public class Base64 {

    private static char[] base64EncodeChars = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
                                                           'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
                                                           'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
                                                           'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
                                                           'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
                                                           '8', '9', '+', '/' };

    private static byte[] base64DecodeChars = new byte[] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                                                           -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                                                           -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1,
                                                           -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
                                                           -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
                                                           13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1,
                                                           -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
                                                           37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
                                                           -1, -1, -1, -1, -1 };

    private Base64() {}

    public static synchronized String encode(byte[] data) {
        int len = data.length;
        int i = 0;
        int b1, b2, b3;
        StringBuilder sb = new StringBuilder(len);

        while (i < len) {
            b1 = data[i++] & 0xff;
            if (i == len) {
                sb.append(base64EncodeChars[b1 >>> 2]);
                sb.append(base64EncodeChars[(b1 & 0x3) << 4]);
                sb.append("==");
                break;
            }
            b2 = data[i++] & 0xff;
            if (i == len) {
                sb.append(base64EncodeChars[b1 >>> 2]);
                sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]);
                sb.append(base64EncodeChars[(b2 & 0x0f) << 2]);
                sb.append("=");
                break;
            }
            b3 = data[i++] & 0xff;
            sb.append(base64EncodeChars[b1 >>> 2]);
            sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]);
            sb.append(base64EncodeChars[((b2 & 0x0f) << 2) | ((b3 & 0xc0) >>> 6)]);
            sb.append(base64EncodeChars[b3 & 0x3f]);
        }
        return sb.toString();
    }

    public static synchronized byte[] decode(String str) {
        byte[] data = str.getBytes();
        int len = data.length;
        ByteArrayOutputStream buf = new ByteArrayOutputStream(len);
        int i = 0;
        int b1, b2, b3, b4;

        while (i < len) {

            /* b1 */
            do {
                b1 = base64DecodeChars[data[i++]];
            } while (i < len && b1 == -1);
            if (b1 == -1) {
                break;
            }

            /* b2 */
            do {
                b2 = base64DecodeChars[data[i++]];
            } while (i < len && b2 == -1);
            if (b2 == -1) {
                break;
            }
            buf.write((int) ((b1 << 2) | ((b2 & 0x30) >>> 4)));

            /* b3 */
            do {
                b3 = data[i++];
                if (b3 == 61) {
                    return buf.toByteArray();
                }
                b3 = base64DecodeChars[b3];
            } while (i < len && b3 == -1);
            if (b3 == -1) {
                break;
            }
            buf.write((int) (((b2 & 0x0f) << 4) | ((b3 & 0x3c) >>> 2)));

            /* b4 */
            do {
                b4 = data[i++];
                if (b4 == 61) {
                    return buf.toByteArray();
                }
                b4 = base64DecodeChars[b4];
            } while (i < len && b4 == -1);
            if (b4 == -1) {
                break;
            }
            buf.write((int) (((b3 & 0x03) << 6) | b4));
        }
        return buf.toByteArray();
    }
}



#第二部分
package com.yiban.rec.bill.parse.util;

/**
 * @author su.jf
 */
public class CodingUtil {

    public static byte[] base64Decode(String str) {
        return Base64.decode(str);
    }

    public static String base64Encode(byte[] data) {
        return Base64.encode(data);
    }

    private static final int BIT_SIZE   = 0x10;
    private static final int BIZ_ZERO   = 0X00;

    private static char[][]  charArrays = new char[256][];

    static {
        int v;
        char[] ds;
        String temp;
        for (int i = 0; i < charArrays.length; i++) {
            ds = new char[2];
            v = i & 0xFF;
            temp = Integer.toHexString(v);
            if (v < BIT_SIZE) {
                ds[0] = '0';
                ds[1] = temp.charAt(0);
            } else {
                ds[0] = temp.charAt(0);
                ds[1] = temp.charAt(1);
            }
            charArrays[i] = ds;
        }
    }

    public static String bytesToHexString(byte[] src) {
        return bytesToHexString(src, 0, src.length);
    }
    
    
    public static String bytesToHexString(byte[] src,int posction,int length) {
        HexAppender helper = new HexAppender(src.length * 2);
        if (src == null || src.length <= BIZ_ZERO) {
            return null;
        }
        int v;
        int lengthR = src.length > length ?length:src.length;
        char[] temp;
        for (int i = posction; i < lengthR; i++) {
            v = src[i] & 0xFF;
            temp = charArrays[v];
            helper.append(temp[0], temp[1]);
        }
        return helper.toString();
    }

    public static String bytesToHexStringSub(byte[] src, int length) {
       return bytesToHexString(src, 0, length);
    }

    
    public static String bytesToHexString(byte[] src, int startWith) {
    	return bytesToHexString(src, startWith, src.length);
    }
    
    /**
     * Convert hex string to byte[]
     * 
     * @param hexString the hex string
     * @return byte[]
     */
    public static byte[] hexStringToBytes(String hexString) {
        if ( hexString ==null) {
            return null;
        }
        int length = hexString.length() / 2;
        byte[] d = new byte[length];
        int pos;
        for (int i = 0; i < length; i++) {
            pos = i * 2;
            d[i] = (byte) (charToByte(hexString.charAt(pos)) << 4 | charToByte(hexString.charAt(pos + 1)));
        }
        return d;
    }

    /**
     * Convert char to byte
     * 
     * @param c char
     * @return byte
     */
    private static byte charToByte(char c) {
        return (byte) (c < 58 ? c - 48 : c < 71 ? c - 55 : c - 87);
    }
    
    
    private static final char[] DIS_256  = {'Ḁ','ḁ','Ḃ','ḃ','Ḅ','ḅ','Ḇ','ḇ','Ḉ','ḉ','Ḋ','ḋ','Ḍ','ḍ','Ḏ','ḏ','Ḑ','ḑ','Ḓ','ḓ','Ḕ','ḕ','Ḗ','ḗ','Ḙ','ḙ','Ḛ','ḛ','Ḝ','ḝ','Ḟ','ḟ','Ḡ','ḡ','Ḣ','ḣ','Ḥ','ḥ','Ḧ','ḧ','Ḩ','ḩ','Ḫ','ḫ','Ḭ','ḭ','Ḯ','ḯ','Ḱ','ḱ','Ḳ','ḳ','Ḵ','ḵ','Ḷ','ḷ','Ḹ','ḹ','Ḻ','ḻ','Ḽ','ḽ','Ḿ','ḿ','Ṁ','ṁ','Ṃ','ṃ','Ṅ','ṅ','Ṇ','ṇ','Ṉ','ṉ','Ṋ','ṋ','Ṍ','ṍ','Ṏ','ṏ','Ṑ','ṑ','Ṓ','ṓ','Ṕ','ṕ','Ṗ','ṗ','Ṙ','ṙ','Ṛ','ṛ','Ṝ','ṝ','Ṟ','ṟ','Ṡ','ṡ','Ṣ','ṣ','Ṥ','ṥ','Ṧ','ṧ','Ṩ','ṩ','Ṫ','ṫ','Ṭ','ṭ','Ṯ','ṯ','Ṱ','ṱ','Ṳ','ṳ','Ṵ','ṵ','Ṷ','ṷ','Ṹ','ṹ','Ṻ','ṻ','Ṽ','ṽ','Ṿ','ṿ',
    		'Ẁ','ẁ','Ẃ','ẃ','Ẅ','ẅ','Ẇ','ẇ','Ẉ','ẉ','Ẋ','ẋ','Ẍ','ẍ','Ẏ','ẏ','Ẑ','ẑ','Ẓ','ẓ','Ẕ','ẕ','ẖ','ẗ','ẘ','ẙ','ẚ','ẛ','ẜ','ẝ','ẞ','ẟ','Ạ','ạ','Ả','ả','Ấ','ấ','Ầ','ầ','Ẩ','ẩ','Ẫ','ẫ','Ậ','ậ','Ắ','ắ','Ằ','ằ','Ẳ','ẳ','Ẵ','ẵ','Ặ','ặ','Ẹ','ẹ','Ẻ','ẻ','Ẽ','ẽ','Ế','ế','Ề','ề','Ể','ể','Ễ','ễ','Ệ','ệ','Ỉ','ỉ','Ị','ị','Ọ','ọ','Ỏ','ỏ','Ố','ố','Ồ','ồ','Ổ','ổ','Ỗ','ỗ','Ộ','ộ','Ớ','ớ','Ờ','ờ','Ở','ở','Ỡ','ỡ','Ợ','ợ','Ụ','ụ','Ủ','ủ','Ứ','ứ','Ừ','ừ','Ử','ử','Ữ','ữ','Ự','ự','Ỳ','ỳ','Ỵ','ỵ','Ỷ','ỷ','Ỹ','ỹ','Ỻ','ỻ','Ỽ','ỽ','Ỿ','ỿ'};
    
    private static final int DIS_START = 'Ḁ';
    
    
    public static String bytesToByteString(byte[]datas){
    	char[]dataC = new char[datas.length];
    	for (int i = 0; i < datas.length; i++) {
    		dataC[i] = DIS_256[datas[i]+127];
		}
    	return new String(dataC);
    }
    
    public static byte[] byteStringToByte(String byteString){
    	char[]datas = byteString.toCharArray();
    	byte[]result = new byte[datas.length];
    	for (int i = 0; i < datas.length; i++) {
    		result[i] = (byte) (datas[i]-DIS_START-127);
		} 
    	return result;
    }
    
    
    
    public static long byte2long(byte[]value){
    	long temp = 0;  
        long res = 0;  
        for (int i=0;i<8;i++) {  
            res <<= 8;  
            temp = value[i] & 0xff;  
            res |= temp;  
        }  
        return res;
    }
    
   
    private static class HexAppender {

        private int    offerSet = 0;
        private char[] charData;

        public HexAppender(int size) {
            charData = new char[size];
        }

        public void append(char a, char b) {
            charData[offerSet++] = a;
            charData[offerSet++] = b;
        }

        @Override
        public String toString() {
            return new String(charData, 0, offerSet);
        }
    }

    
}

#第三部分
package com.yiban.rec.bill.parse.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

import com.alibaba.fastjson.util.IOUtils;

public class MD5Util {

	private static MessageDigest MD5;
	static {
		try {
			MD5 = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
	}

	public static String getMd5(File file){
		byte[]buffer = new byte[4096];
		InputStream inputStream = null;
		int postion = -1;
		try {
			MessageDigest md5 = null;
			try {
				md5 = MessageDigest.getInstance("MD5");
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
				return null;
			}
			inputStream = new FileInputStream(file);
			while((postion = inputStream.read(buffer))> 0){
				md5.update(buffer, 0, postion);
			}
			return CodingUtil.bytesToHexString(md5.digest());
		} catch (Throwable e) {
			e.printStackTrace();
		} finally{
			IOUtils.close(inputStream);
		}
		return "";
	}
	
	public static String getMd5(InputStream input){
		byte[]buffer = new byte[4096];
		int postion = -1;
		try {
			MessageDigest md5 = null;
			try {
				md5 = MessageDigest.getInstance("MD5");
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
				return null;
			}
			while((postion = input.read(buffer))> 0){
				md5.update(buffer, 0, postion);
			}
			return CodingUtil.bytesToHexString(md5.digest());
		} catch (Throwable e) {
			e.printStackTrace();
		} finally{
			IOUtils.close(input);
		}
		return "";
	}
	
	public static synchronized String getMd5(String msg) {
		return getMd5(msg.getBytes());
	}

	public static String getMd5(String source, String key){
		byte[] k_ipad = new byte[64];
		byte[] k_opad = new byte[64];
		byte[] keyb = null;
		byte[] value = null;
		try {
			keyb = key.getBytes("UTF8");
			value = source.getBytes("UTF8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		
		
		Arrays.fill(k_ipad, keyb.length, 64, (byte)0x36);
		Arrays.fill(k_opad, keyb.length, 64, (byte)0x5c);
		
		for (int i = 0; i < keyb.length; i++) {
			k_ipad[i] = (byte) (keyb[i] ^ 0x36);
			k_opad[i] = (byte) (keyb[i] ^ 0x5C);
		}
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			return null;
		}
		md.update(k_ipad);
		md.update(value);
		byte[] dg = md.digest();
		md.reset();
		md.update(k_opad);
		md.update(dg, 0, 16);
		dg = md.digest();
		return CodingUtil.bytesToHexString(dg);
	}

	public static synchronized byte[] getMd5Byte(String msg) {
		return getMd5Byte(msg.getBytes());
	}
	
	public static synchronized byte[] getMd5Byte16(String msg) {
		return getMd5Byte16(msg.getBytes());
	}
	
	public static synchronized String getMd5_16(String msg) {
		return CodingUtil.bytesToHexString(getMd5Byte(msg), 4, 12); 
	}
	
	public static synchronized byte[] getMd5Byte16(byte[] msg) {
		byte[]md5Byte = MD5.digest(msg);
		byte[]result = new byte[8];
		System.arraycopy(md5Byte, 4, result, 0, 8);
		return result;
	}
	

	public static synchronized byte[] getMd5Byte(byte[] msg) {
		return MD5.digest(msg);
	}
	
	

	public static synchronized String getMd5(byte[] msg) {
		MD5.update(msg);
		return CodingUtil.bytesToHexString(MD5.digest());
	}
	
	public  static void main(String[] args){
		System.out.println(MD5Util.getMd5("cmbc510623").toUpperCase());
	}
}

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值