Encoding Rules
The idea of BASE64 encoding is to re-encode the data using 64 basic ASCII characters. It splits the data that needs to be encoded into a byte array. A group of 3 bytes. The 24-bit data is sorted sequentially, and the 24 bits of data are divided into 4 groups, 6 bits per group. Then fill in a byte with two 0 in front of the highest bit of each group. This re-encodes a 3-byte set of data into 4 bytes. When the number of bytes of data to encode is not an integral multiple of 3, that is, the last group is less than 3 bytes at the time of grouping. At this point the last group is populated with 1 to 2 0 bytes. and add 1 to 2 "=" At the end after the last encoding is complete.
Code to implement:
//Here are 64 basic codesvarBase64encodechars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/";varBase64decodechars =NewArray (-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);//Method of EncodingfunctionBase64Encode (str) {varOut , I, Len; varC1, C2, C3; Len=str.length; I= 0; out= ""; while(I <Len) {C1= Str.charcodeat (i++) & 0xFF; if(i = =Len) { out+ = Base64encodechars.charat (C1 >> 2); out+ = Base64encodechars.charat ((C1 & 0x3) << 4); out+= "=="; Break; } C2= Str.charcodeat (i++); if(i = =Len) { out+ = Base64encodechars.charat (C1 >> 2); out+ = Base64encodechars.charat (((C1 & 0x3) << 4) | ((C2 & 0xF0) >> 4)); out+ = Base64encodechars.charat ((C2 & 0xF) << 2); out+= "="; Break; } C3= Str.charcodeat (i++); out+ = Base64encodechars.charat (C1 >> 2); out+ = Base64encodechars.charat (((C1 & 0x3) << 4) | ((C2 & 0xF0) >> 4)); out+ = Base64encodechars.charat (((C2 & 0xF) << 2) | ((C3 & 0xC0) >>6)); out+ = Base64encodechars.charat (C3 & 0x3F); } returnOut ;}//methods of decodingfunctionBase64decode (str) {varC1, C2, C3, C4; varI, Len, out; Len=str.length; I= 0; out= ""; while(I <Len) { Do{C1= Base64decodechars[str.charcodeat (i++) & 0xFF]; } while(I < len && C1 = =-1); if(C1 = =-1) Break; Do{C2= Base64decodechars[str.charcodeat (i++) & 0xFF]; } while(I < len && C2 = =-1); if(C2 = =-1) Break; out+ = String.fromCharCode ((C1 << 2) | ((C2 & 0x30) >> 4)); Do{C3= Str.charcodeat (i++) & 0xFF; if(C3 = = 61) returnOut ; C3=BASE64DECODECHARS[C3]; } while(I < Len && C3 = =-1); if(C3 = =-1) Break; out+ = String.fromCharCode (((C2 & 0XF) << 4) | ((C3 & 0x3C) >> 2)); Do{C4= Str.charcodeat (i++) & 0xFF; if(C4 = = 61) returnOut ; C4=BASE64DECODECHARS[C4]; } while(I < len && C4 = =-1); if(C4 = =-1) Break; out+ = String.fromCharCode (((C3 & 0x03) << 6) |C4); } returnOut ;}functionUtf16to8 (str) {varOut , I, Len, C; out= ""; Len=str.length; for(i = 0; i < Len; i++) {C=str.charcodeat (i); if((c >= 0x0001) && (c <= 0x007F) ) { out+=Str.charat (i); } Else if(C > 0x07ff) { out+ = String.fromCharCode (0xE0 | ((c >> b) & 0x0F)); out+ = String.fromCharCode (0x80 | ((c >> 6) & 0x3F)); out+ = String.fromCharCode (0x80 | ((c >> 0) & 0x3F)); } Else{ out+ = String.fromCharCode (0xC0 | ((c >> 6) & 0x1F)); out+ = String.fromCharCode (0x80 | ((c >> 0) & 0x3F)); } } returnOut ;}functionutf8to16 (str) {varOut , I, Len, C; varChar2, Char3; out= ""; Len=str.length; I= 0; while(I <Len) {C= Str.charcodeat (i++); Switch(C >> 4) { Case0: Case1: Case2: Case3: Case4: Case5: Case6: Case7: //0xxxxxxxOut + = Str.charat (i-1); Break; Case12: Case13: //110x xxxx 10xx xxxxCHAR2 = Str.charcodeat (i++); out+ = String.fromCharCode (((C & 0x1F) << 6) | (Char2 & 0x3F)); Break; Case14: //1110 xxxx 10xx xxxx 10xx xxxxCHAR2 = Str.charcodeat (i++); Char3= Str.charcodeat (i++); out+ = String.fromCharCode (((C & 0x0F) << 12) |((Char2& 0x3F) << 6) |((Char3& 0x3F) << 0)); Break; } } returnOut ;} Call://CodingValue =Base64Encode (Utf16to8 (SRC))//decodingValue = utf8to16 (Base64decode (SRC))