`
sure
  • 浏览: 10638 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

使用重排的编码表,实现Base64编码/解码

阅读更多
Base64是一种基于64个可打印字符来表示二进制数据的表示方法。由于2的6次方等于64,所以每6个比特为一个单元,对应某个可打印字符。三个字节有24个比特,对应于4个Base64单元,即3个字节需要用4个可打印字符来表示。它可用来作为电子邮件的传输编码。在Base64中的可打印字符包括字母A-Z、a-z、数字0-9 ,这样共有62个字符,此外两个可打印符号在不同的系统中而不同。一些如uuencode的其他编码方法,和之后binhex的版本使用不同的64字符集来代表6个二进制数字,但是它们不叫Base64。
Base64常用于在通常处理文本数据的场合,表示、传输、存储一些二进制数据。包括MIME的email,email via MIME, 在XML中存储复杂数据.

public class Base64Util {
	private static final int ENCODE_BLOCK=4;
	private static final int UNENCODE_BLOCK=3;
	private static final byte DEFAULTF_PAD='=';
	private static final byte[] ENCODE_TABLE={
		48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98,
		99, 100, 101, 102, 103, 104, 105, 106, 107, 108,
		109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 
		119, 120, 121, 122, 65, 66, 67, 68, 69, 70, 71, 72,
		73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 
		86, 87, 88, 89, 90, 33, 45
	};
	private static final byte DECODE_TABLE[]={
		-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, -1, -1, -1, -1, -1, -1, -1, -1, 63, -1, -1, 0, 1, 2, 3, 
		4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 36, 37, 38, 39, 40, 41,
		42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
		60, 61, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 
		20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, 
		-1, -1, -1, -1
	 };
	public static String encode(String str){
		if(str==null||str.length()==0) return null;
		try {
			return encode(str.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}
	public static String encode(byte[] b) throws UnsupportedEncodingException{
		if(b==null||b.length==0) return null;
		int modulus = b.length%3;
		int len = getEncodeLength(b);
		byte bytes[] = new byte[len];
		int loop= b.length - modulus;
		int b1,b2,b3;
		for(int i=0,j=0;i<loop;){
			b1 = b[i++] & 255;//0xFF
			b2 = b[i++] & 255;
			b3 = b[i++] & 255;
			bytes[j++] = ENCODE_TABLE[(b1 >>> 2) & 63];
			bytes[j++] = ENCODE_TABLE[((b1 << 4) | (b2 >>> 4)) & 0x3f];
			bytes[j++] = ENCODE_TABLE[((b2 << 2) | (b3 >>> 6)) & 0x3f];
			bytes[j++] = ENCODE_TABLE[b3 & 0x3f];
		}
		if(modulus == 1){
			b1 = b[b.length-1] & 0xff;
			bytes[bytes.length-4] = ENCODE_TABLE[(b1 >>> 2) & 0x3f];
			bytes[bytes.length-3] = ENCODE_TABLE[(b1 << 4) & 0x3f];
			bytes[bytes.length-2] = DEFAULTF_PAD;
			bytes[bytes.length-1] = DEFAULTF_PAD;
		}
		if(modulus == 2){
			b1=b[b.length-2] & 0xff;
			b2=b[b.length-1] & 0xff;
			bytes[bytes.length-4] = ENCODE_TABLE[(b1 >>> 2) & 0x3f];
			bytes[bytes.length-3] = ENCODE_TABLE[((b1 << 4) | (b2 >> 4)) & 0x3f];
			bytes[bytes.length-2] = ENCODE_TABLE[(b2 << 2) & 0x3f];
			bytes[bytes.length-1] = DEFAULTF_PAD;
		}
		return new String(bytes,"UTF-8");
	}
	public static String decode(String str) throws UnsupportedEncodingException{
		if(str==null||str.length()==0) return str;
		 return decode(str.getBytes("UTF-8"));
	}
	public static String decode(byte[] b) throws UnsupportedEncodingException{
		byte[] bytes;
		int b1, b2, b3, b4;
		if(b == null || b.length == 0) return "";
		int len=b.length;
		int resultLength=getDecodeLength(b);
		bytes=new byte[resultLength];
		for(int i = 0, j = 0;i < len-4;){
			b1 = DECODE_TABLE [b[i++]];
			b2 = DECODE_TABLE [b[i++]];
			b3 = DECODE_TABLE [b[i++]];
			b4 = DECODE_TABLE [b[i++]];
			bytes[j++] = (byte)((b1 << 2) | (b2 >> 4));
			bytes[j++] = (byte)((b2 << 4) | (b3 >> 2));
			bytes[j++] = (byte)((b3 << 6) | b4);
		}
		if( b[len-2] == DEFAULTF_PAD){//means modulus=1
			b1 = DECODE_TABLE [b [len - 4]];
			b2 = DECODE_TABLE [b [len - 3]];
			bytes[resultLength - 1] =  (byte)((b1 << 2) | (b2 >> 4));
		}else if(b[len-1] == DEFAULTF_PAD){//means modulus=2
			b1 = DECODE_TABLE [b [len - 4]];
			b2 = DECODE_TABLE [b [len - 3]];
			b3 = DECODE_TABLE [b [len - 2]];
			bytes[resultLength - 2]=(byte)((b1 << 2) | (b2 >> 4));
			bytes[resultLength - 1]=(byte)((b2 << 4) | (b3 >> 2));
		}else{
			b1 = DECODE_TABLE [b[len - 4]];
			b2 = DECODE_TABLE [b[len - 3]];
			b3 = DECODE_TABLE [b[len - 2]];
			b4 = DECODE_TABLE [b[len - 1]];
			bytes[resultLength - 3] = (byte)((b1 << 2) | (b2 >> 4));
			bytes[resultLength - 2] = (byte)((b2 << 4) | (b3 >> 2));
			bytes[resultLength - 1] = (byte)((b3 << 6) | b4);
		}
		return new String(bytes,"UTF-8");
	}
	

	
	
	private static int getDecodeLength(byte[] b){
		int len=b.length;
		if(b[len-2] == DEFAULTF_PAD){//two '=',means modulus=1
			return (len / ENCODE_BLOCK) * UNENCODE_BLOCK - 2;
		}else if(b[len-1] == '='){
			return (len / ENCODE_BLOCK) * UNENCODE_BLOCK - 1;
		}else{
			return (len / ENCODE_BLOCK) * UNENCODE_BLOCK;
		}
	}
	
	private static int getEncodeLength(byte[] b){
		int len=b.length;
		if(len==0) return 0;
		return (((len+UNENCODE_BLOCK)-1)/UNENCODE_BLOCK)*ENCODE_BLOCK;
	}

	public static String base642Str(String str) throws UnsupportedEncodingException{
		return new String(Base64.decodeBase64(str.getBytes("UTF-8")),"UTF-8");
	}
	public static void main(String str[]) throws UnsupportedEncodingException{
		String testStr="测试base64位";
		String encodeStr=Base64Util.encode(testStr);
		System.out.println(encodeStr);
		String decodeStr=Base64Util.decode(encodeStr);
		System.out.println(decodeStr);
	}
	
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics