Original: "Turn" Javascript Base64 encoding and decoding
<HTML><Head><METAhttp-equiv= "Msthemecompatible"CONTENT= "Yes" ><Metahttp-equiv= "Content-type"content= "text/html; Charset=unicode" ><Scriptlanguage= "JavaScript"type= "Text/javascript"src=".. /var.js "></Script><title>Base64 Encode and Decode</title><style></style><Scripttype= "Text/javascript"language= "JavaScript" >varBase64encodechars ="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, 9, 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 9, 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, 4 2, 1, 1,-1,-1,-1 , +-------+functionBase64Encode (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 ;}functionBase64decode (str) {varC1, C2, C3, C4; varI, Len, out; len = str.length; i = 0; out =""; while(i < len) {/ * C1 * / Do{c1 = Base64decodechars[str.charcodeat (i++) & 0xFF]; } while(i < len && C1 = =-1); if(C1 = =-1) Break; / * C2 * / Do{c2 = Base64decodechars[str.charcodeat (i++) & 0xFF]; } while(i < len && C2 = =-1); if(C2 = =-1) Break; Out + = String.fromCharCode ((C1 << 2) | ((C2 & 0x30) >> 4)); / * C3 * / Do{c3 = Str.charcodeat (i++) & 0xFF; if(C3 = =)returnOut ; C3 = Base64decodechars[c3]; } while(i < len && C3 = =-1); if(C3 = =-1) Break; Out + = String.fromCharCode (((C2 & 0XF) << 4) | ((C3 & 0x3C) >> 2)); / * C4 * / Do{C4 = str.charcodeat (i++) & 0xFF; if(C4 = =)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; Case: Case://110x xxxx 10xx xxxxchar2 = str.charcodeat (i++); Out + = String.fromCharCode (((C & 0x1F) << 6) | (Char2 & 0x3F)); Break; Case://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 ;}functionChartohex (str) {varOut , I, Len, C, H; out =""; len = str.length; i = 0; while(i < len) {c = str.charcodeat (i++); H = c.tostring (16); if(H.length < 2) H ="0"+ H; Out + ="\\x"+ H +" "; if(i > 0 && i% 8 = = 0) out + ="\ r \ n"; } returnOut ;}functionDoencode () {varsrc = document.getElementById (' src '). Value; document.getElementById (' dest '). Value = Base64Encode (Utf16to8 (SRC));}functionDodecode () {varsrc = document.getElementById (' src '). Value; varopts = document.getElementById (' opt '); if(opts.checked) {document.getElementById (' dest '). Value = Chartohex (Base64decode (SRC)); } Else{document.getElementById (' dest '). Value = utf8to16 (Base64decode (SRC)); }}</Script></Head><BODYbgcolor= "#EAF0F8"TopMargin= "Ten"LeftMargin= "Ten"RightMargin= "Ten"BottomMargin= "Ten" ><DivID= "contain"style="font-family: Tahoma, Arial; font-size:11pt "> <DivID= "MAINBG" > <Divstyle="Color: #000080; Margin-bottom: Ten "><b>Base64 Encode/decode</b></Div> <Div> <textareaID= "src"rows=11cols=60value="" name= "src" ></textarea> <Divstyle="margin: 0 0 "> <inputonclick="Doencode ();" type=buttonvalue= "Encode"name= "Encode" > <inputonclick="Dodecode ();" type=buttonvalue= "Decode"name= "Decode" > <inputtype= "checkbox"value= "Hex"name= "opt"ID= "opt" >Hex </Div> <textareaID= "Dest"rows=11cols=60value="" name= "Dest" ></textarea> </Div> </Div></Div></BODY></HTML>
Javascript BASE64 encoding and decoding