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 = "1";//byte[] result = XiaoMiBase64.encode(data.getBytes());//byte[] out = new byte[msg.length()*3/4];byte[] out = XiaoMiBase64.Base64Decode(msg.getBytes(), msg.length());System.out.println("AAAAA="+out.toString());for (int i = 0; i < out.length; i++){System.out.println("AAAAA out["+i+"]="+out[i]);}String str = "abcdefghijklmnopqrstuvwxyz";byte[] base64str = Base64Encode(str.getBytes(), str.length());System.out.println("AAAAA base64str ="+ new String(base64str));byte[] str1 = Base64Decode(base64str, base64str.length);System.out.println("AAAAA str1 ="+new String(str1));}}