首页 > 代码库 > BASE64算法及应用

BASE64算法及应用

Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一。使用base64具有以下三个优点,一是信息隐藏加密,避免了明码传输带来的安全性问题,二是把二进制byte流转为可见字符传输,使得很适合在URL中传输,三是避免了在不同平台和不同处理器之间调整大小端和拼接数据的麻烦,具有一定的跨平台和跨编程语言的能力。对于一些不能处理二进制byte流的弱语言或者脚本语言来说,也提供了一个应用窗口。当然他的弱点也相当明显,使原始数据变的比以后大了三分之一。


Base64的算法原理就是把三个字节24bit转为4个字节32bit。三个字节24bit,看作一个整体,每次取出一个6个bit存入新四个字节的低六个bit中,四次正好存四个字节,这样原数据如果为正数是0xff和三个字节,就会变成0x3f的四个字节。如果原数据表示正数,就是0~255,base64编码后,高二位填充为零后,就会变成四个0~63的整数,这样只要选取64个可见字符进行相应的替换,就组成了base64编码。如果使用通用的base64字符替换,网上的在线base64编码就能帮你解码。如果你希望安全些,就要自己打乱那些字符,或者使自己选择的种子字符。常见的字符种子:

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


一个简单的java语言base64编码:

public class XiaoMiBase64 {
	/* ----------------base64 from:http://base64.sourceforge.net/ ----------------- */

	/*
	** Translation Table as described in RFC1113
	*/
	/*
	static final byte[] cb64={ (byte) ‘A‘, (byte) ‘B‘,  
        (byte) ‘5‘, (byte) ‘6‘, (byte) ‘7‘, (byte) ‘8‘, (byte) ‘9‘,  
        (byte) ‘H‘, (byte) ‘I‘, (byte) ‘J‘, (byte) ‘K‘, (byte) ‘L‘,  
        (byte) ‘v‘, (byte) ‘w‘, (byte) ‘x‘, (byte) ‘y‘, (byte) ‘z‘,  
        (byte) ‘R‘, (byte) ‘S‘, (byte) ‘T‘, (byte) ‘U‘, (byte) ‘V‘,  
        (byte) ‘W‘, (byte) ‘X‘, (byte) ‘Y‘, (byte) ‘Z‘, (byte) ‘a‘,  
        (byte) ‘q‘, (byte) ‘r‘, (byte) ‘s‘, (byte) ‘t‘, (byte) ‘u‘,  
        (byte) ‘C‘, (byte) ‘D‘, (byte) ‘E‘, (byte) ‘F‘, (byte) ‘G‘,  
        (byte) ‘g‘, (byte) ‘h‘, (byte) ‘i‘, (byte) ‘j‘, (byte) ‘k‘,  
        (byte) ‘M‘, (byte) ‘N‘, (byte) ‘O‘, (byte) ‘P‘, (byte) ‘Q‘,  
        (byte) ‘b‘, (byte) ‘c‘, (byte) ‘d‘, (byte) ‘e‘, (byte) ‘f‘,  
        (byte) ‘0‘, (byte) ‘1‘, (byte) ‘2‘, (byte) ‘3‘, (byte) ‘4‘,  
        (byte) ‘l‘, (byte) ‘m‘, (byte) ‘n‘, (byte) ‘o‘, (byte) ‘p‘,  
        (byte) ‘+‘, (byte) ‘/‘ , (byte) ‘=‘};
        */
	static final String scb64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	static final byte[] cb64 = scb64.getBytes();

	/*
	** Translation Table to decode (created by author)
	*/
	//static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
	static byte base64_map_rev(byte c)
	{		   
		byte j;
		for(j = 0; j < 65; j++)
		{
			if(c == cb64[j]) 
			{ 
				return j;
			} 
		}
		return 0;
	}
	/*
	** decodeblock
	**
	** decode 4 ‘6-bit‘ characters into 3 8-bit binary bytes
	*/
	static byte[] decodeblock(byte[] in)
	{   
		byte[] out = new byte[3];
		
		out[0] = (byte) (in[0] << 2 | in[1] >> 4);
		out[1] = (byte) (in[1] << 4 | in[2] >> 2);
		out[2] = (byte) (((in[2] << 6) & 0xc0) | in[3]);
		
		return out;
	}

	static byte[] Base64Decode(byte[] b64_inbuf, long b64_len) 
	{ 
		int size = (int)((b64_len*3)/4);
		byte[] b64_outbuf = new byte[size];
		byte[] in= new byte[4];
		byte[] out;// = new byte[3];

	    for (int i = 0; i < b64_len; i++)
	    {
	    	b64_inbuf[i] = base64_map_rev(b64_inbuf[i]);
	    	if (b64_inbuf[i] == 64) {
				size--;
			}
			if (b64_inbuf[i] == 64) {
				size--;
			}
	    }
	    
		for (int i = 0; i < (b64_len/4); i++)
		{
			System.arraycopy(b64_inbuf, i*4, in, 0, in.length);
			out=decodeblock(in);
			System.arraycopy(out, 0, b64_outbuf, i*3, out.length);
		}
		
		byte[] b64_outbuf2 = new byte[size];
		System.arraycopy(b64_outbuf, 0, b64_outbuf2, 0, size);
		return b64_outbuf2;
	} 


	static byte[] encodeblock(byte[] in, int len )
	{
		System.out.println("AAAAA  len="+len);
		byte[] out = new byte[4];
		System.out.println("AAAAA  in[0]="+in[0]);
		System.out.println("AAAAA  (in[0] >> 2)="+(in[0] >> 2));
		out[0] = (byte) cb64[((in[0] >> 2))&0x3f];
		out[1] = (byte) cb64[((((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4)))&0x3f];
		out[2] = (byte) (len > 1 ? cb64[((((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6)))&0x3f] : ‘=‘);
		out[3] = (byte) (len > 2 ? cb64[((in[2] & 0x3f))&0x3f] : ‘=‘);
		
	    return out;
	}


	static byte[] Base64Encode(byte[] inbuf, long len)
	{
		int size = (int)((len*4)/3) + ((((len*4)%3)==0)?0:4);
		byte[] b64_outbuf = new byte[size];
		
		byte[] in = new byte[3];
		byte[] out;// = new byte[4];
		int i;

		for (i = 0; i < (len/3); i ++)
		{
			System.arraycopy(inbuf, i*3, in, 0, in.length);
			out=encodeblock(in, in.length);
			System.arraycopy(out, 0, b64_outbuf, i*4, out.length);
		}
		
		if ((len%3) != 0)
		{
			System.arraycopy(inbuf, i*3, in, 0, (int)(len-i*3));
			out=encodeblock(in, (int)(len-i*3));
			System.arraycopy(out, 0, b64_outbuf, i*4, out.length);
		}
		return b64_outbuf;
	}
	public static void main(String[] args) {
		
		String msg = "Z6V3ARgCAABPAgAAeQYAAKUIAABfEQAAKiMAAOiZAABMmgAA1H" +
				     "UBADh2AQCQygQAJCcFAORGBwAAAAAAAAAAAAAAAABFAQEBAQEB" +
				     "IQEhISEhISEBISEBAQEBAQEBASEhISEhISFhNYE1kTWRNZE1kT" +
				     "WRNZE1kTWxRQEBAQEBASEBISEhISEhASEhAQEBAQEBAQEhISEh" +
				     "ISEhcTWRNZE1kTWhRQEBAQEBASEBISEhISEhASEhAQEBAQEBAQ" +
				     "EhISEhISEhYTWBNYE1kTWBNcFFAQEBAQEBIQEhISEhISEBISEB" +
				     "AQEBAQEBASEhISEhISFxNZE1kTWRNZE1kTWBNYE1AQ==";
		//String data = http://www.mamicode.com/"1";>



BASE64算法及应用