VAR Base64Encodechars = "Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 /";
VAR Base64Decodechars = new array (
-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, -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);
Function Base64encode (STR) {
VAR OUT, I, LEN;
VAR C1, C2, C3;
Len = str.length;
i = 0;
OUT = "";
While (i 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); } Return Out; } Function Base64Decode (str) { VAR C1, C2, C3, C4; VAR I, LEN, OUT; Len = str.length; i = 0; OUT = ""; While (i / * c1 * / Do { C1 = base64decodechars [str.charcodeat (i ) & 0xff]; WHILE (I IF (C1 == -1) Break; / * C2 * / Do { C2 = Base64DecodeChars [str.charcodeat (i ) & 0xff]; WHILE (I IF (C2 == -1) Break; OUT = String.Fromcharcode ((C1 << 2) | ((C2 & 0x30) >> 4)); / * C3 * / Do { C3 = Str.Charcodeat (i ) & 0xff; IF (C3 == 61) Return Out; C3 = base64decodechars [c3]; WHILE (I IF (C3 == -1) Break; OUT = String.Fromcharcode (((C2 & 0xF) << 4) | ((C3 & 0x3c) >> 2)); / * C4 * / Do { C4 = Str.Charcodeat (i ) & 0xff; IF (C4 == 61) Return Out; C4 = base64decodechars [c4]; WHILE (I IF (C4 == -1) Break; OUT = String.fromcharcode (((C3 & 0x03) << 6) | C4); } Return Out; } Function UTF16TO8 (STR) { VAR OUT, I, LEN, C; OUT = ""; Len = str.length; For (i = 0; i C = str.charcodeat (i); IF ((c> = 0x0001) && (c <= 0x007f)) { OUT = Str.Charat (i); } else IF (c> 0x07ff) { OUT = String.Fromcharcode (0xE0 | ((C >> 12) & 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); } } Return Out; } Function UTF8TO16 (STR) { VAR OUT, I, LEN, C; Var Char2, Char3; OUT = ""; Len = str.length; i = 0; While (i C = str.charcodeat (i ); Switch (c >> 4) { Case 0: Case 1: Case 2: Case 3: Case 4: Case 5: Case 6: Case 7: // 0xxxxxxx OUT = str.charat (i-1); Break; Case 12: Case 13: // 110x xxxx 10xx xxxx Char2 = str.charcodeat (i ); Out = string.fromcharcode ((C & 0x1f) << 6) | (char2 & 0x3f)); Break; Case 14: // 1110 xxxx 10xx xxxx 10xx xxxx Char2 = str.charcodeat (i ); Char3 = str.charcodeat (i ); Out = string.fromcharcode (((c & 0x0f) << 12) | ((char2 & 0x3f) << 6) | ((Char3 & 0x3f) << 0)); Break; } } Return Out; } Function DOIT () { VAR f = Document.f f.output.value = base64encode (UTF16TO8 (F.Source.Value)) f.Decode.Value = UTF8TO16 (Base64Decode (f.output.value)) } script> HEAD> Original code Base64 encode Base64 Decode Form> Body> Base64 h1>